如果优惠券金额高于购物车小计,还要减少运费总额

发布于 2025-01-12 13:08:30 字数 733 浏览 0 评论 0原文

该代码应该检查固定优惠券金额是否大于购物车小计。如果是,请从运费总额中减去剩余价格。它在我的函数文件中。

add_filter('woocommerce_package_rates', 'custom_shipping_costs', 10, 2 );
function custom_shipping_costs( $rates, $package ){

//get the shipping total, cart subtotal and coupon amount
$dostava = $cart->shipping_total;
$iznos = $woocommerce->cart->get_cart_subtotal();
$kuponi = $woocommerce->cart->discount_total;

//subtract the coupon amount from cart subtotal
$razlika = $iznos - $kuponi;

//now check if it's higher than what's in the cart, if yes, set the new shipping costs
if($razlika < 0){
    $novadostava = $dostava - $razlika;
    $cart->shipping_total = $novadostava;
}

return $rates;
}

不幸的是..它不起作用。有什么建议吗?

The code is supposed to check if the fixed coupon amount is bigger than cart subtotals. If yes, subtract the remainder of the price from shipping totals. It's in my functions file.

add_filter('woocommerce_package_rates', 'custom_shipping_costs', 10, 2 );
function custom_shipping_costs( $rates, $package ){

//get the shipping total, cart subtotal and coupon amount
$dostava = $cart->shipping_total;
$iznos = $woocommerce->cart->get_cart_subtotal();
$kuponi = $woocommerce->cart->discount_total;

//subtract the coupon amount from cart subtotal
$razlika = $iznos - $kuponi;

//now check if it's higher than what's in the cart, if yes, set the new shipping costs
if($razlika < 0){
    $novadostava = $dostava - $razlika;
    $cart->shipping_total = $novadostava;
}

return $rates;
}

Unfortunately.. it doesn't work. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2025-01-19 13:08:30

这是一个示例,因为如果必须定义等,则需要考虑更多税率、运输方法等,但当前示例可以正常工作
我们有售价 18 美元的产品和折扣 20 美元的优惠券,运费税 15 美元,最后我们总共得到 13 美元 - https ://prnt.sc/365xV2BWx2wA

add_filter('woocommerce_package_rates', 'custom_shipping_costs');
function custom_shipping_costs( $rates ){

$cart = WC()->cart;
if($rates):
    foreach($rates as $rate_key => $rate):
        $dostava = $rate->get_cost();
    endforeach;
endif;
$iznos = $cart->subtotal;
$kuponi = $cart->get_coupons();
if($kuponi):
    foreach($kuponi as $kupon):
        $kuponi_amount = $kupon->get_amount();
    endforeach;
endif;

$razlika = $iznos - $kuponi_amount;
$razlika = abs($razlika);
if($razlika > 0){
    $novadostava = $dostava - $razlika;
    $rates[$rate_key]->cost = $novadostava;
}
return $rates;
}

Here is an example since there are more to consider to it as taxes rates shipping methods if must define etc but current example works as it
We have product that cost 18$ and coupon that discount 20$ with shipping tax 15$ at the end we get 13$ total - https://prnt.sc/365xV2BWx2wA

add_filter('woocommerce_package_rates', 'custom_shipping_costs');
function custom_shipping_costs( $rates ){

$cart = WC()->cart;
if($rates):
    foreach($rates as $rate_key => $rate):
        $dostava = $rate->get_cost();
    endforeach;
endif;
$iznos = $cart->subtotal;
$kuponi = $cart->get_coupons();
if($kuponi):
    foreach($kuponi as $kupon):
        $kuponi_amount = $kupon->get_amount();
    endforeach;
endif;

$razlika = $iznos - $kuponi_amount;
$razlika = abs($razlika);
if($razlika > 0){
    $novadostava = $dostava - $razlika;
    $rates[$rate_key]->cost = $novadostava;
}
return $rates;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文