在WooCommerce中到处保存和显示产品选择的自定义数据 [英] Save and display product selected custom data everywhere in WooCommerce

查看:58
本文介绍了在WooCommerce中到处保存和显示产品选择的自定义数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,该代码显示了产品编辑页面上的复选框。当我单击此复选框时,单个产品页面上将显示一个选择框。

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.

这是我的代码:

// 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是我的代码:

// 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?

推荐答案

我急忙重新访问了您的代码,还添加了一些缺少的功能并删除了另一个功能:

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 );
    }
}

代码会出现在您活跃孩子的function.php文件中主题(或活动主题)。经过测试并有效。

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

在前端购物车页面上:

在后端订单编辑页面上:

关于电子邮件通知:

这篇关于在WooCommerce中到处保存和显示产品选择的自定义数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆