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

后端 未结 2 1660
星月不相逢
星月不相逢 2020-12-20 00:02

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 differe

相关标签:
2条回答
  • 2020-12-20 00:20

    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 );
    
    0 讨论(0)
  • 2020-12-20 00:33

    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
    
    0 讨论(0)
提交回复
热议问题