我试图覆盖woocommerce_product_is_in_stock过滤器/钩子,我已经找到了这个答案:额外的股票期权
add_filter('woocommerce_product_is_in_stock', 'woocommerce_product_is_in_stock' );
function woocommerce_product_is_in_stock( $is_in_stock ) {
global $product;
// array of custom stock statuses that will have add to cart button
$stock_statuses = array('onrequest','preorder');
if (!$is_in_stock && in_array($product->stock_status, $stock_statuses )) {
$is_in_stock = true;
}
return $is_in_stock;
}
但这只对我有帮助,在大多数情况下,这确实会将产品作为库存返回,但每当我试图查看购物车时,都找不到产品。每当我var_dump全局$产品时,它似乎都是“NULL”。
如何更改此代码,以便购物车允许我以自己的自定义库存状态订购产品?
提前感谢!
我是通过更新woocommerce中的woocommerce is_in_stock()函数来实现的-
public function is_in_stock() {
if ( $this->managing_stock() && $this->backorders_allowed() ) {
return true;
} elseif ( $this->managing_stock() && $this->get_total_stock() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
return false;
} else {
return $this->stock_status === 'instock';
}
到
public function is_in_stock() {
$stock_statuses = array('onrequest','preorder');
if ($this->get_total_stock() > get_option( 'woocommerce_notify_no_stock_amount' ) && in_array($this->stock_status, $stock_statuses )) {
return true;
}
if ( $this->managing_stock() && $this->backorders_allowed() ) {
return true;
} elseif ( $this->managing_stock() && $this->get_total_stock() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
return false;
} else {
return $this->stock_status === 'instock';
}
}