【问题标题】:Remove next day shipping method for out of stock items in Woocommerce删除 Woocommerce 中缺货商品的次日发货方式
【发布时间】:2013-11-11 20:01:08
【问题描述】:
我使用表格运费插件设置了 4 个送货区域,每个区域都有标准和次日送货选项。
商店中的每件商品都将在 10-15 天内有货,但一些产品有库存,可在第二天发货。如果购物车中的任何产品都没有库存,则应该只有标准运输选项可用。
我相当确定我需要使用 'woocommerce_available_shipping_methods' 过滤器,如这里类似的问题/答案所示:Hide Shipping Options Woocommerce,但是我完全不知道如何检查每个购物车的库存水平物品。
任何指针将不胜感激。
【问题讨论】:
标签:
wordpress
woocommerce
shipping
【解决方案1】:
我现在已经解决了这个问题。
我的解决方案绝对不适合所有人,但它可能会对某人有所帮助。
我正在为 woocommerce 使用这个表格运费插件:http://codecanyon.net/item/table-rate-shipping-for-woocommerce/3796656?WT(如果你想复制它的工作方式)
感谢这个问题的答案:Hide Shipping Options Woocommerce
代码基本上是从那里复制的,但设置为检查产品的库存数量。
此代码不考虑是否允许延期交货,它实际上检查库存数量是否 > 0 并且购物车数量
/* !Hide Shipping Options Woocommerce */
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_quantity' , 10, 1 );
function check_cart_for_oos() {
// load the contents of the cart into an array.
global $woocommerce;
$found = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$_product_quantity = $_product->get_stock_quantity();
$_cart_quantity = $values['quantity'];
if (($_product_quantity <= 0) || ($_cart_quantity > $_product_quantity)) {
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_quantity( $available_methods ) {
// use the function check_cart_for_oos() to check the cart for products with 0 stock.
if ( check_cart_for_oos() ) {
// remove the rate you want
unset( $available_methods['table_rate_shipping_next-day'] ); // Replace "table_rate_shipping_next-day" with "table_rate_shipping_your-identifier".
}
// return the available methods without the one you unset.
return $available_methods;
}