我正在使用创建帖子(可变产品)(wp_insert_post
) 作用例如,我有9种颜色,9种尺寸。这使得1个产品的总变化量为9×9=81,比要执行的功能低81倍。
function create_product_variation( $product_id, $variation_data){
$product = wc_get_product($product_id);
$variation_post = array(
\'post_title\' => $product->get_title(),
\'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 )
{
//Only have 2 attributes, size and color.
$taxonomy = \'pa_\'.$attribute;
update_post_meta( $variation_id, \'attribute_\'.$taxonomy, $term_name );
}
// Prices
$variation->set_price( $variation_data[\'regular_price\'] );
$variation->set_regular_price( $variation_data[\'regular_price\'] );
$variation->set_image_id($variation_data[\'variation_thumbnail_id\']);
$variation->save(); // Save the data
}
在运行上述代码之前,我将循环封装如下:
$productColors = array("siyah","kirmizi","bordo","haki","beyaz","antrasit","gri-kircilli","sari","lacivert","acik-mavi");
$Sizes = array("5xl","4xl","3xl","xxl","xl","l","m","s","xs");
wp_defer_term_counting( true ); //Speeding Up Bulk Update Tricks
wp_defer_comment_counting( true ); //Speeding Up Bulk Update Tricks
foreach ($Sizes as $size){ //Create each variation
foreach($productColors as $color){
$existingVarId = $wpdb->get_col($wpdb->prepare( "SELECT child.post_id
FROM wp_postmeta AS child
INNER JOIN wp_postmeta AS parent
ON child.post_id = parent.post_id
WHERE child.meta_value = %s and parent.meta_value = %s
and child.post_id in (select id from wp_posts where post_type = \'product_variation\' and post_parent = %d)", array( $size,$color,$post_id )));
if(!isset($existingVarId[0]))
{
$varCount++;
if (in_array($size, $oversize))
{
/* SKIP Beyaz - Kırmızı - Oversize*/
switch ($model) {
case "Kadın Tişört": $price = 49;break;
case "Fermuarlı Kapşonlu Sweatshirt":$price = 134;break;
case "Kapşonlu Sweatshirt":$price = 119;break;
case "Sweatshirt":$price = 109;break;
case "Atlet":$price = 49;break;
case "Tişört":$price = 65;break;
}
}
else
{
switch ($model) {
case "Kadın Tişört":$price = 49;break;
case "Fermuarlı Kapşonlu Sweatshirt":$price = 108;break;
case "Kapşonlu Sweatshirt":$price = 94;break;
case "Sweatshirt":$price = 84;break;
case "Atlet":$price = 49;break;
case "Tişört":$price = 49;break;
}
}
$variation_data = array(
\'attributes\' => array(
\'beden\' => $size,
\'renk\' => $color,
),
\'regular_price\' => $price,
\'variation_thumbnail_id\' => $productColorsAndIDs[$color],
);
create_product_variation( $post_id, $variation_data);
}
}
}
wp_defer_term_counting( false); //Speeding Up Bulk Update Tricks
wp_defer_comment_counting( false ); //Speeding Up Bulk Update Tricks
创建过程每时每刻都在变慢,即使我正在进行快速托管(SiteGround GoGeek托管计划)上面的代码在1-2分钟内创建了一个产品,这相当慢,而且大多数情况下,我在运行gateway 504时会遇到错误。
如何优化它以更快地工作?推迟似乎一点也不起作用。