我们如何更新产品版本?
我们有这段代码可以创建产品变体,每次运行我们编写的代码时,它都会创建并再次创建变体。
如何检测产品是否存在,以便我们进行更新。
这是我们使用的代码。谢谢!
public function insert_product_variations ($post_id, $variations, $attribute_count, $tier_variations) {
$product = wc_get_product($product_id);
foreach ($variations as $product_variant) {
$variants = explode(',', $product_variant['name']);
$variation_post = array(
'post_title' => 'Variantion ' . $product_variant['name'] . ' for #' . $post_id,
'post_name' => 'product-'.$post_id.'-variation-'.$product_variant['name'],
'post_status' => 'publish',
'post_parent' => $post_id,
'post_type' => 'product_variation',
'guid' => home_url() . '/?product_variation=product-' . $post_id . '-variation-' . $product_variant['name']
);
$variation_post_id = wp_insert_post($variation_post);
echo 'Variantion ' . $product_variant['name'] . ' for #' . $post_id;
// Get an instance of the WC_Product_Variation object
$variation = new WC_Product_Variation( $variation_id );
for ($i = 0; $i < $attribute_count ; $i++) {
$tname=strtolower($tier_variations[$i]['name']);
//Replace Type word
$tname = str_replace('type', 'model', $tname);
$name = $tname;
//Remove Space
$name = str_replace(' ', '', $name);
$attribute_taxonomy = 'pa_' . strtolower ($name);
$attribute_term = get_term_by('name', str_replace('+', 'Plus', $variants[$i]) , $attribute_taxonomy);
update_post_meta($variation_post_id, 'attribute_' . $attribute_taxonomy , $attribute_term->slug);
}
//Adjust Price
$markup = 50;
$shopee_price = $product_variant['price'];
$price = $shopee_price / 100000;
$sale_price = $price + $markup;
$reg_price = $price + 100;
//END - Adjust Price
update_post_meta($variation_post_id, '_sale_price', $sale_price);
update_post_meta($variation_post_id, '_regular_price', $reg_price);
update_post_meta($variation_post_id, '_manage_stock', 'yes');
update_post_meta($variation_post_id, '_stock', $product_variant['stock']);
}
}