3
votes

I have a issue with add to cart. I have a product with different custom conditions to choose. When the customer selects a specific choice. it adds to the cart. When the customer choice another choice and adds. Its shows as a second item in the cart. Which is okay. But after the payment, the order shows the both the custom option under the item 1 and item 2 without the custom data.

so I was thinking instead of showing the same product as different items. I want to update the product custom data, so it will always show as single item.

(Note: I have enabled the option "Sold Individually" from the admin, but its working).

If not can you tell me how to show in the order correctly, so the email sent after payment, orders page will be shown correctly.

NOTE: The custom data I am using is loc and date.

Thank you in advance.

1

1 Answers

5
votes

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.