<?php
/**
* Template Name: Woo Test
*/
get_header();
?>
<?php
	
	
	$args = array(
    'post_type'     => 'product_variation',
    'post_status'   => array( 'private', 'publish' ),
    'numberposts'   => -1,
    'orderby'       => 'menu_order',
    'order'         => 'asc',
    'post_parent'   => 1954 // get parent post-ID
);
$variations = get_posts( $args );

foreach ( $variations as $variation ) {

    // get variation ID
    echo $variation_ID = $variation->ID;
	echo " | ";
    // get variations meta
    $product_variation = new WC_Product_Variation( $variation_ID );

    // get variation featured image
  //  echo $variation_image = $product_variation->get_image();
//	echo " | ";

    // get variation price
    echo $variation_price = $product_variation->get_price_html();


    get_post_meta( $variation_ID , '_text_field_date_expire', true );
		echo " <br /> ";

}
	





	// AAAA
	echo "<hr >";
	
	
	$product_variable = new WC_Product_Variable(1954);
	$product_variations = $product_variable->get_available_variations();
	echo $variation_price = $product_variation->get_price_html()." | ";

	foreach ($product_variations as $variation)  {
		print_r ($variation['attributes'])." | ";
		echo $variation['attributes']['attribute_pa_color']." | "; 
  		echo $variation['max_qty']." | ";
		echo $variation['is_in_stock']." | ";
		echo $variation['regular_price']." | ";
		echo "<br />";
	}




//	AAAA
	echo "<hr >";
	$product_id = 1954;
	$variance_id = 1956;
	$get_stocks = $woocommerce->get('products/'.$product_id);
	$stock_quantity_old = $get_stocks["stock_quantity"];
	print_r ($stock_quantity_old);
	$data = [
			'manage_stock' => true,
			'stock_quantity' => $stock_quantity_old - 1
		];
	// $result = $woocommerce->put('products/'.$product_id.'/variations/'.$variance_id, $data);
	
// AAA
	
	
	$children = get_posts( array(
    'post_parent'   => 1954,
    'posts_per_page'=> -1,
    'post_type'   => 'product_variation',
    'post_status'  => 'publish'
) );

$out_of_stock_staus = 'outofstock';

foreach ( $children as $thischild ) {
	
	echo $thischild->ID;
	echo "<br />";
//	update_post_meta( $thischild->ID, '_max_qty', 33);
//    update_post_meta( $thischild->ID, '_stock_status', wc_clean( $out_of_stock_staus ) );
}



function create_product_variation( $product_id, $variation_data ){
    // Get the Variable product object (parent)
    $product = wc_get_product($product_id);

    $variation_post = array(
        'post_title'  => $product->get_name(),
        'post_name'   => 'product-'.$product_id.'-variation',
        'post_status' => 'publish',
        'post_parent' => $product_id,
        'post_type'   => 'product_variation',
        'guid'        => $product->get_permalink()
    );

    // Creating the product variation
    $variation_id = wp_insert_post( $variation_post );

    // Get an instance of the WC_Product_Variation object
    $variation = new WC_Product_Variation( $variation_id );

    // Iterating through the variations attributes
    foreach ($variation_data['attributes'] as $attribute => $term_name )
    {
        $taxonomy = 'pa_'.$attribute; // The attribute taxonomy

        // If taxonomy doesn't exists we create it (Thanks to Carl F. Corneil)
        if( ! taxonomy_exists( $taxonomy ) ){
            register_taxonomy(
                $taxonomy,
               'product_variation',
                array(
                    'hierarchical' => false,
                    'label' => ucfirst( $attribute ),
                    'query_var' => true,
                    'rewrite' => array( 'slug' => sanitize_title($attribute) ), // The base slug
                ),
            ),
        }

        // Check if the Term name exist and if not we create it.
        if( ! term_exists( $term_name, $taxonomy ) )
            wp_insert_term( $term_name, $taxonomy ); // Create the term

        $term_slug = get_term_by('name', $term_name, $taxonomy )->slug; // Get the term slug

        // Get the post Terms names from the parent variable product.
        $post_term_names =  wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );

        // Check if the post term exist and if not we set it in the parent variable product.
        if( ! in_array( $term_name, $post_term_names ) )
            wp_set_post_terms( $product_id, $term_name, $taxonomy, true );

        // Set/save the attribute data in the product variation
        update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
    }

    ## Set/save all other data

    // SKU
    if( ! empty( $variation_data['sku'] ) )
        $variation->set_sku( $variation_data['sku'] );

    // Prices
    if( empty( $variation_data['sale_price'] ) ){
        $variation->set_price( $variation_data['regular_price'] );
    } else {
        $variation->set_price( $variation_data['sale_price'] );
        $variation->set_sale_price( $variation_data['sale_price'] );
    }
    $variation->set_regular_price( $variation_data['regular_price'] );

    // Stock
    if( ! empty($variation_data['stock_qty']) ){
        $variation->set_stock_quantity( $variation_data['stock_qty'] );
        $variation->set_manage_stock(true);
        $variation->set_stock_status('');
    } else {
        $variation->set_manage_stock(false);
    }

    $variation->set_weight(''); // weight (reseting)

    $variation->save(); // Save the data
}
?>
<?php get_sidebar();
?>
<?php get_footer();
