Robert
Robert

Reputation: 39

Woocommerce 3.7: Set Product to Out of Stock but it doesn't show in report

I set a product to out of stock (OOS) programatically below, it appears fine on Woocommerce>Products and on the front end, however it doesn't show up on the report Woocommerce>Reports>Stock>Out of Stock

However, If I edit a product manually and set its stock status to out of stock and check the Manage Stock box, the product will display in the above OOS report.

// Update stock status

update_post_meta($product_id, '_stock_status', 'outofstock');

// Update manage stock

update_post_meta($product_id, '_manage_stock', yes);

// Update stock quantity

update_post_meta($product_id, '_stock', 0);

// Update post term relationship

wp_set_post_terms($product_id, 'outofstock', 'product_visibility', true);

Any idea how to get it to show on the report?

Upvotes: 0

Views: 1604

Answers (1)

itzmekhokan
itzmekhokan

Reputation: 2770

Try to do your modifications as follows -

$product = wc_get_product( $product_id );
    // Update stock status
    $product->set_stock_status('outofstock');
    // Update manage stock
    $product->set_manage_stock('yes');
    // Update stock quantity
    $product->set_stock_quantity(0);
    // Update post term relationship
    $product->set_catalog_visibility('hidden');

    // Save the data and refresh caches
    $product->save();

Upvotes: 1

Related Questions