WooCommerceで「送料無料が有効な場合に他の配送オプションを表示しないようにする」覚え書き

業務用覚え書きです。

If you’ve defined a few flat rate shipping methods (say a standard, expedited, and next day rate) and want to hide the standard shipping method when free shipping is available, you can use something like this in your functions.php:

// Hide standard shipping option when free shipping is available
add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );

/**
 *  Hide Standard Shipping option when free shipping is available
 * 
 * @param array $available_methods
 */
function hide_standard_shipping_when_free_is_available( $available_methods ) {

    if( isset( $available_methods['free_shipping'] ) AND isset( $available_methods['flat_rate'] ) ) {

        // remove standard shipping option
        unset( $available_methods['flat_rate'] );
    }

    return $available_methods;
}

I’ve used this when customers were selecting and paying for standard shipping even though free shipping was available and selected by default.

詳細はこちら:

https://github.com/woothemes/woocommerce/issues/1499

 

もしくは、複数の配送方法がある場合、すべてを非表示にする方法

 

/**
* woocommerce_package_rates is a 2.1+ hook
*/
add_filter( ‘woocommerce_package_rates’, ‘hide_shipping_when_free_is_available’, 10, 2 );
/**
* Hide shipping rates when free shipping is available
*
* @param array $rates Array of rates found for the package
* @param array $package The package array/object being shipped
* @return array of modified rates
*/
function hide_shipping_when_free_is_available( $rates, $package ) {
// Only modify rates if free_shipping is present
if ( isset( $rates[‘free_shipping’] ) ) {
// To unset a single rate/method, do the following. This example unsets flat_rate shipping
unset( $rates[‘flat_rate’] );
// To unset all methods except for free_shipping, do the following
$free_shipping = $rates[‘free_shipping’];
$rates = array();
$rates[‘free_shipping’] = $free_shipping;
}
return $rates;
}

http://docs.woothemes.com/document/hide-other-shipping-methods-when-free-shipping-is-available/

コメントを残す