Woocommerce 从购物车商品数量中删除 free_gift
Woocommerce remove free_gift from cart item count
我有一个功能可以将购物车中商品的最大订购数量设置为 16。因此用户无法结帐超过 16 件商品。
我也是运行一个插件,在添加优惠券时会在$cart_item array
中添加一个free_gift
键。
问题是,当用户添加 16 件商品时 + free_gift
= 总数为 17 件,这会阻止结帐。
如何删除 free_gift
添加到购物车的商品数量?
示例:
- 让 Woocommerce 购物车认为 16 件商品 + 1
free_gift
= 16 件商品
- 让 Woocommerce 购物车认为 12 件商品 + 1
free_gift
= 12 件商品
到目前为止,我的代码允许添加 free_gifts 超过最大 16 个限制,但不应用 16 个项目的最大规则:
// Set a maximum number of products requirement before checking out
add_action( 'woocommerce_check_cart_items', 'spyr_set_max_num_products' );
function spyr_set_max_num_products() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
$cart_num_products = 0;
foreach ( WC()->cart->cart_contents as $cart_item_key => $cart_item ) {
// HERE I AM TRYING TO SKIP AND PREVENT CART ITEMS OF FREE_GIFTS BEING COUNTED
if ( isset( $cart_item['free_gift'] ) ) {
continue;
}
// Count for regular products.
$cart_num_products++;
}
// Set the maximum number of products before checking out
$maximum_num_products = 16;
// Compare values and add an error is Cart's total number of products
// happens to be less than the minimum required before checking out.
// Will display a message along the lines of
// A Maximum of 16 products is allowed before checking out. (Cont. below)
if( $cart_num_products > $maximum_num_products ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Maximum of %s snacks are allowed per order.</strong>'
. '<br />Current number of snacks: %s.',
$maximum_num_products,
$cart_num_products ),
'error' );
}
}
}
首先检查购物车中是否有免费商品。当您在结帐可访问之前计算函数中的项目“-1”时。
function free_product_in_cart($free_product_id) {
$free_product_cart_id = WC()->cart->generate_cart_id( $free_product_id );
return WC()->cart->find_product_in_cart( $free_product_cart_id ); //Return true when the free product is in the cart
}
然后你更新你的逻辑来检查:
if(free_product_in_cart(your product id) {
$free_item_space = 1;
} else {
$free_item_space = 0;
}
if( $cart_num_products > $maximum_num_products + $free_item_space) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Maximum of %s snacks are allowed per order.</strong>'
. '<br />Current number of snacks: %s.',
$maximum_num_products,
$cart_num_products ),
'error' );
}
尝试以下操作,这会将您自定义的免费商品从购物车商品数量中移除:
add_action( 'woocommerce_check_cart_items', 'max_allowed_cart_items' );
function max_allowed_cart_items() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
// Set the maximum number of products before checking out
$max_items_count = 16;
$cart_items_count = WC()->cart->get_cart_contents_count( );
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( isset( $cart_item['free_gift'] ) ) {
$cart_items_count -= $cart_item['quantity'];
}
}
if( $cart_items_count > $max_items_count ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Maximum of %s snacks are allowed per order.</strong>'
. '<br />Current number of snacks: %s.',
$max_items_count,
$cart_items_count ),
'error' );
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。
我有一个功能可以将购物车中商品的最大订购数量设置为 16。因此用户无法结帐超过 16 件商品。
我也是运行一个插件,在添加优惠券时会在$cart_item array
中添加一个free_gift
键。
问题是,当用户添加 16 件商品时 + free_gift
= 总数为 17 件,这会阻止结帐。
如何删除 free_gift
添加到购物车的商品数量?
示例:
- 让 Woocommerce 购物车认为 16 件商品 + 1
free_gift
= 16 件商品 - 让 Woocommerce 购物车认为 12 件商品 + 1
free_gift
= 12 件商品
到目前为止,我的代码允许添加 free_gifts 超过最大 16 个限制,但不应用 16 个项目的最大规则:
// Set a maximum number of products requirement before checking out
add_action( 'woocommerce_check_cart_items', 'spyr_set_max_num_products' );
function spyr_set_max_num_products() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
$cart_num_products = 0;
foreach ( WC()->cart->cart_contents as $cart_item_key => $cart_item ) {
// HERE I AM TRYING TO SKIP AND PREVENT CART ITEMS OF FREE_GIFTS BEING COUNTED
if ( isset( $cart_item['free_gift'] ) ) {
continue;
}
// Count for regular products.
$cart_num_products++;
}
// Set the maximum number of products before checking out
$maximum_num_products = 16;
// Compare values and add an error is Cart's total number of products
// happens to be less than the minimum required before checking out.
// Will display a message along the lines of
// A Maximum of 16 products is allowed before checking out. (Cont. below)
if( $cart_num_products > $maximum_num_products ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Maximum of %s snacks are allowed per order.</strong>'
. '<br />Current number of snacks: %s.',
$maximum_num_products,
$cart_num_products ),
'error' );
}
}
}
首先检查购物车中是否有免费商品。当您在结帐可访问之前计算函数中的项目“-1”时。
function free_product_in_cart($free_product_id) {
$free_product_cart_id = WC()->cart->generate_cart_id( $free_product_id );
return WC()->cart->find_product_in_cart( $free_product_cart_id ); //Return true when the free product is in the cart
}
然后你更新你的逻辑来检查:
if(free_product_in_cart(your product id) {
$free_item_space = 1;
} else {
$free_item_space = 0;
}
if( $cart_num_products > $maximum_num_products + $free_item_space) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Maximum of %s snacks are allowed per order.</strong>'
. '<br />Current number of snacks: %s.',
$maximum_num_products,
$cart_num_products ),
'error' );
}
尝试以下操作,这会将您自定义的免费商品从购物车商品数量中移除:
add_action( 'woocommerce_check_cart_items', 'max_allowed_cart_items' );
function max_allowed_cart_items() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
// Set the maximum number of products before checking out
$max_items_count = 16;
$cart_items_count = WC()->cart->get_cart_contents_count( );
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( isset( $cart_item['free_gift'] ) ) {
$cart_items_count -= $cart_item['quantity'];
}
}
if( $cart_items_count > $max_items_count ) {
// Display our error message
wc_add_notice( sprintf( '<strong>A Maximum of %s snacks are allowed per order.</strong>'
. '<br />Current number of snacks: %s.',
$max_items_count,
$cart_items_count ),
'error' );
}
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。