Php WooCommerce自定义代码将在项目';已添加到购物车中
我正在尝试设置一个自定义优惠券代码,以便客户可以将附加产品添加到他们的购物车中,前提是他们的购物车总额为50美元或更多 我使用的回答代码,允许或拒绝添加产品取决于他们的购物车总数,这是伟大的工作 我的问题是,如果加载项在购物车中并且购物车总数不再在阈值内,我还有一段代码应该删除它 在我的例子中,我将阈值设置为50美元,插件产品的价格为10美元 在测试中,我发现当购物车的总金额为60美元或更多时,一切正常。我甚至可以从购物车中删除几个项目,插件项目将一直保留,直到总数低于阈值,然后它也会被删除 但是,如果在我添加附加项时购物车的总金额在$50到$60之间,则该附加项将在添加后立即删除 我期待着行动的顺序Php WooCommerce自定义代码将在项目';已添加到购物车中,php,wordpress,woocommerce,cart,add-on,Php,Wordpress,Woocommerce,Cart,Add On,我正在尝试设置一个自定义优惠券代码,以便客户可以将附加产品添加到他们的购物车中,前提是他们的购物车总额为50美元或更多 我使用的回答代码,允许或拒绝添加产品取决于他们的购物车总数,这是伟大的工作 我的问题是,如果加载项在购物车中并且购物车总数不再在阈值内,我还有一段代码应该删除它 在我的例子中,我将阈值设置为50美元,插件产品的价格为10美元 在测试中,我发现当购物车的总金额为60美元或更多时,一切正常。我甚至可以从购物车中删除几个项目,插件项目将一直保留,直到总数低于阈值,然后它也会被删除 但
add_action( 'woocommerce_before_calculate_totals', 'add_or_remove_cart_items', 10, 1 );
function add_or_remove_cart_items( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// ONLY for logged users (and avoiding the hook repetition)
if ( ! is_user_logged_in() && did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$threshold_amount = 50; // The threshold amount for cart total
$addon_product_id = 7039; // ID of the addon item
$addon_product_cost = 10; // Cost of addon item
$cart_items_total = 0; // Initializing
$threshold_amount_with_addon = $threshold_amount + $addon_product_cost;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
// Check if the free product is in cart
if ( $cart_item['data']->get_id() == $addon_product_id ) {
$addon_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 below the defined amount and addon product is in cart, we remove it.
if ( $cart_items_total < $threshold_amount_with_addon && isset($addon_item_key) ) {
$cart->remove_cart_item( $addon_item_key );
return;
}
}
add_动作('woocommerce_在计算_总数之前,'add_或_remove_cart_项目',10,1);
功能添加或删除购物车项目($cart){
if(定义了('DOING'uajax'))
返回;
//仅适用于已登录的用户(并避免钩子重复)
如果(!is_user_logged_in()&&did_action('woocommerce_before_calculate_totals')>=2)
返回;
$threshold\u amount=50;//购物车总计的阈值金额
$addon\u product\u id=7039;//加载项的id
$addon\u product\u cost=10;//附加项的成本
$cart\u items\u total=0;//正在初始化
$threshold\u amount\u with\u addon=$threshold\u amount+$addon\u product\u cost;
//循环浏览购物车项目
foreach($cart->get\u cart()作为$cart\u item\u key=>$cart\u item){
//检查免费产品是否在购物车中
如果($cart\u item['data']->get\u id()==$addon\u product\u id){
$addon\u item\u key=$cart\u item\u key;
}
//获取购物车小计(包括来自项目的税费)(如果有折扣)
$cart_items_total+=$cart_item['line_total']+$cart_item['line_tax'];
}
//如果购物车总额低于定义的金额,并且加载项产品在购物车中,我们将其删除。
if($cart\u items\u total<$threshold\u amount\u,带有附加项和isset($addon\u item\u key)){
$cart->remove_cart_item($addon_item_key);
返回;
}
}
我一辈子都搞不清楚到底出了什么问题。任何帮助都将不胜感激。在下面,我们将附加项目从小计计算中排除,因此我们不需要将附加价格添加到阈值金额中(因此我们保留步骤1、2和4) 重新访问的代码:
add_action( 'woocommerce_before_calculate_totals', 'add_or_remove_cart_items', 10, 1 );
function add_or_remove_cart_items( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// ONLY for logged users, avoiding the hook repetition
if ( ! is_user_logged_in() || did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
$addon_product_id = 7039; // ID of the addon item
$threshold_amount = 50; // The threshold amount
$cart_items_total = 0; // Initializing
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ){
// For Add-On product: Check if it is in cart
if ( in_array( $addon_product_id, [$cart_item['product_id'], $cart_item['variation_id']] ) ) {
$addon_item_key = $cart_item_key;
}
// For other items: add their line total including taxes to items subtotal
else {
// Sum dicounted line total incl. taxes to get other items subtotal
$cart_items_total += $cart_item['line_total'] + $cart_item['line_tax'];
}
}
// If cart total is below the defined amount and addon product is in cart, we remove it.
if ( $cart_items_total < $threshold_amount && isset($addon_item_key) ) {
$cart->remove_cart_item( $addon_item_key );
}
}
add_动作('woocommerce_在计算_总数之前,'add_或_remove_cart_项目',10,1);
功能添加或删除购物车项目($cart){
if(定义了('DOING'uajax'))
返回;
//仅适用于已登录的用户,避免钩子重复
如果(!is_user_logged_in()| | did_action('woocommerce_before_calculate_totals')>=2)
返回;
$addon\u product\u id=7039;//加载项的id
$threshold\u amount=50;//阈值金额
$cart\u items\u total=0;//正在初始化
//循环浏览购物车项目
foreach($cart->get\u cart()作为$cart\u item\u key=>$cart\u item){
//对于附加产品:检查它是否在购物车中
if(在数组中($addon\u product\u id、[$cart\u item['product\u id']、$cart\u item['variation\u id']])){
$addon\u item\u key=$cart\u item\u key;
}
//对于其他项目:将其行总计(包括税费)添加到项目小计中
否则{
//合计已计算的行总计,包括用于获取其他项目小计的税费
$cart_items_total+=$cart_item['line_total']+$cart_item['line_tax'];
}
}
//如果购物车总额低于定义的金额,并且加载项产品在购物车中,我们将其删除。
if($cart\u items\u total<$threshold\u amount&&isset($addon\u item\u key)){
$cart->remove_cart_item($addon_item_key);
}
}
代码进入活动子主题(或活动主题)的functions.php文件。经过测试,效果良好。非常好,谢谢!如果您不介意我问一下,为什么在前面的代码不起作用的情况下,这个功能可以工作?@SIGdesign首先,如果这个答案回答了您的问题,您可以请您回答,谢谢。解决方案在开头解释(不包括要在购物车总额和阈值金额中求和的附加价格)。