在 WooCommerce 中随处保存和显示产品选择的自定义数据

Save and display product selected custom data everywhere in WooCommerce

我有一个代码可以在产品编辑页面上显示复选框。当我点击这个复选框时,单个产品页面上会显示一个 select 框。

这是我的代码:

// 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>';
    }
}

单击复选框时,select离子字段会正确显示。

但是 select 选项后的数据没有保存。

此外,这些数据不会显示在购物车页面、结帐页面、订单等

这是我的代码:

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

如何修复代码以使一切正常工作?

我已经重新访问了您的代码 "in a hurry",还添加了一些缺少的功能并删除了另一个:

// 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 文件。已测试并有效。

在前端购物车页面上:

后台订单编辑页面:

关于电子邮件通知:

您的复选框值已存储到数据库中,请尝试更改此代码

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

if ( isset( $_POST['_roast_checkbox'] ) {
$roast_checkbox = $_POST['_roast_checkbox'];
//update_post_meta
}