3

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

5

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.

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hey LoicThe Aztec, I am kind of adding meta fields like that but using javascript on click of add to cart. till the payment it is showing correctly, but after the payment is done. On the order view and the order email, it is showing me all the custom fields on the first occurrence of the product and the second occurrence it is showing without custom fields – hailey Turlapati Feb 01 '17 at 05:38
  • @haileyTurlapati You should update your question with all your customization code and details, without it nobody can help… [**This is a kind of related question**](http://stackoverflow.com/questions/41915857/dynamically-calculate-product-price-based-on-custom-fields-values-when-added-to/41917319#41917319), so may be your problem is in the way that you set the information in cart, first. This **woocommerce_add_order_item_meta** is the correct hook that is maid to display that data in orders view and in email notifications… – LoicTheAztec Feb 01 '17 at 06:10
  • @haileyTurlapati I have updated my answer… This is going to work for you. – LoicTheAztec Feb 02 '17 at 07:25
  • thank you @Loic , I resolved the issue. I am deleting the early record and updating with new record. – hailey Turlapati Feb 08 '17 at 04:57