基于重量的累进折扣显示剩余重量以获得更好的折扣信息

我正在使用基于 Woocommerce 购物车总重量答案代码(针对我之前的问题)应用渐进折扣来根据购物车总重量添加批量折扣。

代码很完美,但我想在购物车页面(仅限购物车)上方显示一个横幅,例如“已添加到购物车”消息,其中显示一条消息以及客户必须为下一个折扣规则订购更多的金额。

示例消息:“如果您订购 10 欧元,额外您将获得 10% 的折扣”

我试图为消息框找到正确的代码,但找不到正确的代码。我只能找到wc_add_to_cart_message_html,但我很确定这个不是正确的。

很抱歉提出所有这些问题,但我对 PHP 很陌生并且想学习。希望任何人都可以提供帮助。


暮色呼如
浏览 52回答 1
1回答

一只甜甜圈

更新您必须将所有相关代码替换为以下代码,该代码将根据购物车重量进行折扣,显示一条显示剩余重量的自定义消息,以获得更好的百分比折扣(显示百分比)。对于消息:由于它是基于重量的折扣,因此无法显示剩余成本。相反,您可以显示获得更好折扣所需的剩余重量。这是代码:add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_discount', 30, 1 );function shipping_weight_discount( $cart ) {&nbsp; &nbsp; if ( is_admin() && ! defined( 'DOING_AJAX' ) )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; $cart_weight&nbsp; &nbsp;= $cart->get_cart_contents_weight();&nbsp; &nbsp; $cart_subtotal = $cart->get_subtotal(); // Or $cart->subtotal;&nbsp; &nbsp; $percentage&nbsp; &nbsp; = 0;&nbsp; &nbsp; if ( $cart_weight >= 10 && $cart_weight < 30 )&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $percentage&nbsp; &nbsp; &nbsp; &nbsp;= 5;&nbsp; &nbsp; &nbsp; &nbsp; $remaining_weight = 30 - $cart_weight;&nbsp; &nbsp; &nbsp; &nbsp; $next_percent&nbsp; &nbsp; &nbsp;= 7.5;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; elseif ( $cart_weight >= 30 && $cart_weight < 70 )&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $percentage&nbsp; &nbsp; &nbsp; &nbsp;= 7.5;&nbsp; &nbsp; &nbsp; &nbsp; $remaining_weight = 70 - $cart_weight;&nbsp; &nbsp; &nbsp; &nbsp; $next_percent&nbsp; &nbsp; &nbsp;= 10;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; elseif ( $cart_weight >= 70 && $cart_weight < 130 )&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $percentage&nbsp; &nbsp; &nbsp; &nbsp;= 10;&nbsp; &nbsp; &nbsp; &nbsp; $remaining_weight = 130 - $cart_weight;&nbsp; &nbsp; &nbsp; &nbsp; $next_percent&nbsp; &nbsp; &nbsp;= 12.5;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; elseif ( $cart_weight >= 130 && $cart_weight < 200 ) {&nbsp; &nbsp; &nbsp; &nbsp; $percentage&nbsp; &nbsp; &nbsp; &nbsp;= 12.5;&nbsp; &nbsp; &nbsp; &nbsp; $remaining_weight = 200 - $cart_weight;&nbsp; &nbsp; &nbsp; &nbsp; $next_percent = 15;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; elseif ( $cart_weight >= 200 )&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $percentage&nbsp; &nbsp; &nbsp; &nbsp;= 15;&nbsp; &nbsp; &nbsp; &nbsp; $next_percent&nbsp; &nbsp; &nbsp;= false;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $next_percent = 5;&nbsp; &nbsp; &nbsp; &nbsp; $remaining_weight = 10 - $cart_weight;&nbsp; &nbsp; }&nbsp; &nbsp; // Apply a calculated discount based on weight&nbsp; &nbsp; if( $percentage > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; $discount = $cart_subtotal * $percentage / 100;&nbsp; &nbsp; &nbsp; &nbsp; $cart->add_fee( sprintf( __( 'Weight %s discount', 'woocommerce' ), $percentage.'%'), -$discount );&nbsp; &nbsp; }&nbsp; &nbsp; if ( did_action( 'woocommerce_cart_calculate_fees' ) >= 2 )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; // Display a custom message&nbsp; &nbsp; if ( is_cart() && $next_percent ) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; wc_add_notice( sprintf(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __("If you order for %s extra, you will receive a %s discount.", "woocommerce"),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_format_weight($remaining_weight), $next_percent.'%'&nbsp; &nbsp; &nbsp; &nbsp; ), 'notice' );&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。购物车中显示的消息的屏幕截图:
打开App,查看更多内容
随时随地看视频慕课网APP