基于总购物车金额的百分比存款
问题描述:
我已经从另一篇文章中获取此代码,基本上从我的理解中,此代码试图强制购物车价格更改为固定金额40美元,并将其作为预订收取费用费用。基于总购物车金额的百分比存款
我想要做的就是强制购物车的数量是总购物车中所有产品的总和的20%。我的网站是为保留,所以我只想收取押金,然后让他们在使用他们的预订时支付。
下面是这篇文章的代码:Woocommerce cart deposit
add_action('woocommerce_cart_calculate_fees', 'booking_fee');
function booking_fee() {
global $woocommerce;
if (is_admin() && ! defined('DOING_AJAX'))
return;
$bookingfee_array = array('2434');
$fixed = 40.00;
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
if(in_array($values['product_id'], $bookingfee_array)) {
$surcharge = $fixed;
$woocommerce->cart->add_fee('Broneeringutasu', $surcharge, true, '');
}
}
}
这里是我会怎么改变它:
add_action('woocommerce_cart_calculate_fees', 'booking_fee');
function booking_fee() {
global $woocommerce;
if (is_admin() && ! defined('DOING_AJAX'))
return;
$bookingfee_array = array('2434');
$percent = .20;
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $values) {
if(in_array($values['product_id'], $bookingfee_array)) {
$surcharge = $percent;
$woocommerce->cart->add_fee('Booking Fee', $surcharge, true, '');
}
}
}
但预期这是行不通的。
对此有何帮助?
感谢
答
你的代码是不是真的会做你所期望的,因为你要添加的o.2
一定的费用,总购物车数量。因此,如果车量100
,总计将是100.2
...
你想做的事,而不是,什么是去除购物车总金额的80%,获得20%的车量。这可以使用总购物车数量的80%的负费用。如果您不需要像原始代码那样设置目标产品ID,请使用下面的第二个功能。
这里,将删除总购物车数量的80%的第一功能,当车项目的比赛,在功能设置的针对性产品ID:
add_action('woocommerce_cart_calculate_fees', 'booking_deposit_calculation');
function booking_deposit_calculation($cart_object) {
if (is_admin() && ! defined('DOING_AJAX'))
return;
## Set HERE your targeted products IDs
$target_product_ids = array(2434);
## Set HERE your negative percentage (to remove an amount from cart total)
$percent = -.80; // 80% off (negative)
$matching = false;
// Iterating through each cart items
foreach ($cart_object->get_cart() as $item_values)
{
if(in_array($item_values['product_id'], $target_product_ids))
{ // If a cart item match with a targeted product ID
// Get cart subtotal excluding taxes
$cart_subtotal = $cart_object->subtotal_ex_tax;
// or for subtotal including taxes use instead:
// $cart_subtotal = $cart_object->subtotal;
## ## CALCULATION ## ##
$calculated_amount = $cart_subtotal * $percent;
// Adding a negative fee to cart amount (Including taxes)
$cart_object->add_fee(__('Deposit calculation', 'woocommerce'), $calculated_amount, true);
break; // We stop the loop
}
}
}
代码放在你的积极的function.php文件儿童主题(或主题)或任何插件文件。
但可能是你并不需要有一些有针对性的产品作为原代码。
如果是这种简化代码的情况:
add_action('woocommerce_cart_calculate_fees', 'booking_deposit_calculation');
function booking_deposit_calculation($cart_object) {
if (is_admin() && ! defined('DOING_AJAX'))
return;
## Set HERE your negative percentage (to remove an amount from cart total)
$percent = -.80; // 80% off (negative)
// Get cart subtotal excluding taxes
$cart_subtotal = $cart_object->subtotal_ex_tax;
// or for subtotal including taxes use instead:
// $cart_subtotal = $cart_object->subtotal;
## ## CALCULATION ## ##
$calculated_amount = $cart_subtotal * $percent;
// Adding a negative fee to cart amount (Including taxes)
$cart_object->add_fee(__('Deposit calculation', 'woocommerce'), $calculated_amount, true);
}
代码放在您的活动子主题(或主题)的function.php文件中的任何插件文件或者也。
两种功能测试和工程
这正是我一直在寻找!非常感谢你。 –
@ Stewsgarage.com欢迎,这对我来说非常简单...还有一些免费插件:https://wordpress.org/plugins/search.php?q = woocommerce + deposit – LoicTheAztec