WooCommerce自定义库存状态

时间:2016-09-07 12:23:02

标签: php wordpress woocommerce

我正在尝试覆盖woocommerce_product_is_in_stock过滤器/钩子,我已经找到了这个答案: Additional stock options in woocommerce

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”。

如何更改此代码,以便购物车允许我使用自己的自定义库存状态订购产品?

提前致谢!

1 个答案:

答案 0 :(得分:1)

我通过更新woocommerce中的woocommerce is_in_stock()函数来做到这一点 - >包括 - >摘要 - > abstract-wc-product.php

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';
    }
}