Php 根据WooCommerce购物车总数和月份范围添加或删除免费产品

Php 根据WooCommerce购物车总数和月份范围添加或删除免费产品,php,wordpress,woocommerce,cart,hook-woocommerce,Php,Wordpress,Woocommerce,Cart,Hook Woocommerce,我在一个项目,需要添加到购物车在特定的促销月免费项目。因此,我需要使产品价值为0.00,并将其自动添加到特定促销月份的购物车中。到目前为止,我已经添加产品时,自动添加到购物车总数达到一定数额 /* *当购物车总金额达到$500时,自动将产品添加到购物车。 */ 功能aapc_将_产品_添加到_购物车(){ 全球商业; $cart_总计=500; 如果($woocommerce->cart->total>=$cart\u total){ 如果(!is_admin()){ $free\u produ

我在一个项目,需要添加到购物车在特定的促销月免费项目。因此,我需要使产品价值为0.00,并将其自动添加到特定促销月份的购物车中。到目前为止,我已经添加产品时,自动添加到购物车总数达到一定数额

/*
*当购物车总金额达到$500时,自动将产品添加到购物车。
*/
功能aapc_将_产品_添加到_购物车(){
全球商业;
$cart_总计=500;
如果($woocommerce->cart->total>=$cart\u total){
如果(!is_admin()){
$free\u product\u id=12989;//将添加到购物车的免费产品的产品id
$found=false;
//检查产品是否已在购物车中
如果(sizeof(WC()->cart->get_cart())>0){
foreach(WC()->cart->get_cart()作为$cart\u item\u key=>$value){
$_product=$values['data'];
如果($\u产品->获取\u id()==$free\u产品\u id)
$found=true;
}
//如果未找到产品,请添加它
如果(!$found)
WC()->cart->add_to_cart($free_product_id);
}否则{
//如果购物车中没有产品,请添加它
WC()->cart->add_to_cart($free_product_id);
}        
}
}        
}
添加操作(“模板重定向”、“aapc将产品添加到购物车”);
...
请尝试以下方法(处理购物车页面和月份范围中的动态更改)

注意:阈值的总金额只能是购物车项目总数

守则:

function is_free_product_allowed_for_month() {
    // Define allowed months in the array (values from 1 to 12)
    $allowed_months   = array('3', '7', '8');

    return in_array( date('n'), $allowed_months );
}


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

    // Check if we are in an allowed month
    if ( ! is_free_product_allowed_for_month() )
        return;

    $free_product_id  = 339; // ID of the free product
    $threshold_amount = 200; // The threshold amount for cart subtotal
    $cart_items_total = 0; // Initializing

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
        // Check if the free product is in cart
        if ( in_array( $free_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
            $cart_item['data']->set_price(0); // Set price to Zero
            $free_item_key = $cart_item_key;

        }
        // Get cart subtotal incl. tax from items (with discounts if any)
        $cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];
    }

    // If Cart total is up to the defined amount and if the free products is not in cart, we add it.
    if ( $cart_items_total >= $threshold_amount && ! isset($free_item_key) ) {
        $cart->add_to_cart( $free_product_id );
    }
    // If cart total is below the defined amount and free product is in cart, we remove it.
    elseif ( $cart_items_total < $threshold_amount && isset($free_item_key) ) {
        $cart->remove_cart_item( $free_item_key );
    }
}


// For minicart displayed free product price
add_filter( 'woocommerce_cart_item_price', 'free_product_cart_item_price', 10, 3 );
function free_product_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
    // Check if we are in an allowed month
    if ( ! is_free_product_allowed_for_month() )
        return $price_html;

    $free_product_id  = 339; // ID of the free product

    if ( in_array( $free_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
        return wc_price( 0 );
    }
    return $price_html;
}
功能是\u免费的\u产品\u允许的\u月(){
//在数组中定义允许的月份(值从1到12)
$allowed_months=数组('3','7','8');
以数组形式返回(日期('n'),$allowed\u个月);
}
添加操作(“计算总计前的woocommerce”、“添加、删除、免费产品”);
功能添加\删除\免费\产品($cart){
if(定义了('DOING'uajax'))
返回;
//检查我们是否在允许的月份内
如果(!是否允许在月份()内提供免费产品)
返回;
$free\u product\u id=339;//免费产品的id
$threshold\u amount=200;//购物车小计的阈值金额
$cart\u items\u total=0;//正在初始化
//循环浏览购物车项目
foreach($cart->get\u cart()作为$cart\u item\u key=>$cart\u item){
//检查免费产品是否在购物车中
if(在数组中($free\u product\u id,数组($cart\u item['product\u id'],$cart\u item['variation\u id'])){
$cart_item['data']->set_price(0);//将价格设置为零
$free\u item\u key=$cart\u item\u key;
}
//获取购物车小计(包括来自项目的税费)(如果有折扣)
$cart_items_total+=$cart_item['line_total']+$cart_item['line_tax'];
}
//如果购物车总额达到定义的金额,并且如果免费产品不在购物车中,我们将其添加。
如果($cart\u items\u total>=$threshold\u amount&&!isset($free\u item\u key)){
$cart->add_to_cart($free_product_id);
}
//如果购物车总额低于定义的金额,并且购物车中有免费产品,我们将其删除。
elseif($cart\u items\u total<$threshold\u amount&&isset($free\u item\u key)){
$cart->remove_cart_item($free_item_key);
}
}
//对于微型购物车,显示免费产品价格
添加过滤器('woocommerce\u cart\u item\u price','free\u product\u cart\u item\u price',10,3);
功能免费的商品购物车商品价格($price\u html、$cart\u item、$cart\u item\u key){
//检查我们是否在允许的月份内
如果(!是否允许在月份()内提供免费产品)
返回$price_html;
$free\u product\u id=339;//免费产品的id
if(在数组中($free\u product\u id,数组($cart\u item['product\u id'],$cart\u item['variation\u id'])){
返回wc_价格(0);
}
返回$price_html;
}
代码进入活动子主题(或活动主题)的functions.php文件。它应该会起作用


相关:

谢谢@LoicTheAztec,你刚刚救了我一天,效果非常好。当客户向购物车添加可变产品时,我还需要添加一个促销弹出窗口,这可能吗?这其中的片段是什么?非常感谢您的热情帮助…@LoicTheAztec感谢您让我知道,我已经接受了答案,并对其进行了投票。谢谢