我的环境是:
- WordPress 4.7.4
- Debian Linux 8
- WooCommerce 3.0.5
- 256M内存
我尝试过多种解决方案,包括:
然而,我仍然没有得到正确的结果,而且在这个项目的这一点上,时间至关重要。精度,我对所有属性值都有相同的价格...
我使用自定义表单创建了一个自定义的single-product.php tempalte:
<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方法并按预期添加到购物车。
然而:
我的问题:
那么如何将自定义属性选择值添加到购物车,结帐,订单和电子邮件通知?
我知道我可以采用可变产品方法,但即使在256M内存中,“变量产品”区域中的“变体”菜单也会不断旋转,因此我无法进入此区域添加变体。
答案 0 :(得分:2)
最好使用作为女佣的原始钩子
do_action('woocommerce_before_add_to_cart_button');
来通过一些自定义的钩子函数在其中注入代码。>不是覆盖你的模板single-product.php。 p>
据我了解,您不需要使用可变产品。您希望使用单个产品来显示自定义选择器字段,您可以在其中设置现有的&#34;颜色&#34;属性与此产品的选定值。
这是钩子函数:
// 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>';
}
}
}
然后,因为你想传递购物车项目所选的&#34;颜色&#34;属性&#34;价值&#34;当产品被添加到购物车并最终显示在购物车中时,结账,订购和电子邮件通知就是您需要的代码:
// 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 +
在您的模板中
您必须删除选择器代码并重新插入oringinal hook:
<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