Woocommerce的数量和价格条件

huangapple go评论62阅读模式
英文:

Condition for Quantity and Price for Woocommerce

问题

我有代码来检查购物车中商品的数量然后根据数量进行折扣我如何对价格低于500的商品进行额外检查对于这些商品折扣应为0)。

以下代码基于 https://stackoverflow.com/questions/44856874/woocommerce-cart-quantity-based-discount/44857080#44857080 的答案:
英文:

Ii've code for checking quantity of goods in cart, and then it makes a discount based on quantity. How can I make additional check on price for goods below 500 (for these goods, discount should be 0).

The following code is based on https://stackoverflow.com/questions/44856874/woocommerce-cart-quantity-based-discount/44857080#44857080 answer:

add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );

function wc_cart_quantity_discount( $cart_object ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    ## -------------- DEFINIG VARIABLES ------------- ##
    $discount = 0;
    $cart_item_count = $cart_object->get_cart_contents_count();
    $cart_total_excl_tax = $cart_object->subtotal_ex_tax;
    /*foreach( $cart_object->get_cart() as $cart_item_key => $cart_item ) {
		$cart_item = $cart_object->get_cart_contents_count();
	}*/
	
    
    // Assign each product's price to its cart item key (to be used again later)
    
    ## ----------- CONDITIONAL PERCENTAGE ----------- ##

        if( $cart_item_count <= 1  )
            $percent = 5;
        elseif( $cart_item_count <= 2  )
            $percent = 10;
        elseif( $cart_item_count <= 3  )
            $percent = 15;
        elseif( $cart_item_count <= 4  )
            $percent = 20;
        elseif( $cart_item_count >= 5  )
            $percent = 25;
        else {
            $percent = 0;
        }

    ## ------------------ CALCULATION ---------------- ##
    $discount -= ($cart_total_excl_tax / 100) * $percent;

    ## ----  APPLYING CALCULATED DISCOUNT TAXABLE ---- ##
    if( $percent > 0 )
        $cart_object->add_fee( __( "Quantity discount $percent%", "woocommerce" ), $discount, true);
}

Should be checking for quantity and price below, for example, 500 (the discount should be 0). For another goods with condition: price is higher 500 and the corresponding quantity, its own discount:
1 - 5%
2 - 10%
3 - 15%
etc.

答案1

得分: 1

以下是您提供的代码的翻译部分:

*已更新*

以下代码仅会计算超过500的项目,以根据该项目数量应用百分比折扣:
```php
add_action( 'woocommerce_cart_calculate_fees','wc_cart_intem_quantity_discount' );
function wc_cart_intem_quantity_discount( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // 初始化变量
    $min_item_amount = 500;
    $discount = $items_count = $percent = $items_subtotal = 0;

    // 循环遍历购物车项目
    foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
        // 仅计算超过500的项目
        if( $cart_item['data']->get_price() >= $min_item_amount ) {
            $items_count += $cart_item['quantity'];
            $items_subtotal += $cart_item['line_subtotal'];
        }
    }

    // 条件百分比
    if ($items_count == 1) {
        $percent = 5;
    } elseif ($items_count == 2) {
        $percent = 10;
    } elseif ($items_count == 3) {
        $percent = 15;
    } elseif ($items_count == 4) {
        $percent = 20;
    } elseif ($items_count >= 5) {
        $percent = 25;
    }

    // 折扣(可税)
    if( $items_count > 0 ) {
        // 计算
        $discount -= ($items_subtotal / 100) * $percent;

        $cart->add_fee( __( "数量折扣 $percent%", "woocommerce" ), $discount, true);
    }
}

基于以下网址的代码:https://stackoverflow.com/questions/44856874/woocommerce-cart-quantity-based-discount/44857080#44857080


<details>
<summary>英文:</summary>

*Updated*

The following code will only count items that are above 500, to apply a percentage discount based on that item count:
```php
add_action( &#39;woocommerce_cart_calculate_fees&#39;,&#39;wc_cart_intem_quantity_discount&#39; );
function wc_cart_intem_quantity_discount( $cart ) {
    if ( is_admin() &amp;&amp; ! defined( &#39;DOING_AJAX&#39; ) )
        return;

    // INITIALIZING VARIABLES
    $min_item_amount = 500;
    $discount = $items_count = $percent = $items_subtotal = 0;

    // Loop through cart items
    foreach( $cart-&gt;get_cart() as $cart_item_key =&gt; $cart_item ) {
        // Only counting items that are above 500
        if( $cart_item[&#39;data&#39;]-&gt;get_price() &gt;= $min_item_amount ) {
            $items_count += $cart_item[&#39;quantity&#39;];
            $items_subtotal += $cart_item[&#39;line_subtotal&#39;];
        }
    }

    // CONDITIONAL PERCENTAGE
    if ($items_count == 1) {
        $percent = 5;
    } elseif ($items_count == 2) {
        $percent = 10;
    } elseif ($items_count == 3) {
        $percent = 15;
    } elseif ($items_count == 4) {
        $percent = 20;
    } elseif ($items_count &gt;= 5) {
        $percent = 25;
    }

    // DISCOUNT (TAXABLE)
    if( $items_count &gt; 0 ) {
        // Calculation
        $discount -= ($items_subtotal / 100) * $percent;

        $cart-&gt;add_fee( __( &quot;Quantity discount $percent%&quot;, &quot;woocommerce&quot; ), $discount, true);
    }
}

Code based on: https://stackoverflow.com/questions/44856874/woocommerce-cart-quantity-based-discount/44857080#44857080

huangapple
  • 本文由 发表于 2023年6月29日 19:29:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定