猿问

基于 WooCommerce 购物车中某个类别的商品数量计数的折扣

我有一类产品的价格都是 15 美元。当用户从该类别购买 10 到 20 件产品时,他们应该获得 10 美元的折扣价。当用户购买 20+ 时,价格再次变为 5 美元。不能为用户分配自定义角色(如批发商)。我根据另一个问题的 LoicTheAztec 代码松散地创建了代码,并添加了我自己的修改和代码。看起来应该可以。我没有收到任何错误,但它不起作用。


add_action('woocommerce_before_cart', 'check_product_category_in_cart');


function check_product_category_in_cart() {

    // HERE set your product categories in the array (can be IDs, slugs or names)

    $categories = array('surfacing-adhesives');

    $found      = false; // Initializing


    $count = 0;


    // Loop through cart items      

    foreach ( WC()->cart->get_cart() as $cart_item ) {

        // If product categories is found

        if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {

            $count++;

        }

    }


    if (!current_user_can('wholesaler')) {

        // Discounts

        if ($count > 10 && $count < 20) {

            // Drop the per item price

            $price = 10;

        } else if ($count > 20) {

            // Drop the per item price

            $price = 5;

        } else {

            // Did not qualify for volume discount

        }

    }

}


皈依舞
浏览 147回答 1
1回答

杨__羊羊

您没有使用正确的钩子,并且缺少一些东西。尝试以下操作:add_action( 'woocommerce_before_calculate_totals', 'discounted_cart_item_price', 20, 1 );function discounted_cart_item_price( $cart ){&nbsp; &nbsp; // Not for wholesaler user role&nbsp; &nbsp; if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) || current_user_can('wholesaler') )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; // Required since Woocommerce version 3.2 for cart items properties changes&nbsp; &nbsp; if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; // HERE set your product categories in the array (can be IDs, slugs or names)&nbsp; &nbsp; $categories = array('surfacing-adhesives');&nbsp; &nbsp; $categories = array('t-shirts');&nbsp; &nbsp; // Initializing&nbsp; &nbsp; $found = false;&nbsp; &nbsp; $count = 0;&nbsp; &nbsp; // 1st Loop: get category items count&nbsp;&nbsp;&nbsp; &nbsp; foreach ( WC()->cart->get_cart() as $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; // If product categories is found&nbsp; &nbsp; &nbsp; &nbsp; if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $count += $cart_item['quantity'];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Applying discount&nbsp; &nbsp; if ( $count >= 10 ) {&nbsp; &nbsp; &nbsp; &nbsp; // Discount calculation (Drop the per item qty price)&nbsp; &nbsp; &nbsp; &nbsp; $price = $count >= 20 ? 5 : 10;&nbsp; &nbsp; &nbsp; &nbsp; // 2nd Loop: Set discounted price&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; foreach ( WC()->cart->get_cart() as $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If product categories is found&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cart_item['data']->set_price( $price );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}代码进入您的活动子主题(或活动主题)的 functions.php 文件。测试和工作。
随时随地看视频慕课网APP
我要回答