Php 吴宇森商务促销折扣:买10送1

Php 吴宇森商务促销折扣:买10送1,php,wordpress,woocommerce,cart,discount,Php,Wordpress,Woocommerce,Cart,Discount,我正在尝试为三种可变产品(464、465和466)设置一个特定的折扣。如果一个顾客买了十种产品,他们可以免费得到一种 根据答案代码,我得出了以下代码: add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_11th_at_100', 10, 1 ); function add_custom_discount_11th_at_100( $wc_cart ){ if ( is_admin() &&

我正在尝试为三种可变产品(464、465和466)设置一个特定的折扣。如果一个顾客买了十种产品,他们可以免费得到一种

根据答案代码,我得出了以下代码:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_11th_at_100', 10, 1 );
function add_custom_discount_11th_at_100( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;
    $items_prices = array();

    // Set HERE your targeted variable product ID
    $targeted_product_id = 464;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $qty = intval( $cart_item['quantity'] );
            for( $i = 0; $i < $qty; $i++ )
                $items_prices[] = floatval( $cart_item['data']->get_price());
        }
    }
    $count_items_prices = count($items_prices);
    if( $count_items_prices > 10 ) foreach( $items_prices as $key => $price )
        if( $key % 11 == 1 ) $discount -= number_format($price / 1, 11 );

    if( $discount != 0 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("Buy 10 Get 1 Free"), 'notice');

        // The discount
        $wc_cart->add_fee( 'Buy 10 Get 1 Free', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}
add_action('woocommerce_cart_计算_费用','add_定制_折扣_第11_在_100',10,1处);
功能添加\u自定义\u折扣\u第11次\u在\u 100($wc\u购物车){
if(is_admin()&&!defined('DOING_AJAX'))返回;
$折扣=0;
$items_prices=array();
//在此设置目标变量产品ID
$targeted_product_id=464;
foreach($wc\u cart->get\u cart()作为$key=>$cart\u项目){
if($cart\u item['product\u id']==$targeted\u product\u id){
$qty=intval($cart_项目['quantity']);
对于($i=0;$i<$qty;$i++)
$items_prices[]=floatval($cart_item['data']->get_prices());
}
}
$count\u items\u prices=计数($items\u prices);
如果($count\u items\u prices>10)foreach($key=>$prices的items\u prices)
如果($key%11==1)$discount-=number\u格式($price/1,11);
如果($折扣!=0){
//显示自定义通知(可选)
wc_清除通知();
wc添加通知(“买10送1”),“通知”);
//折扣
$wc_购物车->添加费用('Buy 10 Get 1 Free',$折扣,true);
#注意:add_fee()方法中的最后一个参数与是否将税款应用于折扣有关(true或false)
}
}

但它只适用于一个产品ID。如何将其扩展为适用于三个产品ID?

要使其适用于多个产品,您可以在\u array()中使用
php函数,如下所示:

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

    // Set HERE your targeted variable products IDs
    $targeted_product_ids = array( 464, 465, 466 );

    $each_n_items = 10; // Number of items required to get a free one
    $discount = 0; // Initializing
    $items_prices = array(); // Initializing 

    foreach ( $cart->get_cart() as $cart_item ) {
        if( in_array( $cart_item['product_id'], $targeted_product_ids ) ) {
            $qty = intval( $cart_item['quantity'] );

            for ( $i = 0; $i < $qty; $i++ ) {
                $items_prices[] = floatval( $cart_item['data']->get_price() );
            }
        }
    }
    $count_items_prices = count($items_prices);

    if ( $count_items_prices > $each_n_items ) {
        foreach ( $items_prices as $key => $price ) {
            if ( $key % ($each_n_items + 1) == 1 ) {
                $discount += number_format($price, 2 );
            }
        }
    }

    if ( $discount > 0 ) {
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("Buy 10 Get 1 Free"), 'notice');

        // The discount
        $cart->add_fee( __("Buy 10 Get 1 Free"), -$discount, true  );
    }
}
add\u action('woocommerce\u cart\u calculate\u fees','buy\u ten\u get\u one\u free',10,1);
功能买十送一($cart){
if(is_admin()&&!defined('DOING_AJAX'))返回;
//在此设置您的目标可变产品ID
$targeted_product_id=数组(464465466);
$each_n_items=10;//获得免费商品所需的商品数量
$discount=0;//正在初始化
$items_prices=array();//正在初始化
foreach($cart->get\u cart()作为$cart\u项目){
if(在数组中($cart\u item['product\u id',$targeted\u product\u id)){
$qty=intval($cart_项目['quantity']);
对于($i=0;$i<$qty;$i++){
$items_prices[]=floatval($cart_item['data']->get_prices());
}
}
}
$count\u items\u prices=计数($items\u prices);
如果($count\u items\u prices>$each\u n\u items){
foreach($items\u价格为$key=>$price){
如果($key%($each_n_items+1)=1){
$折扣+=数字格式($price,2);
}
}
}
如果($折扣>0){
//显示自定义通知(可选)
wc_清除通知();
wc添加通知(“买10送1”),“通知”);
//折扣
$cart->add_-fee(uuuu(“买10送1”),-$折扣,true);
}
}

代码放在活动子主题(或活动主题)的function.php文件中,经过测试并正常工作。

首先,当您使用现有答案代码生成自己的代码时,请在您的问题中添加链接……您的代码中存在什么问题?问题是什么?我调整了在线找到的代码片段以实现我的目标。但它只适用于一个产品ID。如何将它扩展到适用于三个产品ID?我试着使用数组,但不起作用。。。