woocommerce_shipping_free_shipping_is_available不起作用

时间:2016-08-04 11:03:56

标签: php wordpress woocommerce shipping hook-woocommerce

我在WordPress安装中定制了WooCommerce。

我有类似的事情:

/**
 * Disable free shipping for select products
 *
 * @param bool $is_available
 */
function my_free_shipping( $is_available ) {
    global $woocommerce;

    // set the product ids that are ineligible
    $ineligible = array( '14009', '14031' );

    // get cart contents
    $cart_items = $woocommerce->cart->get_cart();

    // loop through the items looking for one in the ineligible array
    foreach ( $cart_items as $key => $item ) {
        if( in_array( $item['product_id'], $ineligible ) ) {
            return false;
        }
    }

    // nothing found return the default value
    return $is_available;
}
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'my_free_shipping', 20 );

更新WordPress和WooCommerce,我发现过滤器“woocommerce_shipping_free_shipping_is_available”永远不会被触发。

怎么了?

我发现 woocommerce_available_shipping_methods 过滤器已被弃用,现在我们必须使用 woocommerce_package_rates

woocommerce_shipping_free_shipping_is_available 是否也被弃用,或者我的安装遇到了问题?

更新

最后我使用了两个钩子: woocommerce_shipping_free_shipping_is_available woocommerce_package_rates

似乎 woocommerce_shipping_free_shipping_is_available 挂钩不被弃用。

这是我的代码:

add_filter('woocommerce_shipping_free_shipping_is_available', array( $this, 'enable_free_shipping'), 40, 1);            
if ( version_compare( WOOCOMMERCE_VERSION, '2.1', '>' ) ){
  add_filter('woocommerce_package_rates', array( $this, 'free_shipping_filter'), 10, 1);
}else{
  add_filter('woocommerce_available_shipping_methods', array( $this, 'free_shipping_filter'), 10, 1);
}

function enable_free_shipping($is_available){
    global $woocommerce;
    if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
        /* some check here to enable or not free shipping */
        return $is_available;
    }else{
        return $is_available;
    }
}

function free_shipping_filter( $available_methods )
{
    foreach ($available_methods as $key => $method) {
        if ($method->method_id == 'free_shipping'){
            $available_methods = array();
            $available_methods['free_shipping:1'] = $method;
            break;                      
        }
    }
    return $available_methods;
}

enable_free_shipping
是否启用免费送货方式

free_shipping_filter
仅显示免费送货方式(如果已启用)

钩子不能正常工作的原因是因为必须仅为有效的优惠券启用免费送货方式。通过这种方式,普通用户看不到“免费送货方式”,但我可以通过挂钩woocommerce_shipping_free_shipping_is_available启用它。

0 个答案:

没有答案
相关问题