Displaying custom product data in Order items view

非 Y 不嫁゛ 提交于 2019-12-05 22:49:46
LoicTheAztec

UPDATE:

See this recent related questions (Real working examples):

So you need first to set a product attribute by displayed value in your Orders items, to get a clean displayed label for this value. Then you have to set this attributes with any value in your related products (These mandatories attributes values are going to be replaced by your customs values.

So if your attribute Name is "Primary choice" (for example) you will set pa_primary-choice in:

wc_add_order_item_meta($item_id, 'pa_primary-choice', $custom_field_value, true);

Then you will get in your orders items detais below product title item the label name with your custom field value (here "XXXX" is your displayed custom field value):

Primary choice: XXXX

Sorry, but as your question is not really clear, not very detailed and without any code that you are using. I suppose that you are talking about product custom fields that you have set for your variable products and that are reflected on cart object items.

You might need some additional code to add this information as meta data so that it can be seen as part of the order. You can try something like below, adapting the code depending on how the data is set in your products page, on cart and submitted in checkout

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

    $custom_field1 = $_POST['my_custom_field1_key'];
    // or $values['my_custom_field1_key'];
    $custom_field2 = $_POST['my_custom_field2_key'];
    // or $values['my_custom_field2_key'];

    if ( !empty($custom field1) ) 
        wc_add_order_item_meta($item_id, 'custom_meta_key1', $custom_field1, true);

    if ( !empty($custom field2) ) 
        wc_add_order_item_meta($item_id, 'custom_meta_key2', $custom_field2, true);

    // And so on …
}

For product variations it's more complicated, if you want to get something clean for each item, I mean a title with the value of your custom chosen field. For the moment with the provided information and used code in your question is not possible to help you more than that…

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.


For "Sold Individually", what you can do is to use woocommerce_is_sold_individually hook that will remove quantity front end settings from products:

add_filter( 'woocommerce_is_sold_individually', '__return_true' );

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!