Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
720 views
in Technique[技术] by (71.8m points)

wordpress - Add custom Product data dynamically as item meta data on the Order

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 );
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...