3

I have added some custom options on woocommerce single product page using the code below on my theme's functions.php:

     function options_on_single_product(){
     ?>
      <input type="radio" name="option1" checked="checked" value="option1"> option 1 <br />
      <input type="radio" name="option1" value="option2"> option 2
       <?php
      }
       add_action("woocommerce_before_add_to_cart_button", "options_on_single_product");

Now i want to display the selected option value on cart page. Please help me to do this. Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Archana
  • 337
  • 2
  • 4
  • 12
  • WooCommerce already has a function for this, see the documentation here: https://docs.woocommerce.com/document/variable-product/ – Nathaniel Flick Apr 15 '17 at 08:10

1 Answers1

3

Here is the complete code to Store product custom field in cart object and display that in Cart and Checkout pages:

// Output the Custom field in Product pages
add_action("woocommerce_before_add_to_cart_button", "options_on_single_product", 1);
function options_on_single_product(){
    ?>
        <label for="custom_field">
            <input type="radio" name="custom_field" checked="checked" value="option1"> option 1 <br />
            <input type="radio" name="custom_field" value="option2"> option 2
        </label> <br />
    <?php
}

// Stores the custom field value in Cart object
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_product_field_data', 10, 2 );
function save_custom_product_field_data( $cart_item_data, $product_id ) {
    if( isset( $_REQUEST['custom_field'] ) ) {
        $cart_item_data[ 'custom_field' ] = esc_attr($_REQUEST['custom_field']);
        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

// Outuput custom Item value in Cart and Checkout pages
add_filter( 'woocommerce_get_item_data', 'output_custom_product_field_data', 10, 2 );
function output_custom_product_field_data( $cart_data, $cart_item ) {
    if( isset( $cart_item['custom_field'] ) ) {
        $cart_data[] = array(
            'key'       => __('Custom Item', 'woocommerce'),
            'value'     => $cart_item['custom_field'],
            'display'   => $cart_item['custom_field'],
        );
    }
    return $cart_data;
}

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

This code is tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • #LoicTheAztec thanks for your answer. one more question how can i change cart total price after applying a coupon code. – Archana Apr 17 '17 at 07:01
  • @Archana It depends of the coupon type (settings), but normally the cart total price is updated when you apply a coupon… My code just save to the cart and display your custom field. – LoicTheAztec Apr 17 '17 at 07:36
  • Hi #LoicTheAztec thanks for your reply. yes i know cart total is automaticaly updated after apply a coupon but i want to divide the total price(remain after apply coupon code) by some percantage. is there any way to do this. Thanks – Archana Apr 17 '17 at 07:48