gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:03:09 34 4
gpt4 key购买 nike

我正在进行一个项目,需要在特定的促销月份将免费商品添加到购物车。因此,我需要使产品值(value)为 0.00,并在特定的促销月自动将其添加到购物车。到目前为止,我已经在添加到购物车总数达到一定数量时自动添加了产品

/*
* Automatically adding the product to the cart when cart total amount reach to $500.
*/

function aapc_add_product_to_cart() {
global $woocommerce;

$cart_total = 500;

if ( $woocommerce->cart->total >= $cart_total ) {
if ( ! is_admin() ) {
$free_product_id = 12989; // Product Id of the free product which will get added to cart
$found = false;

//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $free_product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $free_product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $free_product_id );
}
}
}
}

add_action( 'template_redirect', 'aapc_add_product_to_cart' );
...

最佳答案

请尝试以下方法(处理购物车页面以及月份范围中的动态变化)

注意阈值的总金额只能是购物车商品的总金额。

代码:

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;
}

代码进入事件子主题(或事件主题)的 functions.php 文件。它应该有效。

相关:Add or remove specific cart Item based on WooCommerce cart total

关于php - 根据 WooCommerce 购物车总数和月份范围添加或删除免费产品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66626228/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com