Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter tahik

    (@tahik)

    HI Marc,

    I am trying to retrieve image via function like this:

    function mkl_pc_get_order_details_image_url( WC_Order_Item_Product $item, WC_Product $orig_product = null ) : string {
    $merge_saved = mkl_pc_find_merge_url_in_item( $item );
    if ( $merge_saved ) return mkl_pc_force_size_params( $merge_saved );

    if ( $orig_product instanceof WC_Product ) {
    $ids = mkl_pc_collect_merge_ids_from_config_data( $item );
    if ( empty($ids) ) {
    $ids = mkl_pc_collect_merge_ids_from_raw_item( $item );
    }
    if ( $ids ) {
    $rebuilt = mkl_pc_build_order_details_image_url( $orig_product->get_id(), $ids );
    if ( $rebuilt ) return mkl_pc_force_size_params( $rebuilt );
    }
    }
    return '';
    }

    Where mkl_pc_find_merge_url_in_item() is:

    function mkl_pc_find_merge_url_in_item( WC_Order_Item_Product $item ) : string {
    $candidate_keys = [
    '_mkl_pc_composed_image_url',
    '_mkl_pc_cart_image',
    '_mkl_pc_preview_url',
    '_mkl_pc_summary_image',
    '_mkl_pc_merge_url',
    ];
    foreach ( $candidate_keys as $k ) {
    $val = $item->get_meta( $k );
    $url = mkl_pc_merge_url_from_maybe_html( $val );
    if ( $url ) return $url;
    }

    $html = $item->get_meta( 'Konfiguration' );
    if ( $html === '' ) {
    $html = mkl_pc_get_item_meta_via_db( $item->get_id(), 'Konfiguration' );
    }
    $url = mkl_pc_merge_url_from_konfiguration_html( (string) $html );
    if ( $url ) return $url;

    foreach ( $item->get_meta_data() as $meta ) {
    $url = mkl_pc_merge_url_from_maybe_html( $meta->value );
    if ( $url ) return $url;
    }

    return '';
    }

    mkl_pc_collect_merge_ids_from_config_data() is :

    function mkl_pc_collect_merge_ids_from_config_data( WC_Order_Item_Product $item ) : array {
    $config = $item->get_meta( '_configurator_data', true );
    if ( empty( $config ) ) {
    $config = $item->get_meta( '_pc_configurator_data', true );
    }

    if ( empty( $config ) ) {
    // DB fallbacks
    $maybe = mkl_pc_get_item_meta_via_db( $item->get_id(), '_configurator_data' );
    $config = maybe_unserialize( $maybe );
    if ( empty( $config ) ) {
    $maybe = mkl_pc_get_item_meta_via_db( $item->get_id(), '_pc_configurator_data' );
    $config = maybe_unserialize( $maybe );
    }
    }

    if ( empty( $config ) ) return [];

    $ids = [];
    foreach ( (array) $config as $choice ) {
    if ( is_object( $choice ) && ! empty( $choice->layer_data->image ) ) {
    $ids[] = (int) $choice->layer_data->image;
    } elseif ( is_array( $choice ) && ! empty( $choice['image'] ) ) {
    $ids[] = (int) $choice['image'];
    }
    }
    $seen = []; $out = [];
    foreach ( $ids as $id ) {
    if ( $id && empty( $seen[ $id ] ) ) { $out[] = $id; $seen[ $id ] = 1; }
    }
    return $out;
    }

    mkl_pc_collect_merge_ids_from_raw_item():

    function mkl_pc_collect_merge_ids_from_raw_item( WC_Order_Item_Product $item ) : array {
    $raw = $item->get_meta('_configurator_data_raw', true);
    if ( empty($raw) ) {
    $maybe = mkl_pc_get_item_meta_via_db( $item->get_id(), '_configurator_data_raw' );
    $raw = maybe_unserialize( $maybe );
    }
    if ( empty($raw) || !is_array($raw) ) return [];
    $ids = [];
    foreach ( $raw as $row ) {
    if ( is_object($row) && !empty($row->image) ) $ids[] = (int) $row->image;
    elseif ( is_array($row) && !empty($row['image']) ) $ids[] = (int) $row['image'];
    }
    $seen = []; $out = [];
    foreach ($ids as $id) { if ($id && empty($seen[$id])) { $out[] = $id; $seen[$id]=1; } }
    return $out;
    }

    and mkl_pc_build_order_details_image_url():

    function mkl_pc_build_order_details_image_url( $product_id, array $merge_ids ) : ?string {
    $product_id = (int) $product_id;
    if ( $product_id <= 0 || empty($merge_ids) ) return null;
    $merge_str = implode('-', array_map('intval', $merge_ids));
    $base = rtrim( home_url(), '/' );
    return "{$base}/wp-json/mkl_pc/v1/merge/{$product_id}/{$merge_str}/";
    }

    The result is called in this function:

    function mkl_pc_attach_best_image_priority_merge( $item, $config_data, $orig_product, $de_id ) {
    $result = [ 'de_attachment_id' => null, 'en_attachment_id' => null ];

    $merge_ids = mkl_pc_collect_merge_ids( $config_data );
    if ( ! empty( $merge_ids ) && $orig_product instanceof WC_Product ) {
    $merge_url = mkl_pc_build_merge_url( $orig_product->get_id(), $merge_ids, 1280, 1280 );
    if ( $merge_url ) {
    $try = attach_image_to_product_wpml_aware( $de_id, $merge_url, 'de', 'en' );
    if ( ! empty( $try['de_attachment_id'] ) ) {
    error_log( "Attached MERGE image {$try['de_attachment_id']} to DE product {$de_id}" );
    return $try;
    } else {
    error_log( "Failed to attach MERGE image to DE product {$de_id}" );
    }
    }
    }

    $best_url = mkl_pc_best_image_url( $item, $config_data );
    if ( $best_url ) {
    $try = attach_image_to_product_wpml_aware( $de_id, $best_url, 'de', 'en' );
    if ( ! empty( $try['de_attachment_id'] ) ) {
    error_log( "Attached config image {$try['de_attachment_id']} to DE product {$de_id}" );
    return $try;
    }
    }

    if ( $orig_product instanceof WC_Product ) {
    $orig_thumb_id = (int) get_post_thumbnail_id( $orig_product->get_id() );
    if ( $orig_thumb_id ) {
    set_post_thumbnail( $de_id, $orig_thumb_id );
    error_log( "Reused original product thumbnail {$orig_thumb_id} for DE product {$de_id}" );
    return [ 'de_attachment_id' => $orig_thumb_id, 'en_attachment_id' => null ];
    }
    }

    return $result;
    }

    which is assigned to a variable

    $de_att = mkl_pc_attach_best_image_priority_merge( $item, $config_data, $orig_product, $de_id );

    tha is then uset to set post thumbnail

    if ( ! empty( $de_att['en_attachment_id'] ) ) {
    set_post_thumbnail( $en_id, (int) $de_att['en_attachment_id'] );
    error_log( "Set duplicated EN image {$de_att['en_attachment_id']} for EN {$en_id}" );
    } elseif ( ! empty( $de_att['de_attachment_id'] ) ) {
    set_post_thumbnail( $en_id, (int) $de_att['de_attachment_id'] );
    error_log( "Reused DE image {$de_att['de_attachment_id']} for EN {$en_id}" );
    }

    Thank you for your time and have a nice day

Viewing 1 replies (of 1 total)