【问题标题】:Add a product add-on field to specific products on WooCommerce在 WooCommerce 上为特定产品添加产品附加字段
【发布时间】:2019-05-20 11:31:11
【问题描述】:

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

我已将 $product_id 更改为 $product_id = 2130(我的特定产品 ID), 不确定我是否应该更改所有 $product_id$_POST 以使此代码仅显示在 2 个产品上

它适用于使用名称进行个性化的特定产品。我尝试将代码 $product_id 更改为许多其他扩展,但无济于事。我不确定是否应该将所有 $product_id 替换为什么代码,还是应该将其替换为 $_POST 并全部替换。

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

【问题讨论】:

  • 为什么要添加这么多cmets而不是更改问题本身?
  • 对不起,我是论坛的新手,我很抱歉,现在会弄清楚如何做到这一点。
  • 抱歉,我已删除 cmets 并编辑了我的问题。

标签: php wordpress woocommerce cart checkout


【解决方案1】:

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

您必须在前 2 个函数中定义您想要的产品 ID,在 $defined_products_ids 变量数组中。

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

完整代码:

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

【讨论】:

  • 如果我想更改此代码,而不是输入用户可以上传 1 ,2 或 4 个文件的文本,还必须像上面一样附加到电子邮件和订单,我应该只创建一个容器和让上面指向容器而不是 custom_text_area
  • @RompelStompel 这是不同的东西,你应该先自己搜索并尝试一些代码......然后如果需要,你将能够提出一个新问题。
  • ,这个论坛好像没有个人信息,我只是想感谢你的时间和努力。我找到了一篇您评论的帖子,正是我需要的内容,但仅适用于上述代码等特定产品。 stackoverflow.com/questions/51716368/…
猜你喜欢
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-07
  • 2018-12-14
相关资源
最近更新 更多