I have added custom price on woocommerce single product page using the code below on my theme's functions php:
<?php // 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(){ ?> <ul> <li> <div> <input type="radio" name="filter_opt" checked> <label for="opt1"> <span></span>Single Site License </label> </div> <p>Nunc placerat mi id nisi interdum is mollis. Praesent pharetra, justo ut sceleris que the mattis, leo quam.</p> </li> <li> <div> <input type="radio" name="filter_opt"> <label for="opt2"> <span></span>2 Sites License</label> </div> <p>Nunc placerat mi id nisi interdum is mollis. Praesent pharetra, justo ut sceleris que the mattis, leo quam.</p> </li> </ul> <?php } Now i want to display the selected option value on cart page. Please help me to do this. Thanks
1 Answer
// 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' ] = $_REQUEST['custom_field']; // below statement make sure every add to cart action as unique line item $cart_item_data['unique_key'] = md5( microtime().rand() ); WC()->session->set( 'my_order_data', $_REQUEST['custom_field'] ); } 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( !empty( $cart_data ) ) $custom_items = $cart_data; if( isset( $cart_item['custom_field'] ) ) { $custom_items[] = array( 'key' => __('Custom Item', 'woocommerce'), 'value' => $cart_item['custom_field'], 'display' => $cart_item['custom_field'], ); } return $custom_items; } 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.
1