0

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

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;
}
}

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

4

1 回答 1

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;
    }
}
于 2014-02-17T13:26:32.423 回答