• Resolved befla.net

    (@beflanet)


    Hi. Thank you very much for this great plugin. I′m trying to get the SKU for each variant with this code but I don′t get it:

    public function value( array $parameters ) {
    		$order_items = $this->get_order()->get_items();
    $items_with_sku = array();
    foreach ( $order_items as $item ) {
     if ( $item instanceof \WC_Order_Item_Product ) {
        $item_with_sku = array();
        $item_with_sku[] = $item->get_meta( 'op_item_data_details', true );
        $items_with_sku[] = implode(' ', $item_with_sku);
      }
    }
    
    return implode('<br>', $items_with_sku['sku']);
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Bartek

    (@bartj)

    Hello @beflanet!

    The issue lies in the last line of foreach loop. You call $item->get_meta( 'op_item_data_details', true); and store the result in an array. What type of values should that meta call returned? I guess, it’s string (I don’t know exactly). Then you implode, but your current data looks like $item_with_sku = [ 'meta_data_from_call' ], so implode only changes this array into a string, and thus there’s only one item in an array, you get the same string as returned from meta call.

    And finally, on plain array you try to call index named ‘sku’.

    If you expect array to be returned from meta call, then you could just write it as:

    
    if ( $item instanceof \WC_Order_Item_Product ) {
     $item_with_sku = $item->get_meta( 'op_item_data_details' );
     $items_with_sku[] = $item_with_sku['sku'];
    }
    

    and finally: return implode('<br>', $items_with_sku ); without calling any array index.

    Thread Starter befla.net

    (@beflanet)

    Hi @bartj Thank you very much for your fast replay.
    In the table woocommerce_order_itemmeta I have the meta_key _op_item_data_details with the meta_value s:9:"parent_id";i:3667;s:3:"sku";s:13:"0000000036702";s:3:"qty";i:654;. I′m trying to get the SKU, so the output should be 0000000036702.
    I changed the code to

    public function value( array $parameters ) {
    		$order_items = $this->get_order()->get_items();
    $items_with_sku = array();
    foreach ( $order_items as $item ) {
     if ( $item instanceof \WC_Order_Item_Product ) {
     $item_with_sku = $item->get_meta( 'op_item_data_details' );
     $items_with_sku[] = $item_with_sku['sku'];
    }
    }
    
    return implode('<br>', $items_with_sku );

    but still I`m getting an empty output.

    Plugin Contributor Bartek

    (@bartj)

    Hi @beflanet!

    As for me, the output should display correctly. I am not sure about two things here:

    1. How data are saved to the table? For what I recall, any meta data about order should be stored in wp_postmeta table, associated with post of shop_order or product type. Table woocommerce_order_itemmeta is rather only for fast lookup, not to store there data in general.
    2. You mentioned your meta_key is _op_item_data_details, what implies private meta field in WP/WC context. Yet, you try to retrieve that data by calling op_item_data_details (without underscore in the beginning). I’m not sure if it’s allowed.

    As to the point 2 – I looked up the WC source code, and comment to the method read_meta_data (which is called underneath your get_meta call) leaves no doubts. It ignores any internal properties, then you may not be able to get required data in such way.

    Plugin Contributor Bartek

    (@bartj)

    Marking topic as resolved due to no further response. Feel free to start another issue if you will find some problems in our plugin ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Get SKU’ is closed to new replies.