Save and display product selected custom data everywhere in WooCommerce

跟風遠走 提交于 2020-01-11 10:02:31

问题


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:

// 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 $post;

    // If is single product page and have the "roast_checkbox" enabled we display the field
    if ( is_product() && get_post_meta( $post->ID, '_roast_checkbox', true ) == '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>';
    }
}

When you click on the checkbox, the selection field is shown correctly.

But the data, after selecting the options are not saved.

Also, these data are not displayed on the cart page, on the checkout page, in the order, etc.

Нere is my code:

// Save roast custom field
add_action( 'woocommerce_add_to_cart', 'roast_custom_field_add_to_cart', 20, 6 );
function roast_custom_field_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){

    if( isset($_POST['roast_custom_options']) ){
        $roast_values   = (array) get_post_meta( $product_id, '_roast_custom_options_values', true );
        $roast_values[] = sanitize_text_field( $_POST['roast_custom_options'] );
        update_post_meta( $product_id, '_roast_custom_options_values', $roast_values );
    }
}

// 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( $roast_values = $cart_item['data']->get_meta('_roast_custom_options_values') ) {
        $item_name .= '<br /><div class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $roast_values . ' </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( $roast_values = $cart_item['data']->get_meta('_roast_custom_options_values') ) {
        $item_qty .= '<br /><div class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $roast_values . ' </div>';
    }
    return $item_qty;
}

// Display roast custom fields values on orders and email notifications
add_filter( 'woocommerce_order_item_name', 'roast_custom_order_item_name', 10, 2 );
function roast_custom_order_item_name( $item_name, $item ) {
    $product = $item->get_product();

    if( $roast_values = $product->get_meta('_roast_custom_options_values') ) {
        $item_name .= '<br /><span class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $roast_values . ' </span>';
    }
    return $item_name;
}

How to fix the code so that everything works correctly?


回答1:


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:




回答2:


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
}


来源:https://stackoverflow.com/questions/55433246/save-and-display-product-selected-custom-data-everywhere-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!