kreamweb
2/20/2020 - 10:05 AM

[POS] Enable options multistock to all products

[POS] Enable options multistock to all products

<?php 


add_action( 'init', 'yith_pos_add_meta_to_products' );
function yith_pos_add_meta_to_products() {
	$option = get_option( 'yith_pos_stock_product_enabled', 'no' );

	if ( $option === 'no' ) {
		$products = wc_get_products( array( 'limit' => - 1 ) );
		if ( $products ) {
			foreach ( $products as $product ) {
				update_post_meta( $product->get_id(), '_yith_pos_multistock_enabled', 'yes' );
			}
		}
	}

	add_option( 'yith_pos_stock_product_enabled', 'yes' );
}


add_action( 'init', 'yith_pos_add_meta_to_product_variations' );
function yith_pos_add_meta_to_product_variations() {
	$option = get_option( 'yith_pos_stock_product_variation_enabled', 'no' );

	if ( $option === 'no' ) {
		$products = wc_get_products( array( 'limit' => -1, 'type' =>'variable' ) );

		if ( $products ) {
			foreach ( $products as $variable ) {
				$variations = $variable->get_available_variations();

				if( $variations ){
					foreach ( $variations as $variation ) {
						update_post_meta( $variation['variation_id'], '_yith_pos_multistock_enabled', 'yes' );
					}
				}
			}
		}
	}

	add_option( 'yith_pos_stock_product_variation_enabled', 'yes' );
}