Add custom Product data dynamically as item meta d

2020-02-13 02:28发布

Is it possible to add a metadata on a product which is inside the Order?

In this case, there would be one metadata but each product (in the order) would have different value. Example:

Order 1:
     * Product 1 
     Sample Meta: Meta1

     * Product 2
     Sample Meta: Meta2

Thanks.


Update1 :

I'm now stuck on how would I grab the values from the woocommerce_add_cart_item_data filter. I was able to successfully add the meta data from there.

I need to grab those values for me to use in this woocommerce_add_order_item_meta action hook.

This is how I successfully added the metadata to the filter:

function add_cart_item_data( $cart_item_data, $product_id ) {
    $cart_item_data[ "meta1" ] = $_POST["meta1"];  
    $cart_item_data[ "meta2" ] = $_POST["meta2"]; 
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 99, 2 );

2条回答
手持菜刀,她持情操
2楼-- · 2020-02-13 02:48

After creating attribute "The Meta" and adding it to the product.

I answered my problem with the following:

First: Add the meta via woocommerce_add_cart_item_data filter hook. This would add first the meta to the items inside the cart.

function add_cart_item_data( $cart_item_data, $product_id ) {
    $cart_item_data[ "meta1" ] = $_POST["meta1"];  
    $cart_item_data[ "meta2"] = $_POST["meta2"]; 
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 10, 2 );

Second: Save the meta added to the cart to the actual items purchased within the order

function adding_custom_data_in_order_items_meta( $item_id, $values, $cart_item_key ) {
    if ( isset($values['meta1']) && isset($values['meta2']) ) {
        $the_meta = $values['meta1'] . '.' .  $values['meta2'];
        wc_add_order_item_meta($item_id, 'pa-the-meta', $the_meta );
    }
}
add_action('woocommerce_add_order_item_meta','adding_custom_data_in_order_items_meta', 10, 3 );
查看更多
Viruses.
3楼-- · 2020-02-13 03:01

Yes this is possible using a custom function hooked in woocommerce_add_order_item_meta action hook.

add_action('woocommerce_add_order_item_meta','adding_custom_data_in_order_items_meta', 1, 1 );
function adding_custom_data_in_order_items_meta( $item_id, $values, $cart_item_key ) {

    // The corresponding Product Id for the item:
    $product_id = $values[ 'product_id' ];

    $custom_meta_value = $values['my_custom_field1_key'];
    // or $custom_meta_value = $_POST['my_custom_field_key'];
    // or $custom_meta_value = get_post_meta( $values[ 'product_id' ], '_some_meta_key', true );

    if ( !empty($custom_meta_value) ) 
        wc_add_order_item_meta($item_id, 'custom_meta_key', $custom_meta_value, true);

    // And so on …
}

But as your question is not detailed and you are not showing any code related to how this custom data is set in your products or passed to the cart object, is not possible to help more that that.


Update related to your answer: (See in the linked answer with a working real example):

Adding user custom field value to order items details

So, as in this linked answer, you will need to create first a product attribute because in your code, wc_add_order_item_meta($item_id, 'The Meta', $the_meta ); is not correct as the second argument has to be a meta_key slug without uppercase and space characters, so 'The Meta' is not convenient and not recommended...

This product attribute creation name will be (regarding your answer code): 'The Meta' and the slug 'the_meta'.

Then you will have to set it in each related product with a mandatory value (just any value, as this value is going to be replaced by your custom value below).

So once done, your code will be:

add_action('woocommerce_add_order_item_meta','adding_custom_data_in_order_items_meta', 10, 3 );
function adding_custom_data_in_order_items_meta( $item_id, $values, $cart_item_key ) {
    if ( isset($values['meta1']) && isset($values['meta2']) ) {
        $custom_value = $values['meta1'] . '.' .  $values['meta2'];
        wc_add_order_item_meta($item_id, 'pa_the-meta', $custom_value );
    }
}

Then you will get this kind of display in your Orders items ('XXXX' is your custom value here):

The Meta: XXXX
查看更多
登录 后发表回答