Woocommerce - How to add a product change from an external script

I have a variable with the taxonomy pa_size attribute set with the following values: S | M | L | XL

How to add changes for this product using an external php script?

Thanks in advance.

I am using the following code:

    $sku = 21333;
    $size = 'S';
    $stock = 2;
    $price_a = 60;
    $price_b = 30;

    $product_parent = get_product_by_sku($sku);
    $product = new WC_Product_Variable($product_parent->id);
    $variations = $product->get_available_variations();

    // First off all delete all variations
    foreach($variations as $prod_variation) {
      $metaid=mysql_query("SELECT meta_id FROM wp_postmeta WHERE post_id = ".$prod_variation['variation_id']);
      while ($row = mysql_fetch_assoc($metaid)) {
        mysql_query("DELETE FROM wp_postmeta WHERE meta_id = ".$row['meta_id']);
      }
      mysql_query("DELETE FROM wp_posts WHERE ID = ".$prod_variation['variation_id']);
    }

    // Now add new variation
    $thevariation = array(
      'post_title'=> '',
      'post_name' => 'product-' . $product_parent->id . '-variation',
      'post_status' => 'publish',
      'post_parent' => $product_parent->id,
      'post_type' => 'product_variation',
      'guid'=>home_url() . '/?product_variation=product-' . $product_parent->id . '-variation'
    );
    $variation_id = wp_insert_post( $thevariation );
    update_post_meta($variation_id, 'post_title', 'Variation #' . $variation_id . ' of '. $product_parent->id);

    wp_set_object_terms( $variation_id, $size, 'pa_size' );
    update_post_meta($variation_id, 'attribute_pa_size', $size);

    update_post_meta($variation_id, '_regular_price', $price_a);
    update_post_meta($variation_id, '_downloadable_files', '');
    update_post_meta($variation_id, '_download_expiry', '');
    update_post_meta($variation_id, '_download_limit', '');
    update_post_meta($variation_id, '_sale_price_dates_to', '');
    update_post_meta($variation_id, '_sale_price_dates_from', '');
    update_post_meta($variation_id, '_backorders', 'no');
    update_post_meta($variation_id, '_stock_status', 'instock');
    update_post_meta($variation_id, '_height', '');
    update_post_meta($variation_id, '_manage_stock', 'yes');
    update_post_meta($variation_id, '_width', '');
    update_post_meta($variation_id, '_sale_price_dates_from', '');
    update_post_meta($variation_id, '_backorders', 'no');
    update_post_meta($variation_id, '_stock_status', 'instock');
    update_post_meta($variation_id, '_manage_stock', 'yes');
    update_post_meta($variation_id, '_height', '');
    update_post_meta($variation_id, '_width', '');
    update_post_meta($variation_id, '_length', '');
    update_post_meta($variation_id, '_weight', '');
    update_post_meta($variation_id, '_downloadable', 'no');
    update_post_meta($variation_id, '_virtual', 'no');
    update_post_meta($variation_id, '_thumbnail_id', '0');
    update_post_meta($variation_id, '_sku', '');

    update_post_meta($variation_id, '_sale_price', $price_b);
    update_post_meta($variation_id, '_price', $price_b);
    update_post_meta($product_parent->id, '_min_variation_price', $price_b);
    update_post_meta($product_parent->id, '_max_variation_price', $price_b);
    update_post_meta($product_parent->id, '_min_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_max_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_min_variation_regular_price', $price_a);
    update_post_meta($product_parent->id, '_max_variation_regular_price', $price_a);
    update_post_meta($product_parent->id, '_min_regular_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_max_regular_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_min_variation_sale_price', $price_b);
    update_post_meta($product_parent->id, '_max_variation_sale_price', $price_b);
    update_post_meta($product_parent->id, '_min_sale_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_max_sale_price_variation_id', $variation_id);
    update_post_meta($product_parent->id, '_price', $price_b);

    update_post_meta( $variation_id, '_stock', $stock );
    update_post_meta($product_parent->id, 'post_status', 'publish');
+4
source share
1 answer

, save_post, , , , . WooCommerce woocommerce_process_product_meta, save_post, , , , .. hook, , . , .

add_action( 'woocommerce_process_product_meta', 'so_27902470_update_variations', 10, 2 );
function so_27902470_update_variations( $post_id, $post ){
    if( isset( $post['product_type'] ) && 'variable' == $post['product_type'] ){
        // do your stuff
    }
    return $post_id;
}
+5

Source: https://habr.com/ru/post/1570870/


All Articles