为 WooCommerce 中销售的产品启用免费送货 [英] Enable free shipping for products on sale in WooCommerce

查看:51
本文介绍了为 WooCommerce 中销售的产品启用免费送货的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WooCommerce 中,是否可以自动将免费送货应用于任何在售产品?

In WooCommerce, is it possible to automatically apply free shipping to any product that is on sale?

每个月我们都有不同的产品在促销,所有促销产品自动符合免费送货条件.对于销售产品,我目前必须手动将运输类别更改为免费送货";然后回到标准运输"当销售结束时.我想自动执行此操作,以便任何正在销售的产品自动符合订单免运费的条件.

Every month we have different products on sale, and all sale products automatically qualify for free shipping. For sale products I currently have to manually change the shipping class to "free shipping" and then back to "standard shipping" when the sale is over. I would like to automate this so any product that is on sale automatically qualifies the order for free shipping.

我可以为每个产品 ID 申请免运费,但我无法弄清楚将其应用于销售产品.

I can apply free shipping per product ID, but I have not been able to figure out applying this to sale products.

function wcs_my_free_shipping( $is_available ) {
    global $woocommerce;
 
    // set the product ids that are eligible
    $eligible = array( '360' );
 
    // get cart contents
    $cart_items = $woocommerce->cart->get_cart();

    // loop through the items looking for one in the eligible array
    foreach ( $cart_items as $key => $item ) {
        if( in_array( $item['product_id'], $eligible ) ) {
            return true;
        }
    }
 
    // nothing found return the default value
    return $is_available;
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'wcs_my_free_shipping', 20 );

推荐答案

要提供免费送货服务,您可以使用 is_on_sale();

To make free shipping available, you could use is_on_sale();

function filter_woocommerce_shipping_free_shipping_is_available( $is_available, $package, $shipping_method ) {  
    // Loop through cart items
    foreach( $package['contents'] as $cart_item ) {
        // On sale
        if ( $cart_item['data']->is_on_sale() ) {
            // True
            $is_available = true;
            
            // Notice
            $notice = __( 'free shipping', 'woocommerce' );
            
            // Break loop
            break;
        }
    }
    
    // Display notice
    if ( isset( $notice ) ) {
        wc_add_notice( $notice, 'notice' );
    }
 
    // Return
    return $is_available;
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'filter_woocommerce_shipping_free_shipping_is_available', 10, 3 );

可选:在免费送货时隐藏其他送货方式

Optional: Hide other shipping methods when free shipping is available

function filter_woocommerce_package_rates( $rates, $package ) {
    // Empty array
    $free = array();

    // Loop trough
    foreach ( $rates as $rate_id => $rate ) {
        if ( $rate->method_id === 'free_shipping' ) {
            $free[ $rate_id ] = $rate;
            
            // Break loop
            break;
        }
    }
    
    return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );

这篇关于为 WooCommerce 中销售的产品启用免费送货的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆