简体   繁体   English

WooCommerce 自定义库存状态

[英]WooCommerce custom stock status

I am trying to overwrite the woocommerce_product_is_in_stock filter/hook, I have already found this answer: Additional stock options in woocommerce我正在尝试覆盖 woocommerce_product_is_in_stock 过滤器/挂钩,我已经找到了这个答案: 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;
}

But that only helps me partway, this does return the product as in stock for most instances but whenever I try to view the cart the product cannot be found.但这只会对我有所帮助,在大多数情况下,这确实将产品退回到库存中,但是每当我尝试查看购物车时,都找不到产品。 The global $product seems to be "NULL" whenever I var_dump it.每当我 var_dump 时,全局 $product 似乎都是“NULL”。

How can I change this code so that the cart will allow me to order the product with my own custom stock status?如何更改此代码,以便购物车允许我使用自己的自定义库存状态订购产品?

Thanks in advance!提前致谢!

I did it by updating woocommerce is_in_stock() function in woocommerce -> includes -> abstracts -> abstract-wc-product.php我是通过更新 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';
}

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM