WC_Product_Variable::sync_stock_status PHP Method

sync_stock_status() public static method

Sync parent stock status with the status of all children and save.
public static sync_stock_status ( WC_Product | integer $product, boolean $save = true ) : WC_Product
$product WC_Product | integer Product object or ID for which you wish to sync.
$save boolean If true, the prouduct object will be saved to the DB before returning it.
return WC_Product Synced product object.
    public static function sync_stock_status($product, $save = true)
    {
        if (!is_a($product, 'WC_Product')) {
            $product = wc_get_product($product);
        }
        if (is_a($product, 'WC_Product_Variable')) {
            $data_store = WC_Data_Store::load('product-' . $product->get_type());
            $data_store->sync_stock_status($product);
            if ($save) {
                $product->save();
            }
        }
        return $product;
    }

Usage Example

 /**
  * Bulk edit
  * @param integer $post_id
  * @param WC_Product $product
  */
 public function bulk_edit_save($post_id, $product)
 {
     $old_regular_price = $product->regular_price;
     $old_sale_price = $product->sale_price;
     // Save fields
     if (!empty($_REQUEST['change_weight']) && isset($_REQUEST['_weight'])) {
         update_post_meta($post_id, '_weight', wc_clean(stripslashes($_REQUEST['_weight'])));
     }
     if (!empty($_REQUEST['change_dimensions'])) {
         if (isset($_REQUEST['_length'])) {
             update_post_meta($post_id, '_length', wc_clean(stripslashes($_REQUEST['_length'])));
         }
         if (isset($_REQUEST['_width'])) {
             update_post_meta($post_id, '_width', wc_clean(stripslashes($_REQUEST['_width'])));
         }
         if (isset($_REQUEST['_height'])) {
             update_post_meta($post_id, '_height', wc_clean(stripslashes($_REQUEST['_height'])));
         }
     }
     if (!empty($_REQUEST['_tax_status'])) {
         update_post_meta($post_id, '_tax_status', wc_clean($_REQUEST['_tax_status']));
     }
     if (!empty($_REQUEST['_tax_class'])) {
         $tax_class = wc_clean($_REQUEST['_tax_class']);
         if ('standard' == $tax_class) {
             $tax_class = '';
         }
         update_post_meta($post_id, '_tax_class', $tax_class);
     }
     if (!empty($_REQUEST['_stock_status'])) {
         $stock_status = wc_clean($_REQUEST['_stock_status']);
         if ($product->is_type('variable')) {
             foreach ($product->get_children() as $child_id) {
                 if ('yes' !== get_post_meta($child_id, '_manage_stock', true)) {
                     wc_update_product_stock_status($child_id, $stock_status);
                 }
             }
             WC_Product_Variable::sync_stock_status($post_id);
         } else {
             wc_update_product_stock_status($post_id, $stock_status);
         }
     }
     if (!empty($_REQUEST['_shipping_class'])) {
         $shipping_class = '_no_shipping_class' == $_REQUEST['_shipping_class'] ? '' : wc_clean($_REQUEST['_shipping_class']);
         wp_set_object_terms($post_id, $shipping_class, 'product_shipping_class');
     }
     if (!empty($_REQUEST['_visibility'])) {
         if (update_post_meta($post_id, '_visibility', wc_clean($_REQUEST['_visibility']))) {
             do_action('woocommerce_product_set_visibility', $post_id, wc_clean($_REQUEST['_visibility']));
         }
     }
     if (!empty($_REQUEST['_featured'])) {
         if (update_post_meta($post_id, '_featured', stripslashes($_REQUEST['_featured']))) {
             delete_transient('wc_featured_products');
         }
     }
     // Sold Individually
     if (!empty($_REQUEST['_sold_individually'])) {
         if ($_REQUEST['_sold_individually'] == 'yes') {
             update_post_meta($post_id, '_sold_individually', 'yes');
         } else {
             update_post_meta($post_id, '_sold_individually', '');
         }
     }
     // Handle price - remove dates and set to lowest
     if ($product->is_type('simple') || $product->is_type('external')) {
         $price_changed = false;
         if (!empty($_REQUEST['change_regular_price'])) {
             $change_regular_price = absint($_REQUEST['change_regular_price']);
             $regular_price = esc_attr(stripslashes($_REQUEST['_regular_price']));
             switch ($change_regular_price) {
                 case 1:
                     $new_price = $regular_price;
                     break;
                 case 2:
                     if (strstr($regular_price, '%')) {
                         $percent = str_replace('%', '', $regular_price) / 100;
                         $new_price = $old_regular_price + round($old_regular_price * $percent, wc_get_price_decimals());
                     } else {
                         $new_price = $old_regular_price + $regular_price;
                     }
                     break;
                 case 3:
                     if (strstr($regular_price, '%')) {
                         $percent = str_replace('%', '', $regular_price) / 100;
                         $new_price = max(0, $old_regular_price - round($old_regular_price * $percent, wc_get_price_decimals()));
                     } else {
                         $new_price = max(0, $old_regular_price - $regular_price);
                     }
                     break;
                 default:
                     break;
             }
             if (isset($new_price) && $new_price != $old_regular_price) {
                 $price_changed = true;
                 $new_price = round($new_price, wc_get_price_decimals());
                 update_post_meta($post_id, '_regular_price', $new_price);
                 $product->regular_price = $new_price;
             }
         }
         if (!empty($_REQUEST['change_sale_price'])) {
             $change_sale_price = absint($_REQUEST['change_sale_price']);
             $sale_price = esc_attr(stripslashes($_REQUEST['_sale_price']));
             switch ($change_sale_price) {
                 case 1:
                     $new_price = $sale_price;
                     break;
                 case 2:
                     if (strstr($sale_price, '%')) {
                         $percent = str_replace('%', '', $sale_price) / 100;
                         $new_price = $old_sale_price + $old_sale_price * $percent;
                     } else {
                         $new_price = $old_sale_price + $sale_price;
                     }
                     break;
                 case 3:
                     if (strstr($sale_price, '%')) {
                         $percent = str_replace('%', '', $sale_price) / 100;
                         $new_price = max(0, $old_sale_price - $old_sale_price * $percent);
                     } else {
                         $new_price = max(0, $old_sale_price - $sale_price);
                     }
                     break;
                 case 4:
                     if (strstr($sale_price, '%')) {
                         $percent = str_replace('%', '', $sale_price) / 100;
                         $new_price = max(0, $product->regular_price - $product->regular_price * $percent);
                     } else {
                         $new_price = max(0, $product->regular_price - $sale_price);
                     }
                     break;
                 default:
                     break;
             }
             if (isset($new_price) && $new_price != $old_sale_price) {
                 $price_changed = true;
                 $new_price = !empty($new_price) || '0' === $new_price ? round($new_price, wc_get_price_decimals()) : '';
                 update_post_meta($post_id, '_sale_price', $new_price);
                 $product->sale_price = $new_price;
             }
         }
         if ($price_changed) {
             update_post_meta($post_id, '_sale_price_dates_from', '');
             update_post_meta($post_id, '_sale_price_dates_to', '');
             if ($product->regular_price < $product->sale_price) {
                 $product->sale_price = '';
                 update_post_meta($post_id, '_sale_price', '');
             }
             if ($product->sale_price) {
                 update_post_meta($post_id, '_price', $product->sale_price);
             } else {
                 update_post_meta($post_id, '_price', $product->regular_price);
             }
         }
     }
     // Handle stock
     if (!$product->is_type('grouped')) {
         if (!empty($_REQUEST['change_stock'])) {
             update_post_meta($post_id, '_manage_stock', 'yes');
             wc_update_product_stock($post_id, wc_stock_amount($_REQUEST['_stock']));
         }
         if (!empty($_REQUEST['_manage_stock'])) {
             if ($_REQUEST['_manage_stock'] == 'yes') {
                 update_post_meta($post_id, '_manage_stock', 'yes');
             } else {
                 update_post_meta($post_id, '_manage_stock', 'no');
                 wc_update_product_stock($post_id, 0);
             }
         }
         if (!empty($_REQUEST['_backorders'])) {
             update_post_meta($post_id, '_backorders', wc_clean($_REQUEST['_backorders']));
         }
     }
     do_action('woocommerce_product_bulk_edit_save', $product);
 }
All Usage Examples Of WC_Product_Variable::sync_stock_status