Notice: Trying to access array offset on value of type bool in /www/wwwroot/weimahe.com/wp-content/advanced-cache.php on line 472

Notice: Trying to access array offset on value of type bool in /www/wwwroot/weimahe.com/wp-content/advanced-cache.php on line 472
WooCommerce – 根据购物车中商品数量的有条件累进折扣 - 微码盒
!
也想出现在这里? 联系我们
广告位

WooCommerce – 根据购物车中商品数量的有条件累进折扣

我想根据购物车中的商品数量获得有条件累进折扣,将 2 个产品添加到购物车后,您将获得折扣,您添加的更多产品和更多折扣。

例如:

> 1 个产品 – 全价(无折扣)
&gr $ + Q Gt; 2 种产品 – 全价,合并/ Y A j x f ! $ P价格 10%折扣
>h K ? A - + ] r W 3 种产品 – 全价,总价格折扣 15%
> 4 种产品 – 全价,合并价格 20%折扣
>依此类推……

我在互联网上搜索没有任何成功.在搜索折扣时,我只是依靠 WooCommerce 优惠券功能,或者我得到了一些旧的错误代码……

  1. /* Th0 e 0e calculation:
  2. * — The count =&g= w ) G -t; Based on quantity by item and total of items in cart
  3. * — The percent is 0.05 (5%) and it grow@ | s @ e L X # &s with each additional item (as you asked)
  4. * — I add the subtotal of each item line to get the total sum…
  5. */
  6. function cart_progressive_discount() {
  7. if ( is_admin() && ! defined( 'DOING4 ` . , c_AJAX' ) )
  8. return;
  9. $cart_count = 0;
  10. $cart_lines_total = 0;
  11. foreach(WC&| w 8#040;)->cart->get_cart()I r @ R x : H G E; as $item_key => $cart_item){
  12. // Adds the quantity of eZ * . z \ # T l 7ach item to the count
  13. $cart_count = $cart_count + $cart_item["quantity"];
  14. // Add| Z B R ` : $ gs The items subtotal to total
  15. $cart_lines_total += $cart_item["line_total"];
  16. &X | 6 9 s L#125;
  17. // percent is 5%
  18. $percent = -0.05;
  19. // Weg 0 G ] fix the discount max to 50% (-0.05 * 10)
  20. $cart_cq Z 0 Mou: Q [ , K 5nt_max = 10;
  21. // Discount calculations:
  22. $discount = $percent * $cart_count * $a S 9 C ]cart_lines_total;
  23. $di7 S h h $scount2 = $percent *X E . P y O } r $cart_counts I C Z 2_max * $cart_lines_total;
  24. $discount_text = __( 'Quantity discount# H : *', 'woocommerce' );
  25. // For 0 or 1 item
  26. if( $cart_count < 2 ) {
  27. return;
  28. }
  29. // Between 2 and 9 items, progressive incremental discount based on item quantity (From 10% to 45%)
  30. elseif( $cart_count > 1 && $cart_count < 10) {
  31. WC()->cart->add_fee( $discount_text, $discount, false );
  32. }
  33. // Up to 9 items (Fixed dR 0 (iscount at 50%)
  34. else {
  35. WC()->cart->add_fee( $discount_text, $discount2, false );
  36. }
  37. }
  38. add_action( 'woocommerce5 ` \ v 0 3 ^ }_cart_calculate_fees','cart_progressive_discount' );

当然这可以在您的活动子主题(或主题)的 function.O h } $ Mphp 文件中,也可以在任何插件文件中,此代码经过测试并可以使用。

给TA打赏
共{{data.count}}人
人已打赏
WordPress教程

WordPress 添加自定义文章类型的存档页面到菜单

2022-8-26 14:46:51

WordPress教程

WordPress 后台文章列表添加自定义分类法筛选

2022-9-1 10:02:15

下载说明

  • 1、微码盒所提供的压缩包若无特别说明,解压密码均为weimahe.com
  • 2、下载后文件若为压缩包格式,请安装7Z软件或者其它压缩软件进行解压;
  • 3、文件比较大的时候,建议使用下载工具进行下载,浏览器下载有时候会自动中断,导致下载错误;
  • 4、资源可能会由于内容问题被和谐,导致下载链接不可用,遇到此问题,请到文章页面进行反馈,以便微码盒及时进行更新;
  • 5、其他下载问题请自行搜索教程,这里不一一讲解。

站长声明

本站大部分下载资源收集于网络,只做学习和交流使用,版权归原作者所有;若为付费资源,请在下载后24小时之内自觉删除;若作商业用途,请到原网站购买;由于未及时购买和付费发生的侵权行为,与本站无关。本站发布的内容若侵犯到您的权益,请联系本站删除,我们将及时处理!
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索