2017-01-14 36 views
1

我处于我的Woocommerce网站的独特情况。根据产品尺寸和类别定制费用

我需要添加包装费,这与手续费有点相似。

不幸的是,它并不像每个订单增加5.00美元的手续费那么简单。

由于我根据他们的尺寸(宽x高)销售木制品,因此包装费根据物品的总平方尺寸计算。而且它们也可以根据项目的类别而有所不同。

我已经做了大量的研究,但找不到处理这种情况的插件。

要增加复杂性,需要创建一个整个表格。例如,如果来自一个类别的项目的总平方尺寸介于​​1-10之间,则包装费用将为$ 10。如果总平方英尺在11-20之间,那么它将是$ 20

我该如何做到这一点?

由于

+1

请仔细阅读https://stackoverflow.com/help/on-topic。如上所述的这个问题不适合于stackoverflow的Q&A格式。 –

+0

这似乎是您可能想聘请开发人员为您创建的内容。 – helgatheviking

回答

1

更新:添加WooCommerce 3+兼容性

这是可能的,并且容易woocommerce_cart_calculate_feesadd_fee()方法。以下是简单产品的简单使用示例,包含2个类别,并基于每个购物车的产品尺寸测量计算。其他产品类型也有可能。

下面是示例代码(这将需要定制自己的情况下):

add_action('woocommerce_cart_calculate_fees','custom_applied_fee', 10, 1); 
function custom_applied_fee($cart_object) { 

    if (is_admin() && ! defined('DOING_AJAX')) 
     return; 

    // Set HERE your categories (can be an ID, a slug or the name… or an array of this) 
    $category1 = 'plain'; 
    $category2 = 'plywood'; 

    // variables initialisation 
    $fee = 0; 
    $coef = 1; 

    // Iterating through each cart item 
    foreach($cart_object->get_cart() as $cart_item){ 
     $product_id = version_compare(WC_VERSION, '3.0', '<') ? $cart_item['data']->id : $cart_item['data']->get_id(); 
     $product = $cart_item['data']; // Get the product object 

     // Get the dimentions of the product 
     $height = $product->get_height(); 
     $width = $product->get_width(); 
     // $length = $product->get_length(); 

     // Initialising variables (in the loop) 
     $cat1 = false; $cat2 = false; 

     // Detecting the product category and defining the category coeficient to change price (for example) 
     // Set here for each category the modification calculation rules… 
     if(has_term($category1, 'product_cat', $cart_item['product_id'])) 
      $coef = 1.15; 
     if(has_term($category2, 'product_cat', $cart_item['product_id'])) 
      $coef = 1.3; 

     // ## CALCULATIONS ## (Make here your conditional calculations) 
     $dimention = $height * $with; 
     if($dimention <= 10){ 
      $fee += 10 * $coef; 
     } elseif($dimention > 10 && $dimention <= 20){ 
      $fee += 20 * $coef; 
     } elseif($dimention > 20){ 
      $fee += 30 * $coef; 
     } 
    } 

    // Set here the displayed fee text 
    $fee_text = __('Packaging fee', 'woocommerce'); 

    // Adding the fee 
    if ($fee > 0) 
     WC()->cart->add_fee($fee_text, $fee, false); 
     // Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false) 

} 

你将不得不与相关的变化计算为每个类别设定自己的类别。考虑到购物车中的每个物品,您将得到一个非详细的一般输出费用。

代码发送到您活动的子主题(或主题)的function.php文件中。或者也可以在任何插件php文件中使用。

代码已经过测试并且功能完整。

相关答案: