编程语言
首页 > 编程语言> > php – 在WooCommerce中保存并显示产品选择的自定义数据

php – 在WooCommerce中保存并显示产品选择的自定义数据

作者:互联网

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

这是我的代码:

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

单击复选框时,选择字段将正确显示.

但是,选择选项后的数据不会保存.

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

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

如何修复代码,以便一切正常?

解决方法:

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

// 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文件中.经过测试和工作.

在前端购物车页面:

enter image description here

在后端订单编辑页面:

enter image description here

电子邮件通知:

enter image description here

标签:cart,custom-fields,php,wordpress,woocommerce
来源: https://codeday.me/bug/20190827/1744881.html