0
votes

I am trying to overwrite the woocommerce_product_is_in_stock filter/hook, I have already found this answer: 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;
}

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.

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!

1

1 Answers

1
votes

I did it by updating woocommerce is_in_stock() function in woocommerce -> includes -> abstracts -> 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';
    }
}