因此,我使用这个PHP片段,以便当我的客户购物车里有超过60美元时,他们可以免费送货。
function wc_ninja_change_flat_rates_cost( $rates, $package ) {
// Make sure flat rate is available
if ( isset( $rates['flat_rate:1'] ) ) {
// Current value of the shopping cart
$cart_subtotal = WC()->cart->subtotal;
// Check if the subtotal is greater than 60
if ( $cart_subtotal >= 59.99 ) {
// Set the cost to $0
$rates['flat_rate:1']->cost = 0;
$rates['flat_rate:2']->cost = 0;
$rates['flat_rate:3']->cost = 0;
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'wc_ninja_change_flat_rates_cost', 10, 2 );目前,如果他们的产品在购物车中花费了70美元,并且申请了20美元的优惠券,他们仍然可以免费送货(因为小计在60美元以上)。
我想给予他们一个免费送货(将所有运输方法的成本设置为零),只有当金额大于60美元后,任何折扣/优惠券。
我已经试过:
$cart_subtotal = WC()->cart->total;但是没有区别,有什么办法可以做到吗?
发布于 2022-03-31 13:09:48
试着:
$cart_total = WC()->cart->get_total();发布于 2022-04-01 07:07:53
使用WC()->cart->get_total()不会给出想要的结果,因为输出也包含HTML,而您只需要一个整数,而是使用WC()->cart->cart_contents_total。
也没有必要通过代码($rates['flat_rate:1']、$rates['flat_rate:']等)定义所有的传送方法。),您可以循环遍历所有可用的传送方法并对它们进行调整。
所以你得到了:
function filter_woocommerce_package_rates( $rates, $package ) {
// Cart total amount (integer)
$cart_total = WC()->cart->cart_contents_total;
// Greater than or equal to
if ( $cart_total >= 59.99 ) {
foreach ( $rates as $rate_key => $rate ) {
// For "free shipping" method (enabled), remove it
if ( $rate->method_id == 'free_shipping' ) {
unset( $rates[$rate_key] );
// For other shipping methods
} else {
// Append rate label titles (free)
$rates[$rate_key]->label .= ' ' . __( '(free)', 'woocommerce' );
// Set rate cost
$rates[$rate_key]->cost = 0;
// Set taxes rate cost (if enabled)
$taxes = array();
foreach ( $rates[$rate_key]->taxes as $key => $tax ) {
if ( $rates[$rate_key]->taxes[$key] > 0 ) {
$taxes[$key] = 0;
}
}
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates','filter_woocommerce_package_rates', 10, 2 );不要忘记清空您的购物车来刷新传送缓存的数据!
我的答案很大程度上是基于Set all shipping methods cost to zero for a Free shipping coupon in Woocommerce答案代码
https://stackoverflow.com/questions/71692605
复制相似问题