Save and display product selected custom data everywhere in WooCommerce

后端 未结 2 1106
栀梦
栀梦 2020-12-21 17:07

I have a code that shows the checkbox on the product edit page. When I click on this checkbox, a select box is displayed on the single product page.

Here is my code:

相关标签:
2条回答
  • 2020-12-21 17:35

    I have revisited your code "in a hurry", also added some missing function and removed another one:

    // Display Checkbox Field
    add_action('woocommerce_product_options_general_product_data', 'roast_custom_field_add');
    function roast_custom_field_add(){
        global $post;
    
        // Checkbox
        woocommerce_wp_checkbox(
            array(
                'id' => '_roast_checkbox',
                'label' => __('Roast Level', 'woocommerce' ),
                'description' => __( 'Enable roast level!', 'woocommerce' )
            )
        );
    }
    
    // Save Checkbox Field
    add_action('woocommerce_process_product_meta', 'roast_custom_field_save');
    function roast_custom_field_save($post_id){
        // Custom Product Checkbox Field
        $roast_checkbox = isset( $_POST['_roast_checkbox'] ) ? 'yes' : 'no';
        update_post_meta($post_id, '_roast_checkbox', esc_attr( $roast_checkbox ));
    }
    
    // Display Select Box
    add_action( 'woocommerce_before_add_to_cart_button', 'add_roast_custom_field', 0 );
    function add_roast_custom_field() {
        global $product;
    
        // If is single product page and have the "roast_checkbox" enabled we display the field
        if ( is_product() && $product->get_meta( '_roast_checkbox' ) === 'yes' ) {
    
            echo '<div>';
    
            woocommerce_form_field( 'roast_custom_options', array(
                'type'          => 'select',
                'class'         => array('my-field-class form-row-wide'),
                'label'         => __('Roast Level'),
                'required'      => false,
                'options'   => array(
                    ''      => 'Please select',
                    'Blue'  => 'Blue',
                    'Rare'  => 'Rare',
                    'Medium Rare'   => 'Medium Rare',
                    'Medium'    => 'Medium',
                    'Medium Well'   => 'Medium Well',
                    'Well Done' => 'Well Done'
                )
            ), '' );
    
            echo '</div>';
        }
    }
    
    // Add as custom cart item data
    add_filter( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 10, 3 );
    function add_custom_cart_item_data($cart_item_data, $product_id, $variation_id ){
        if( isset( $_POST['roast_custom_options'] ) ) {
            $cart_item_data['roast_option'] = wc_clean( $_POST['roast_custom_options'] );
        }
        return $cart_item_data;
    }
    
    // Add custom fields values under cart item name in cart
    add_filter( 'woocommerce_cart_item_name', 'roast_custom_field', 10, 3 );
    function roast_custom_field( $item_name, $cart_item, $cart_item_key ) {
        if( ! is_cart() )
            return $item_name;
    
        if( isset($cart_item['roast_option']) ) {
            $item_name .= '<br /><div class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $cart_item['roast_option'] . '</div>';
        }
        return $item_name;
    }
    
    // Display roast custom fields values under item name in checkout
    add_filter( 'woocommerce_checkout_cart_item_quantity', 'roast_custom_checkout_cart_item_name', 10, 3 );
    function roast_custom_checkout_cart_item_name( $item_qty, $cart_item, $cart_item_key ) {
        if( isset($cart_item['roast_option']) ) {
            $item_qty .= '<br /><div class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $cart_item['roast_option'] . 'гр.</div>';
        }
        return $item_qty;
    }
    
    // Save chosen slelect field value to each order item as custom meta data and display it everywhere
    add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product_fitting_color', 10, 4 );
    function save_order_item_product_fitting_color( $item, $cart_item_key, $values, $order ) {
        if( isset($values['roast_option']) ) {
            $key = __('Roast Level', 'woocommerce');
            $value = $values['roast_option'];
            $item->update_meta_data( $key, $value );
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    On frontend Cart page:

    On backend Order edit pages:

    On email notifications:

    0 讨论(0)
  • 2020-12-21 17:57

    Your checkbox value is stored to database, try to change this code

    $roast_checkbox = isset( $_POST['_roast_checkbox'] ) ? 'yes' : 'no';
    

    with

    if ( isset( $_POST['_roast_checkbox'] ) {
    $roast_checkbox = $_POST['_roast_checkbox'];
    //update_post_meta
    }
    
    0 讨论(0)
提交回复
热议问题