0

I can't figure this one out. I've written the following function which updates the stock quantity of a particular item:

public function update_stock( $sku, $quantity ) {
    $id      = $this->get_product_id( $sku );
    $product = wc_get_product( $id );

    if ( $quantity < 0 ) {
        $quantity = 0;
    }

    if ( $product != null ) {
        $product->set_stock_quantity( $quantity );
        if ( $quantity === 0 ) {
            $product->set_stock_status( 'outofstock' );
        } else {
            $product->set_stock_status( 'instock' );
        }
        $product->save();
    }
}

However, calling the product save() function at the end returns a strange error which seems unrelated:

Notice: Trying to get property 'name' of non-object in /opt/lampp/htdocs/wordpress/wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php on line 1488

Here's what you can find in class-wc-product-data-store-cpt.php on line 1488:

public function get_product_type( $product_id ) {
    $post_type = get_post_type( $product_id );
    if ( 'product_variation' === $post_type ) {
        return 'variation';
    } elseif ( 'product' === $post_type ) {
        $terms = get_the_terms( $product_id, 'product_type' );
        return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; //LINE 1488
    } else {
        return false;
    }
}

Any help would be greatly appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.