默认押金占总购物车金额的百分比,但有例外
Default deposit on a percentage of the total cart amount with exceptions
我正在寻找一种方法来在 Woocommerce 网站上的总购物车金额上添加存款(而不是仅仅为每个产品线项目添加存款)。
我在这里找到了这个巧妙线程的答案:
这是我最终使用的代码:
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,然后我可以在前端设置描述的样式。)
但是,我有一些产品(一个产品类别)我不想强行支付这笔押金。
所以,这是我的问题:
我如何调整代码以继续执行默认押金但从一个产品类别中排除押金(如果我不能排除整个类别,则排除该类别中的产品)?
在下面的挂钩函数中,您必须设置一组产品 ID 或 (and) 产品类别,以排除它们。如果你不使用其中之一,你可以设置一个空白数组,例如 $product_categories = array();
…
代码如下:
add_action( 'woocommerce_cart_calculate_fees', 'custom_deposit_calculation', 10, 1 );
function custom_deposit_calculation( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Define the product IDs to exclude
$product_ids = array( 37, 25, 50 );
// Define the product categories to exclude (can be IDs, slugs or names)
$product_categories = array( 'clothing' );
$amount_to_exclude_with_tax = 0;
// Iterating through cart items
foreach ( $cart_object->get_cart() as $cart_item ){
// If condition match we get the sum of the line item total (excl. tax)
if( in_array( $cart_item['product_id'], $product_ids ) || has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) )
$amount_to_exclude_with_tax += $cart_item['line_total'];
// OR replace by (for tax inclusion)
// $amount_to_exclude_with_tax += $cart_item['line_tax'] + $cart_item['line_total'];
}
## Set HERE your negative percentage (to remove an amount from cart total)
$percent = -0.5; // 50% off (negative)
// Get cart subtotal excluding taxes
$cart_subtotal = $cart_object->subtotal_ex_tax - $amount_to_exclude_with_tax;
// or for subtotal including taxes use instead:
// $cart_subtotal = $cart_object->subtotal;
## ## CALCULATION ## ##
$calculated_amount = $cart_subtotal * $percent;
if( $calculated_amount != 0){
// Adding a negative fee to cart amount (Including taxes)
$cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
在 WooCommerce 3 上测试并有效。
我正在寻找一种方法来在 Woocommerce 网站上的总购物车金额上添加存款(而不是仅仅为每个产品线项目添加存款)。
我在这里找到了这个巧妙线程的答案:
这是我最终使用的代码:
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,然后我可以在前端设置描述的样式。)
但是,我有一些产品(一个产品类别)我不想强行支付这笔押金。
所以,这是我的问题:
我如何调整代码以继续执行默认押金但从一个产品类别中排除押金(如果我不能排除整个类别,则排除该类别中的产品)?
在下面的挂钩函数中,您必须设置一组产品 ID 或 (and) 产品类别,以排除它们。如果你不使用其中之一,你可以设置一个空白数组,例如 $product_categories = array();
…
代码如下:
add_action( 'woocommerce_cart_calculate_fees', 'custom_deposit_calculation', 10, 1 );
function custom_deposit_calculation( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Define the product IDs to exclude
$product_ids = array( 37, 25, 50 );
// Define the product categories to exclude (can be IDs, slugs or names)
$product_categories = array( 'clothing' );
$amount_to_exclude_with_tax = 0;
// Iterating through cart items
foreach ( $cart_object->get_cart() as $cart_item ){
// If condition match we get the sum of the line item total (excl. tax)
if( in_array( $cart_item['product_id'], $product_ids ) || has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) )
$amount_to_exclude_with_tax += $cart_item['line_total'];
// OR replace by (for tax inclusion)
// $amount_to_exclude_with_tax += $cart_item['line_tax'] + $cart_item['line_total'];
}
## Set HERE your negative percentage (to remove an amount from cart total)
$percent = -0.5; // 50% off (negative)
// Get cart subtotal excluding taxes
$cart_subtotal = $cart_object->subtotal_ex_tax - $amount_to_exclude_with_tax;
// or for subtotal including taxes use instead:
// $cart_subtotal = $cart_object->subtotal;
## ## CALCULATION ## ##
$calculated_amount = $cart_subtotal * $percent;
if( $calculated_amount != 0){
// Adding a negative fee to cart amount (Including taxes)
$cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
在 WooCommerce 3 上测试并有效。