Wordpress WooCommerce-买10件,免费获得3件-只有最便宜的3件是免费的
我有一位客户目前正在销售线条标记涂料,他们希望在其网站上提供以下方式的报价: 如果客户购买10+种油漆(他们可以混合搭配),他们将获得3种免费油漆,但只有最便宜的3种,免费 示例如下所示:Wordpress WooCommerce-买10件,免费获得3件-只有最便宜的3件是免费的,wordpress,woocommerce,hook-woocommerce,Wordpress,Woocommerce,Hook Woocommerce,我有一位客户目前正在销售线条标记涂料,他们希望在其网站上提供以下方式的报价: 如果客户购买10+种油漆(他们可以混合搭配),他们将获得3种免费油漆,但只有最便宜的3种,免费 示例如下所示: 购买10个x(99.99英镑)、1个y(20.99英镑)和2个z(30.99英镑)。客户应获得3个最便宜的免费,因此,在这种情况下,他们应同时获得z和y免费 购买14件x(99.99英镑)、2件y(20.00英镑)和2件z(30.99英镑)。在这种情况下,客户应免费获得y和z中的一个 我正在努力在WooC
- 购买10个x(99.99英镑)、1个y(20.99英镑)和2个z(30.99英镑)。客户应获得3个最便宜的免费,因此,在这种情况下,他们应同时获得z和y免费
- 购买14件x(99.99英镑)、2件y(20.00英镑)和2件z(30.99英镑)。在这种情况下,客户应免费获得y和z中的一个
function get_cheapest_x_products_in_cart($cat_id)
{
global $woocommerce;
$cat_products = [];
$cheapest_products;
// Add all cart items with correct category to array ($cat_products)
foreach( WC()->cart->get_cart() as $cart_item ) {
if( has_term( $cat_id, 'product_cat', $cart_item['product_id'])) {
$product = wc_get_product( $cart_item['product_id'] );
$price = $product->get_regular_price();
$cat_products[
$cart_item['product_id'] ] = [
'price' => floatval($price),
'quantity' => $cart_item['quantity'],
];
}
}
uasort($cat_products, "sort_this");
$cheapest_three_products = array_slice($cat_products, 0, 3, true);
return $cheapest_three_products;
}
在下面这个自定义函数中,挂接在
woocommerce\u cart\u calculate\u fees
操作挂钩中,客户将根据购物车中每10件商品的最便宜3件商品价格之和获得折扣
您必须定义一个产品类别,并且在应用折扣时,您可以选择获得一个自定义通知
代码如下:
add_action( 'woocommerce_cart_calculate_fees', 'free_cheapest_3_each_10_items', 10, 1 );
function free_cheapest_3_each_10_items( $wc_cart ) {
if ( is_admin() && ! defined('DOING_AJAX') ) return;
// HERE define your product category (or categories) in the array (IDs slugs or names)
$cat_id = array('paints');
$prices = array();
$cat_id = array('clothing');
$discount = $items_count = 0;
foreach ( $wc_cart->get_cart() as $cart_item ){
$sale_price = $cart_item['data']->get_sale_price();
// Only for the defined product category(ies) and no items in sale
if( has_term( $cat_id, 'product_cat', $cart_item['product_id']) && ( empty($sale_price) || $sale_price == 0 ) ) {
for( $i = 0; $i < $cart_item['quantity']; $i++){
$prices[] = floatval( $cart_item['data']->get_regular_price() );
$items_count++;
}
}
}
if( $items_count >= 10 ){
// Ordering prices
asort($prices);
// Get the occurence number for 3 free items each 10 items
for( $i = 0, $j = -1; $i < $items_count; $i++ )
if( $i % 10 == 0 ) $j++;
$count = $j*3;
// Get the 3 free items for each 10 items in cart (for the defined product category(ies))
$free_cheapest_items = array_slice($prices, 0, $count, true);
// Calculate the discount amount
foreach( $free_cheapest_items as $item_price )
$discount -= $item_price;
// The discount
if( $discount != 0 ){
$wc_cart->add_fee( "Bulk discount", $discount, true );
// Displaying a custom notice (optional)
wc_clear_notices();
wc_add_notice( __("You get $count free items for the $items_count items in cart"), 'notice');
}
}
}
add_action('woocommerce_cart_计算_费用','free_最便宜的_3_每个_10_项目',10,1);
功能免费\u最便宜\u 3\u每个\u 10\u项目($wc\u购物车){
if(is_admin()&&!defined('DOING_AJAX'))返回;
//在这里定义阵列中的产品类别(IDs slugs或名称)
$cat_id=数组('paints');
$prices=array();
$cat_id=数组(“衣服”);
$discount=$items\u count=0;
foreach($wc\u cart->get\u cart()作为$cart\u项目){
$sale_price=$cart_item['data']->get_sale_price();
//仅适用于已定义的产品类别,无在售商品
如果(有_项($cat_id,'product_cat',$cart_item['product_id'])和&(空($sale_price)| |$sale_price==0)){
对于($i=0;$i<$cart_项目['quantity'];$i++){
$prices[]=floatval($cart_item['data']->get_regular_price());
$items_count++;
}
}
}
如果($items\u count>=10){
//订购价格
asort(价格);
//获取每10个项目中3个免费项目的发生编号
对于($i=0,$j=-1;$i<$items\u count;$i++)
如果($i%10==0)$j++;
$count=$j*3;
//在购物车中,每10件商品可获得3件免费商品(针对已定义的产品类别)
$free\u cheapest\u items=array\u slice($prices,0,$count,true);
//计算折扣金额
foreach($免费\最便宜\商品作为$商品\价格)
$折扣-=$item\u价格;
//折扣
如果($折扣!=0){
$wc_购物车->添加_费用(“批量折扣”、$折扣,真);
//显示自定义通知(可选)
wc_清除通知();
wc_add_notice(uuuu(“购物车中的$items_count items可获得$count免费商品”),“notice”);
}
}
}
代码位于活动子主题(或主题)的function.php文件或任何插件文件中
在WooCommerce 3上测试,效果良好。您尝试过什么?你在为这项修改的哪一部分而挣扎?@proeviz对不起,我应该提供更多的信息。我正在努力解决的部分,是让最便宜的三种产品打折的功能,特别是当最便宜的三种产品是一种以上产品的组合时。。。如果这有意义的话?一个简单的
foreach()
循环和一个if
条件可以处理这个问题,从你的评论来看?嗨@proeviz,我已经在问题中添加了我的代码,希望这能提供更多信息。我真的很感激你的帮助,谢谢你,非常感谢!不过我有两个问题-在“//获取3个免费项目每10个项目的发生次数”中,该部分到底在做什么?我正在努力跟随那部分。。。还有-我只希望它应用一次,所以不是每10的倍数-我该如何实现呢?@Joshua所以只要更改$count=$j*3代码>至$count=3代码>…