加速批量发布创建-wp_ins_post和update_post_meta

时间:2019-01-16 作者:HOY

我正在使用创建帖子(可变产品)(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时会遇到错误。

如何优化它以更快地工作?推迟似乎一点也不起作用。

1 个回复
SO网友:Svetoslav Marinov

Siteground安装了WP-CLI,当您安装了WooCommerce时,它会添加自己的一组WP-CLI命令。请参见:https://github.com/woocommerce/woocommerce/wiki/WC-CLI-Overview

这样,您的解决方案将更加干净,因为它将通过WC的API。

也许你也可以看看WC是如何做到这一点的。我记得在添加/编辑产品UI中有一个链接可以(重新)生成变体,但我认为这是有限制的。

相关推荐

Performance on WPMS

我的WPMS站点托管在8核/32mb RAM服务器上,但响应时间非常长。我们有大约1000个博客(单个db上有35000多个表)和70000个页面浏览量。我认为我可以缩短响应时间,将具有更多页面浏览量的博客移动到单独的DB中,并使用hyper DB插件将所有博客拆分为每个DB 100个博客。你觉得怎么样?