大佬教程收集整理的这篇文章主要介绍了根据 WooCommerce 购物车总数和月份范围添加或删除免费产品,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个项目,需要在特定的促销月份将免费商品添加到购物车。因此,我需要将产品值设为 0.00 并在特定促销月份将其自动添加到购物车。到目前为止,我已经在添加到购物车总数达到一定数量时自动添加产品
/*
* automatically adding the product to the cart when cart @R_677_10586@l amount reach to $500.
*/
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_@R_677_10586@l = 500;
if ( $woocommerce->cart->@R_677_10586@l >= $cart_@R_677_10586@l ) {
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_@R_677_10586@ls','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 sub@R_677_10586@l
$cart_items_@R_677_10586@l = 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 sub@R_677_10586@l incl. tax from items (with discounts if any)
$cart_items_@R_677_10586@l += $cart_item['line_@R_677_10586@l'] + $cart_item['line_tax'];
}
// If Cart @R_677_10586@l is up to the defined amount and if the free products is not in cart,we add it.
if ( $cart_items_@R_677_10586@l >= $threshold_amount && ! isset($free_item_key) ) {
$cart->add_to_cart( $free_product_id );
}
// If cart @R_677_10586@l is below the defined amount and free product is in cart,we remove it.
elseif ( $cart_items_@R_677_10586@l < $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,$cart_item['variation_id'] ) ) ) {
return wc_price( 0 );
}
return $price_html;
}
代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。
相关:Add or remove specific cart Item based on WooCommerce cart @R_677_10586@l
以上是大佬教程为你收集整理的根据 WooCommerce 购物车总数和月份范围添加或删除免费产品全部内容,希望文章能够帮你解决根据 WooCommerce 购物车总数和月份范围添加或删除免费产品所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。