将自定义属性选择值添加到购物车、结帐、订单和电子邮件通知

Adding Custom Attribute selected value to Cart, Checkout, Order and email notification

My Environment is:
- WordPress 4.7.4
- Debian Linux 8
- WooCommerce 3.0.5
- 256M Memory

我尝试了多种解决方案,包括:

但是,我仍然没有得到正确的结果,在这个项目的这一点上,时间是至关重要的。一个精度,我所有属性值都一样的价格…

我创建了一个带有自定义表单的自定义单product.php模板:

<form id="add-product-form" class="add-product-form form-horizontal" method="post" action="">
    <input name="add-to-cart" value="15709" type="hidden">
    <div class="form-group color-dropdown">
        <label class="col-sm-3 control-label">Color</label>
        <select id="color-options" class="col-sm-9 color-select" name="color" required="">
        <option value="auburn">Auburn</option>
        <option value="black">Black</option>
        <option value="mahogany-ash">Mahogany Ash</option>
        <option value="mocha">Mocha</option>                            </select>
    </div>
    <div class="form-group quantity-area">
        <label class="col-sm-3 control-label">Qty.</label>
        <input name="quantity" id="quantity" maxlength="2" class="col-sm-9 quantity-input" required="" type="text">
    </div>
    <button id="submit-to-cart" value="Add to Cart" class="btn btn-a2c submit" name="submit" type="submit"><i class="fa fa-plus" aria-hidden="true"></i> Add to Cart</button>
</form>

此表单使用 AJAX post 方法并按预期添加到购物车。

但是:

  1. 我没有在 WC 购物车页面上看到他们选择的颜色
  2. 我在 WC Checkout 页面上没有看到他们选择的颜色
  3. 我没有看到他们在相应电子邮件中选择的颜色。我知道我必须编辑 email-order-items.php 但我不知道这里的正确方法。

我的问题:

那么如何将自定义属性选定值添加到购物车、结帐、订单和电子邮件通知?

我知道我可以采用可变产品方法,但即使在 256M 内存下,可变产品区域中的变体菜单不断旋转,所以我永远无法进入该区域添加变体。

Instead of overriding your template single-product.php, it is better to use the original hook do_action('woocommerce_before_add_to_cart_button'); that are made to inject code in it through some custom hooked function.

据我了解,您不需要使用可变产品。您想要使用将显示自定义选择器字段的单个产品,您可以在该字段中使用为此产品选择的值设置现有的“颜色”属性。

这是钩子函数:

// Add the custom field selector based on attribute "Color" values set in the simple product
add_action( 'woocommerce_before_add_to_cart_button', 'action_before_add_to_cart_button' );
function action_before_add_to_cart_button(){
    global $product;
    foreach($product->get_attributes() as $attribute_slug => $attribute_obj){
        if($attribute_slug == 'pa_color'){
            echo '<div class="form-group color-dropdown">
            <label class="col-sm-3 control-label" for="custom_pa_color">'. __('Color', 'woocommerce') .'</label>
            <select id="custom_pa_color" class="col-sm-9 color-select" name="custom_pa_color" required="">';

            foreach( $attribute_obj->get_terms() as $term_obj){
                $term_id   = $term_obj->id;
                $term_name = $term_obj->name;
                $term_slug = $term_obj->slug;
                echo '<option value="'.$term_slug.'">'.$term_name.'</option>';
            }
            echo '</select>
            </div>';
        }
    }
}

然后当你想在产品添加到购物车时将选定的“颜色”属性“值”传递给购物车项目,并最终将其显示在购物车、结帐、订单和电子邮件通知中,这里是你需要的代码:

// Save the custom product custom field data in Cart item
add_action( 'woocommerce_add_cart_item_data', 'save_in_cart_my_custom_product_field', 10, 2 );
function save_in_cart_my_custom_product_field( $cart_item_data, $product_id ) {
    if( isset( $_POST['custom_pa_color'] ) ) {
        $cart_item_data[ 'custom_pa_color' ] = $_POST['custom_pa_color'];

        // When add to cart action make an unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'custom_data', $_POST['custom_pa_color'] );
    }
    return $cart_item_data;
}

// Render the custom product custom field in cart and checkout
add_filter( 'woocommerce_get_item_data', 'render_custom_field_meta_on_cart_and_checkout', 10, 2 );
function render_custom_field_meta_on_cart_and_checkout( $cart_data, $cart_item ) {

    $custom_items = array();

    if( !empty( $cart_data ) )
        $custom_items = $cart_data;

    if( $custom_field_value = $cart_item['custom_pa_color'] )
        $custom_items[] = array(
            'name'      => __( 'Color', 'woocommerce' ),
            'value'     => $custom_field_value,
            'display'   => $custom_field_value,
        );

    return $custom_items;
}

// Add the the product custom field as item meta data in the order + email notifications
add_action( 'woocommerce_add_order_item_meta', 'tshirt_order_meta_handler', 10, 3 );
function tshirt_order_meta_handler( $item_id, $cart_item, $cart_item_key ) {
    $custom_field_value = $cart_item['custom_pa_color'];

    // We add the custom field value as an attribute for this product
    if( ! empty($custom_field_value) )
        wc_update_order_item_meta( $item_id, 'pa_color', $custom_field_value );
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

此代码适用于 WooCommerce 2.5 至 3.0+ 版本

In your template

您必须删除选择器代码并重新插入原始挂钩:

<form id="add-product-form" class="add-product-form form-horizontal" method="post" action="">
<?php 
     // Here we re-insert the original hook
     do_action('woocommerce_before_add_to_cart_button');
?>
    <div class="form-group quantity-area">
        <label class="col-sm-3 control-label">Qty.</label>
        <input name="quantity" id="quantity" maxlength="2" class="col-sm-9 quantity-input" required="" type="text">
    </div>
    <button id="submit-to-cart" value="Add to Cart" class="btn btn-a2c submit" name="submit" type="submit"><i class="fa fa-plus" aria-hidden="true"></i> Add to Cart</button>
</form>

相关回答:Saving a product custom field and displaying it in cart page