脚本宝典收集整理的这篇文章主要介绍了php – 基于Woocommerce购物车项目计数的条件累进百分比折扣,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
例如:
> 1个产品 – 全价(无折扣)
> 2种产品 – 全价,合并价格5%折扣
> 3种产品 – 全价,合并价格10%折扣
> 4种产品 – 全价,总价格折扣15%
>依此类推……
我在互联网上搜索没有任何成功.当搜索折扣我只是落在WooCommerce优惠券功能或我得到一些旧错误的代码…
任何的想法?我该怎么做?
可能吗?
谢谢.
是的,它可以使用一个技巧,来实现这一目标.通常我们在WooCommerce优惠券中使用的购物车折扣.这里的优惠券没有被挪用.我将在这里使用负面的按条件费,这将成为折扣.
计算:
– 项目计数基于项目数量和购物车中的项目总数
– 百分比为0.05(5%),随着每个额外项目的增长(如您所知)
– 我们使用折扣小计(以避免添加优惠券的多次折叠折扣)
代码:
add_action( 'woocommerce_cart_calculate_fees','cart_PRogressive_discount',50,1 ); function cart_progressive_discount( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; // For 1 ITem (quantity 1) we EXIT; if( $cart->get_cart_contents_count() == 1 ) return; ## ------ Settings below ------- ## $PErcent = 5; // Percent rate: Progressive discount by steps of 5% $max_percentage = 50; // 50% (so for 10 items as 5 x 10 = 50) $discount_text = __( 'Quantity discount','woocommerce' ); // Discount Text ## ----- ----- ----- ----- ----- ## $cart_items_count = $cart->get_cart_contents_count(); $cart_lines_total = $cart->get_suBTotal() - $cart->get_discount_total(); // Dynamic percentage calculation $percentage = $percent * ($cart_items_count - 1); // Progressive discount From 5% to 45% (Between 2 and 10 items) if( $percentage < $max_percentage ) { $discount_text .= ' (' . $percentage . '%)'; $discount = $cart_lines_total * $percentage / 100; $cart->add_fee( $discount_text,-$discount ); } // Fixed discount at 50% (11 items and more) else { $discount_text .= ' (' . $max_percentage . '%)'; $discount = $cart_lines_total * $max_percentage / 100; $cart->add_fee( $discount_text,-$discount ); } }
代码放在活动子主题的function.PHP文件中.经过测试和工作.
参考文献:
> WooCommerce – Adding shipping fee for free user plan
> WooCommerce – Make a set of coupons adding a fixed fee to an order
> WooCommerce class – WC_Cart – add_fee() method
以上是脚本宝典为你收集整理的php – 基于Woocommerce购物车项目计数的条件累进百分比折扣全部内容,希望文章能够帮你解决php – 基于Woocommerce购物车项目计数的条件累进百分比折扣所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。