php – 基于Woocommerce购物车项目计数的条件累进百分比折扣
我希望根据购物车中的商品数量获得有条件的累进折扣.将2个产品添加到购物车后,您将获得折扣.您添加的产品越多,折扣越多.
例如: > 1个产品 – 全价(无折扣) 我在互联网上搜索没有任何成功.当搜索折扣我只是落在WooCommerce优惠券功能或我得到一些旧错误的代码… 任何的想法?我该怎么做? 可能吗? 谢谢. 解决方法
是的,它可以使用一个技巧,来实现这一目标.通常我们在WooCommerce优惠券中使用的购物车折扣.这里的优惠券没有被挪用.我将在这里使用负面的按条件费,这将成为折扣. 计算: 代码: 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |