基于 Woocommerce 中的运费 类 的运费折扣
Shipping cost discount based on a shipping classes in Woocommerce
我正在尝试为当前购物车中的产品的一次运费应用折扣 class。这适用于结帐视图。
在 Woocommerce 后端,选项设置为单独收取每次运费 class。另外,我只使用一种名为 "flat rate" 的运输方式。
基于 ,应应用折扣的以下代码:
add_filter('woocommerce_package_rates', 'shipping_class_null_shipping_costs', 10, 2);
function shipping_class_null_shipping_costs( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
$shipping_class_slug = 'large'; // Your shipping class slug
$found = false;
// Loop through cart items and checking for the specific defined shipping class
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_shipping_class() == $shipping_class_slug )
$found = true;
}
$percentage = 50; // 50%
$subtotal = WC()->cart->get_cart_shipping_total();
// Set shipping costs to 50% discount if shipping class is found
if( $found ){
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targetting "flat rate"
if( 'flat_rate' === $rate->method_id ){
$rates[$rate_key]->cost = $subtotal;
}
}
}
return $rates;
}
但是无论我怎么尝试,计算出的运费结果都是 0 美元。
我在这里做错了什么,对运费应用折扣的正确方法是什么 class?
谢谢。
更新(关于设置)
要为“统一费率”送货方式的“大件”送货 class 添加折扣,您必须:
- 直接在您的送货方式成本上设置折扣价。
- 启用选项“每class:每次class单独收取运费”
喜欢:
原回答:
当在购物车商品中找到特定定义的送货方式时,以下代码将为“统一费率”送货方式设置 50% 的运费。
Testing: Temporary "Enable debug mode" in Shipping settings under Shipping options tab...
运费“统一费率”设置:您的运费 classes 成本应该定义。
在下面的代码中,在每个函数中定义您的运输 class slug 和您的自定义通知:
add_filter('woocommerce_package_rates', 'shipping_costs_discounted_based_on_shipping_class', 10, 2);
function shipping_costs_discounted_based_on_shipping_class( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// Your settings below
$shipping_class = 'large'; // <=== Shipping class slug
$percentage = 50; // <=== Discount percentage
$discount_rate = $percentage / 100;
$is_found = false;
// Loop through cart items and checking for the specific defined shipping class
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_shipping_class() == $shipping_class )
$is_found = true;
}
// Set shipping costs to 50% if shipping class is found
if( $is_found ){
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targeting "flat rate"
if( 'flat_rate' === $rate->method_id ){
$rates[$rate_key]->cost = $rate->cost * $discount_rate;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
$has_taxes = true;
$taxes[$key] = $tax * $discount_rate;
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
Don't forget to disable "debug mode" in shipping settings once this has been tested once.
我正在尝试为当前购物车中的产品的一次运费应用折扣 class。这适用于结帐视图。
在 Woocommerce 后端,选项设置为单独收取每次运费 class。另外,我只使用一种名为 "flat rate" 的运输方式。
基于
add_filter('woocommerce_package_rates', 'shipping_class_null_shipping_costs', 10, 2);
function shipping_class_null_shipping_costs( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
$shipping_class_slug = 'large'; // Your shipping class slug
$found = false;
// Loop through cart items and checking for the specific defined shipping class
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_shipping_class() == $shipping_class_slug )
$found = true;
}
$percentage = 50; // 50%
$subtotal = WC()->cart->get_cart_shipping_total();
// Set shipping costs to 50% discount if shipping class is found
if( $found ){
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targetting "flat rate"
if( 'flat_rate' === $rate->method_id ){
$rates[$rate_key]->cost = $subtotal;
}
}
}
return $rates;
}
但是无论我怎么尝试,计算出的运费结果都是 0 美元。
我在这里做错了什么,对运费应用折扣的正确方法是什么 class?
谢谢。
更新(关于设置)
要为“统一费率”送货方式的“大件”送货 class 添加折扣,您必须:
- 直接在您的送货方式成本上设置折扣价。
- 启用选项“每class:每次class单独收取运费”
喜欢:
原回答:
当在购物车商品中找到特定定义的送货方式时,以下代码将为“统一费率”送货方式设置 50% 的运费。
Testing: Temporary "Enable debug mode" in Shipping settings under Shipping options tab...
运费“统一费率”设置:您的运费 classes 成本应该定义。
在下面的代码中,在每个函数中定义您的运输 class slug 和您的自定义通知:
add_filter('woocommerce_package_rates', 'shipping_costs_discounted_based_on_shipping_class', 10, 2);
function shipping_costs_discounted_based_on_shipping_class( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// Your settings below
$shipping_class = 'large'; // <=== Shipping class slug
$percentage = 50; // <=== Discount percentage
$discount_rate = $percentage / 100;
$is_found = false;
// Loop through cart items and checking for the specific defined shipping class
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_shipping_class() == $shipping_class )
$is_found = true;
}
// Set shipping costs to 50% if shipping class is found
if( $is_found ){
foreach ( $rates as $rate_key => $rate ){
$has_taxes = false;
// Targeting "flat rate"
if( 'flat_rate' === $rate->method_id ){
$rates[$rate_key]->cost = $rate->cost * $discount_rate;
// Taxes rate cost (if enabled)
foreach ($rates[$rate_key]->taxes as $key => $tax){
if( $tax > 0 ){
$has_taxes = true;
$taxes[$key] = $tax * $discount_rate;
}
}
if( $has_taxes )
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
Don't forget to disable "debug mode" in shipping settings once this has been tested once.