如何根據購物車數量和產品屬性在WooCommerce中對變數和單一產品應用折扣?
P粉592085423
P粉592085423 2023-07-23 13:26:53

我正在嘗試根據購物車中具有特定產品屬性的產品數量來應用百分比折扣。

更確切地說,我的目標是在購買至少6個具有屬性"flaske"的產品時應用15%的折扣。

我已經成功地在設定了變數屬性的產品上實現了這一目標,但是我似乎無法針對單一/簡單產品進行操作。

這是我迄今為止的程式碼(借用自WooCommerce的數量和價格條件)

// 根据购物车中的产品数量和属性进行折扣。
add_action( 'woocommerce_cart_calculate_fees','wc_cart_item_quantity_discount' );
function wc_cart_item_quantity_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // 初始化变量。
    $min_item_amount = 6; // 最小数量。
    $discount = $items_count = $percent = $items_subtotal = 0;
    $taxonomy   = 'pa_variant'; // 分类
    $term_slugs = array('flaske'); // 术语
    // 遍历购物车中的物品
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
      // 遍历变体
      foreach( $cart_item['variation'] as $attribute => $term_slug ) {
        // 只计算具有属性且数量超过6的物品。
        if( $cart_item['data']->get_price() >= $min_item_amount && $attribute === 'attribute_'.$taxonomy && in_array( $term_slug, $term_slugs ) ) {
            $items_count += $cart_item['quantity'];
            $items_subtotal += $cart_item['line_subtotal'];
        }
      }
    }
    // 条件百分比。
    if ($items_count >= 6 ) {
        $percent = 15;
    }
    // 折扣(应税)。
    if( $items_count > 0 ) {
        // 计算
        $discount -= ($items_subtotal / 100) * $percent;
        $cart->add_fee( __( "Mix & Match rabat - $percent%", "woocommerce" ), $discount, true);
    }
}

我的當前程式碼對於可變產品(變體)非常有效,但似乎對於單一產品並沒有影響,即使我給單一產品賦予了與可變產品相同的屬性。

我懷疑這與foreach循環有關,即foreach( $cart_item['variation'] as $attribute => $term_slug )。

如何使其在一般情況下工作,以便也適用於具有相同屬性「flaske」的單一/簡單產品?

非常感謝您的幫助和建議。

P粉592085423
P粉592085423

全部回覆(1)
P粉323374878

根據數量為每個商品提供Woocommerce百分比折扣

在Woocommerce中排除具有2個特定屬性術語的變體,不可使用優惠券

add_action( 'woocommerce_cart_calculate_fees','wc_cart_item_quantity_discount' );
function wc_cart_item_quantity_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // 初始化变量。
    $min_item_amount = 6; // 最小数量
    $discount = $items_count = $percent = $items_subtotal = 0;
    $taxonomy   = 'pa_variant'; // 分类
    $term_slugs = array('flaske'); 
    // 遍历购物车中的物品
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        $price   = $product->get_price();

        // 产品变种
        if( $product->is_type('variation') ) {
            // 遍历变种属性
            foreach ($cart_item['variation'] as $attribute => $term_slug) {
                // 只计算具有属性且数量大于6的物品。
                if ($price >= $min_item_amount && $attribute === 'attribute_' . $taxonomy && in_array($term_slug, $term_slugs)) {
                    $items_count += $cart_item['quantity'];
                    $items_subtotal += $cart_item['line_subtotal'];
                }
            }
        } 
        // 简单产品
        elseif ( $product->is_type('simple') ) {
            $attributes = $product->get_attributes();

            if( ! empty($attributes) && array_key_exists($taxonomy, $attributes) ) {
                $terms = (array) $attributes[$taxonomy]->get_terms(); // array of WP_Term objects
                $slugs = array_map(function($term) { return $term->slug; }, $terms); // Extract only the term slugs

                if ($price >= $min_item_amount && count( array_intersect($slugs, $term_slugs) ) > 0 ) {
                    $items_count += $cart_item['quantity'];
                    $items_subtotal += $cart_item['line_subtotal'];
                }
            }
        }
    }
    // 条件百分比
    if ($items_count >= 6) {
        $percent = 15;
    }
    // 折扣(应税)。
    if ($items_count > 0) {
        // 计算
        $discount -= ($items_subtotal / 100) * $percent;
        $cart->add_fee(__("Mix & Match rabat - $percent%", "woocommerce"), $discount, true);
    }
}

這應該有用

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!