Wordpress 在WooCommerce中保存和显示所选产品的自定义数据

Wordpress 在WooCommerce中保存和显示所选产品的自定义数据,wordpress,woocommerce,Wordpress,Woocommerce,我使用代码,在产品编辑页面上显示复选框级别。当经理单击此复选框时,单个产品的页面上会出现一个选择框,允许客户选择烘焙级别 选择产品并将其添加到购物车时,所选值将显示在购物车本身中。此值还显示在签出页面、感谢页面、订单、电子邮件通知以及管理面板中的订单编辑页面上 代码如下: // Display Checkbox Field add_action('woocommerce_product_options_general_product_data', 'roast_custom_field_add'

我使用代码,在产品编辑页面上显示复选框级别。当经理单击此复选框时,单个产品的页面上会出现一个选择框,允许客户选择烘焙级别

选择产品并将其添加到购物车时,所选值将显示在购物车本身中。此值还显示在签出页面、感谢页面、订单、电子邮件通知以及管理面板中的订单编辑页面上

代码如下:

// 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);
    }
}
这段代码在店面主题中运行良好,但由于某些原因,它在我之前购买的主题中不起作用。开发人员无法帮助,他们说我需要联系编写此代码的人。这就是为什么

我还使用在店面和购买的主题中工作的代码。给你,那些。它在这两个主题中都非常有效

据我所知,这是由于语法回声。在烘焙级别的形式中,此语法为,因此显示该形式。在购物车或结帐页面上显示所选数据时,不支持此语法

更新

下面是没有echo无法工作的代码:

// 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);
    }
}
我要求更改代码,使其具有语法echo,以便使用echo输出所选数据。我将为你的帮助而高兴

以下代码应该可以工作


对不起,我可能没解释清楚。我的代码可以工作,但只在店面主题中工作。在其他主题中,由于某种原因,他不显示选定的数据,只显示单个产品页面上的表单烘焙级别。据我所知,这是由于语法回声。在烘焙级别的形式中,此语法为,因此显示该形式。在购物车或结帐页面上显示所选数据时,不支持此语法。我要求更改我的代码,使其具有语法echo,以便使用echo输出所选数据。我已更新答案检查是否有效您的代码有一些错误我已解决…因此请检查是否有效您应该告诉我哪些代码不适用于您检查是否有效,如果这不起作用,让我知道你在用哪一个。。。您的主题可能不允许您添加功能或优先级可能是原因…不幸的是,这不起作用。我什么都用。代码中的所有内容,我都需要。有两种变体:1。使用echo设置输出数据。2.在选择字段中为每个选项指定其自己的名称,例如,roast1、roast2等,然后输出如示例所示-
/*---------------------------------------------------------------
*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 class="roast_select">';

        $select = woocommerce_form_field( 'roast_custom_options', array(
            'type'          => 'select',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Roast Level'),
            'required'      => false,
            'return'       => false,
            'options'   => array(
                ''      => 'Please select',
                'Blue'  => 'Blue',
                'Rare'  => 'Rare',
                'Medium Rare'   => 'Medium Rare',
                'Medium'    => 'Medium',
                'Medium Well'   => 'Medium Well',
                'Well Done' => 'Well Done'
            )
        ), '' );
        echo $select;
        echo '</div>';
    }
}
/*---------------------------------------------------------------
* Add as custom cart item data
---------------------------------------------------------------*/
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 10, 21 );
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, 21 );
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, 21 );
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, 21 );
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 ,$item->get_id());
    }
}

/*--------------------------------------------------------------------
The following code played the important role in your theme

Your add to cart form takes the values when user clicks on add to cart button 
After it ajax runs and takes the values from the butoons custom attribute
like data-product_id, data-roast_custom_options So i have added it using jquery 
check the code and your site. All the codesprovided by me working now.

--------------------------------------------------------------------*/

add_action('wp_footer','add_footer_script');
function add_footer_script(){
    ?>
    <script>
       jQuery('#roast_custom_options').on('change',function(){
           var roast_level = jQuery(this).val();
           /*console.log(roast_level); */
           var button = jQuery(this).closest('form').find('.add_to_cart_button'); console.log(button); 
           jQuery(button).attr('data-roast_custom_options',roast_level);
        });

    </script>
    <?php
}