2017-05-03 66 views
1

我的环境是:
- WordPress的4.7.4
- Debian的Linux的8
- WooCommerce 3.0.5
- 256M内存
添加自定义属性选择的值购物车,结账,订单和电子邮件通知

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

不过,我仍然没有得到正确的结果和时间是最重要的,在这一点上这个项目的。精密的,我也有同样的价格为所有属性值...

我创建了一个自定义的单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方法,并添加到购物车如预期。

但是:

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

我的问题:

所以,我怎么能自定义属性选择的值添加到购物车,结账,订单和电子邮件通知?

我知道我可以把不同产品的做法,但即使在256M内存,在不同产品领域的变化不断菜单旋转,所以我可以永远到不了这个区域添加的变化。

+0

“我知道我可以采取变量产品的方法,但即使在256M的内存,变量产品领域的变化菜单不断旋转”你检查你的控制台?这听起来像你可能有一个错误,阻止了Javascript的完成。在尝试编写自己的可变产品替代品之前,我会尝试调试它。切换到默认主题。禁用插件。启用'WP_DEBUG_LOG' ...整个调试工作。 – helgatheviking

+0

我发现这个问题,无论如何,仍然有一个问题将属性带到购物车。 – mayvn

+0

问题是什么?您的主题是否覆盖任何购物车模板?他们过时了吗?因为我认为变体属性默认显示在购物车中。 – helgatheviking

回答

2

而是覆盖您的模板单product.php的,最好是使用原来的钩do_action('woocommerce_before_add_to_cart_button');是女仆通过一些自定义挂钩函数注入它的代码。

据我所知,您不需要使用可变产品。你想用那将会显示您设置一个现有的“颜色”属性与该产品所选择的值自定义选择场的单品。

下面是挂钩的函数:

// 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 +

在模板

你将不得不删除选择器代码,并重新插入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

+0

多么好的信息答案。谢谢Loic! – mayvn

+0

关于答案的荣誉。它对我来说仍然看起来像一个可变产品,只有一个单一的颜色属性,但在这里已经很晚了,我不能再想直了。 – helgatheviking

相关问题