Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Force main product status to "instock" even when 0 stock
<?php
add_action( 'slw_product_wc_stock_status', function( $stock, $id ) {
if( ! empty( $id ) ) {
$product = wc_get_product( $id );
if( empty( $product ) ) return;
$parent_id = $product->get_parent_id();
if( $parent_id == 0 ) {
$locations_stock = \SLW\SRC\Helpers\SlwProductHelper::get_product_locations_stock_total( $parent_id );
if( $locations_stock == 0 ) {
update_post_meta( $parent_id, '_stock_status', 'instock' );
}
}
}
}, 10, 2 );
@alexmigf
Copy link
Author

alexmigf commented May 27, 2021

Instructions:

  1. Install the Code Snippets plugin: https://wordpress.org/plugins/code-snippets/
  2. Create a new snippet in the Code Snippets plugin and paste the code above without the <?php

@fahadmahmood8
Copy link

fahadmahmood8 commented Dec 17, 2021

This PHP code snippet has been added to the version 1.6.8 and added as an option on settings page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment