2014-02-17 83 views
0

我的数据库中的每个产品都有一个“pa_producator”属性,我想要以下东西: 当pa_producator等于“Bosch”时,仅对该产品应用10%的折扣,而不是整车。这可能吗?到目前为止,我只有以下代码:Woocommerce折扣如果有某些属性

add_action('woocommerce_before_cart_table', 'discount_producer'); 
function discount_producer() { 
global $woocommerce; 
$test = $woocommerce->cart->get_cart(); 
foreach($test as $product) { 
    $test = get_the_terms($product['product_id'], 'pa_producator'); 
    $producator = $test[0]->name; 
} 
} 

我该如何为这些产品申请折扣?

回答

0

在任何输出之前更新值更好。所以它适用于使用购物车的所有地方。

add_action('woocommerce_cart_loaded_from_session', 'check_cart_items', 99, 1); 

搜索此操作。这是在购物车被创建之后调用的。然后,您可以调整购物车中每件商品的价格。你已经在尝试的方式也一样。

public function check_cart_items($cart) { 
    foreach ($cart->cart_contents as $key => $product) { 
     // find attribute for current product 
     // .. do stuff 
     $cart->cart_contents[ $key ]['data']->price = $new_price; 
     $cart->cart_contents[ $key ]['data']->regular_price = $price; 
     $cart->cart_contents[ $key ]['data']->sale_price = $price; 
    } 
}