【发布时间】:2017-10-18 01:14:49
【问题描述】:
我正在寻找一种在 Woocommerce 网站上的总购物车金额上添加押金的方法(而不仅仅是向每个产品行项目添加押金)。
我在这里找到了这个巧妙线程的答案:Deposit based on a percentage of total cart amount
这是我最终使用的代码:
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 = -.50; // 50% 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 (excluding taxes)
$cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, false );
}
这会为购物车和结帐页面上的每个产品创建 50% 的默认押金。杰出的! (使用 CSS,我可以在前端设置描述的样式。)
但是,我不想对一些产品(一个产品类别)进行强制存款。
所以,这是我的问题:
如何调整代码以继续强制执行默认押金,但将押金排除在一个产品类别(或此类别中的产品,如果我无法排除整个类别)之外?
【问题讨论】:
标签: php wordpress woocommerce cart fee