将产品附加字段添加到WooCommerce上的特定产品 [英] Add a product add-on field to specific products on WooCommerce

查看:75
本文介绍了将产品附加字段添加到WooCommerce上的特定产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改代码以不显示所有WooCommerce产品上的文本区域,而仅显示2个,这是在 functions.php 文件下的WordPress子主题上.

I need to change the code to not display text area on all of my WooCommerce products but only 2, this is on my WordPress child theme under functions.php file.

我已将$ product_id更改为 $ product_id = 2130 (我的特定产品ID),不知道我是否应该更改所有 $ product_id $ _ POST 以便将此代码仅显示在2种产品上

I have changed $product_id to $product_id = 2130(my specific product id), not sure if I'm supposed to change all the $product_id or $_POST for this code to display on only 2 products

它用于特定产品或具有名称个性化的产品.我尝试将代码$ product_id更改为许多其他扩展,但无济于事.我不确定是否应该将所有$ product_id替换为什么代码,或者将其替换为 $ _ POST 并将其全部替换.

It is for a specific product or products that gets personalized with a name. I have tried changing the code $product_id to numerous other extensions to no avail. I'm not sure if I should replace all the $product_id with what code, or should it be at $_POST and replace all of them.

add_action( 'woocommerce_before_add_to_cart_button', 'custom_product_add_on', 9 );  
function custom_product_add_on() {
    $value = isset( $_POST['_custom_text_add_on'] ) ? sanitize_text_field( $_POST['_custom_text_add_on'] ) : '';

    echo '<div>
        <label>Custom Text Add-On <abbr class="required" title="required">*</abbr></label>
        <p>
             <input name="_custom_text_add_on" value="' . $value . '">
        </p>
    </div>';
}

add_filter( 'woocommerce_add_to_cart_validation', 'custom_product_add_on_validation', 10, 3 );
function custom_product_add_on_validation( $passed, $product_id, $qty ){
    if( isset( $_POST['_custom_text_add_on'] ) && sanitize_text_field( 
$_POST['_custom_text_add_on'] ) == '' ) {
         wc_add_notice( 'Custom Text Add-On is a required field', 'error' );
         $passed = false;
    }
    return $passed;
}

add_filter( 'woocommerce_add_cart_item_data', 'custom_product_add_on_cart_item_data', 10, 2 );
function custom_product_add_on_cart_item_data( $cart_item, $product_id ){
    if( isset( $_POST['_custom_text_add_on'] ) ) {
        $cart_item['custom_text_add_on'] = sanitize_text_field( 
$_POST['_custom_text_add_on'] );
    }
    return $cart_item;
}

add_filter( 'woocommerce_get_item_data', 'custom_product_add_on_display_cart', 10, 2 );
function custom_product_add_on_display_cart( $_data, $cart_item ) {
    if ( isset( $cart_item['custom_text_add_on'] ) ){
        $data[] = array(
            'name' => 'Custom Text Add-On',
            'value' => sanitize_text_field( $cart_item['custom_text_add_on'] )
        );
    }
    return $data;
}

add_action( 'woocommerce_add_order_item_meta', 'custom_product_add_on_order_item_meta', 10, 2 );
function custom_product_add_on_order_item_meta( $item_id, $values ) {
    if ( ! empty( $values['custom_text_add_on'] ) ) {
        wc_add_order_item_meta( $item_id, 'Custom Text Add-On', 
$values['custom_text_add_on'], true );
    }
}

add_filter( 'woocommerce_order_item_product', 'custom_product_add_on_display_order', 10, 2 );
function custom_product_add_on_display_order( $cart_item, $order_item ){
    if( isset( $order_item['custom_text_add_on'] ) ){
        $cart_item_meta['custom_text_add_on'] = 
$order_item['custom_text_add_on'];
    }
    return $cart_item;
}

add_filter( 'woocommerce_email_order_meta_fields', 'custom_product_add_on_display_emails' );
function custom_product_add_on_display_emails( $fields ) { 
    $fields['custom_text_add_on'] = 'Custom Text Add-On';
    return $fields; 
}

推荐答案

我几乎在所有地方都重新访问了您的代码,进行了一些更改并删除了不必要的功能.

I have revisited your code mostly everywhere, making some changes and removing unnecessary functions.

您将必须在前两个函数的 $ defined_products_ids 变量数组中定义所需的产品ID.

You will have to define your wanted product IDs in the first 2 functions, in $defined_products_ids variable array.

我在可变产品中的下拉菜单/附加组件之间添加了12px的间隔,并添加到购物车按钮.

I have added add a space of 12px between dropdowns / add-on and add to cart button in variable products.

完整代码:

// Display Product add on custom fields
add_action( 'woocommerce_before_add_to_cart_button', 'display_product_add_on_custom_fields', 9 );
function display_product_add_on_custom_fields() {
    global $product;

    $defined_products_ids = array( 37, 53 );

    if( in_array($product->get_id(), $defined_products_ids) ) {
        $value = isset($_POST['custom_text']) ? $_POST['custom_text'] : '';

        // Adding some space between dropdowns and add to cart button for variable products
        $style = $product->is_type('variable') ? ' style="padding-bottom:12px;"' : '';

        echo '<div class="product-add-on"' . $style . '>
            <label>'. __('Add your customized text', 'woocommerce') . ' ' .
                '<abbr class="required" title="required">*</abbr>
            </label>
            <p>
                <input name="custom_text" value="' . $value . '">
            </p>
        </div>';
    }
}

// Product add on custom fields validation
add_filter( 'woocommerce_add_to_cart_validation', 'product_add_on_custom_fields_validation', 10, 3 );
function product_add_on_custom_fields_validation( $passed, $product_id, $quantity ){
    $defined_products_ids = array( 37, 53 );

    if( isset( $_POST['custom_text'] ) && empty( $_POST['custom_text'] )
    && in_array($product_id, $defined_products_ids) ) {
         wc_add_notice( '"Custom Text" is a required field', 'error' );
         $passed = false;
    }
    return $passed;
}

// Add custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 10, 2 );
function add_custom_cart_item_data( $cart_item_data, $product_id ){
    if( isset($_POST['custom_text']) ) {
        $cart_item_data['custom_text'] = sanitize_text_field( $_POST['custom_text'] );
        $cart_item_data['unique_key'] = md5( microtime().rand() ); // Make each item unique
    }
    return $cart_item_data;
}

// Display custom cart item data on cart and checkout
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $cart_item_data, $cart_item ) {
    if ( isset( $cart_item['custom_text'] ) ){
        $cart_item_data[] = array(
            'name' => __('Your custom text', 'woocommerce'),
            'value' => $cart_item['custom_text'] // Already sanitized field
        );
    }
    return $cart_item_data;
}

// Save and display custom item data everywhere on orders and email notifications
add_action( 'woocommerce_checkout_create_order_line_item', 'add_product_custom_field_as_order_item_meta', 10, 4 );
function add_product_custom_field_as_order_item_meta( $item, $cart_item_key, $values, $order ) {
    if ( isset($values['custom_text']) ) {
        $item->update_meta_data('Your custom text', $values['custom_text'] );
    }
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.经过测试和工作.

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

这篇关于将产品附加字段添加到WooCommerce上的特定产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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