【问题标题】:Woocommerce discount if has certain attributeWoocommerce 折扣如果具有某些属性
【发布时间】:2014-02-17 09:20:19
【问题描述】:

我数据库中的每个产品都有一个“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;
}
}

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

【问题讨论】:

    标签: php wordpress woocommerce commerce


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 2014-02-12
      • 2016-10-07
      • 1970-01-01
      • 2019-08-28
      • 2018-03-08
      • 1970-01-01
      • 2021-11-11
      • 2014-02-09
      相关资源
      最近更新 更多