Zau*_*ker 5 php wordpress shipping woocommerce 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 );
Run Code Online (Sandbox Code Playgroud)
更新 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;
}
Run Code Online (Sandbox Code Playgroud)
enable_free_shipping
启用或不启用免费送货方式
free_shipping_filter
仅显示免费送货方式(如果已启用)
挂钩不能正常工作的原因是因为必须仅对有效优惠券启用免费送货方式。这样普通用户看不到“免费送货方式”,但我可以通过钩子woocommerce_shipping_free_shipping_is_available启用它。
| 归档时间: |
|
| 查看次数: |
2013 次 |
| 最近记录: |