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.

Метод класса: WC_Product_Variable_Data_Store_CPT{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$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) (обязательный) (передается по ссылке — &)
Product object.

Список изменений

С версии 3.0.0 Введена.

Код WC_Product_Variable_Data_Store_CPT::sync_managed_variation_stock_status() WC 8.7.0

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'] );
		}
	}
}