Save product custom field radio button value in cart and display it on Cart page

后端 未结 1 1141
失恋的感觉
失恋的感觉 2021-01-20 11:50

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_produc         


        
1条回答
  •  爱一瞬间的悲伤
    2021-01-20 12:31

    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(){
        ?>
             
    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.

    0 讨论(0)
提交回复
热议问题