WC_Product_Variable_Data_Store_CPT::sync_managed_variation_stock_status()publicWC 3.0.0

Stock managed at the parent level - update children being managed by this product. This sync function syncs downwards (from parent to child) when the variable product is saved.

Method of the class: WC_Product_Variable_Data_Store_CPT{}

No Hooks.

Return

null. Nothing (null).

Usage

$WC_Product_Variable_Data_Store_CPT = new WC_Product_Variable_Data_Store_CPT();
$WC_Product_Variable_Data_Store_CPT->sync_managed_variation_stock_status( $product );
$product(WC_Product) (required) (passed by reference — &)
Product object.

Changelog

Since 3.0.0 Introduced.

WC_Product_Variable_Data_Store_CPT::sync_managed_variation_stock_status() code WC 9.4.2

public function sync_managed_variation_stock_status( &$product ) {
	global $wpdb;

	if ( $product->get_manage_stock() ) {
		$children = $product->get_children();
		$changed  = false;

		if ( $children ) {
			$status           = $product->get_stock_status();
			$format           = array_fill( 0, count( $children ), '%d' );
			$query_in         = '(' . implode( ',', $format ) . ')';
			$managed_children = array_unique( $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_manage_stock' AND meta_value != 'yes' AND post_id IN {$query_in}", $children ) ) ); // @codingStandardsIgnoreLine.
			foreach ( $managed_children as $managed_child ) {
				if ( update_post_meta( $managed_child, '_stock_status', $status ) ) {
					$this->update_lookup_table( $managed_child, 'wc_product_meta_lookup' );
					$changed = true;
				}
			}
		}

		if ( $changed ) {
			$children = $this->read_children( $product, true );
			$product->set_children( $children['all'] );
			$product->set_visible_children( $children['visible'] );
		}
	}
}