首页 > 解决方案 > 页面执行被杀死

问题描述

我正在使用这段代码(结合我自己的):

https://www.sbloggers.com/add-a-woocommerce-product-using-custom-php-code-programmatically

此代码按预期工作,但是我有一个有很多变化的产品,并且由于某种原因该过程正在停止。

我有:

  1. 将 max_execution 时间设置为 600
  2. 发布 set_time_limit(0); 在脚本的顶部。

目前我只允许编写一种产品,它基于一个属性有 157 种变体。它正在将产品写入数据库,但有时它会写入 19 个变体,有时是 27 个,有时是 80 个。脚本在随机时间被终止。

谁能给我一些想法,以防止它被杀死?我希望脚本运行完成。

提前感谢您的想法/解决方案/想法。

我正在使用的编写变体的代码如下。它正在工作,但 Wordpress 页面在此例程完成之前完成加载。它在随机时间停止。

====================================


//Loop and create the variations now based on the product.
            if($variations){

                echo '<p style="margin-bottom:15px;">Variations exist. Variation building...<br /></p>';

                try{
                    foreach($variations as $variation){
                        $objVariation = new WC_Product_Variation();
                        $objVariation->set_price($variation["price"]);
                        $objVariation->set_regular_price($variation["regular_price"]);
                        $objVariation->set_parent_id($product_id);
                        if(isset($variation["sku"]) && $variation["sku"]){
                            $objVariation->set_sku($variation["sku"]);
                        }
                        $objVariation->set_manage_stock($variation["manage_stock"]);
                        //$objVariation->set_stock_quantity($variation["stock_quantity"]);
                        $objVariation->set_stock_quantity('1000');
                        $objVariation->set_stock_status('instock'); // in stock or out of stock value
                        $var_attributes = array();
                        foreach($variation["attributes"] as $vattribute){
                            $taxonomy = "pa_".wc_sanitize_taxonomy_name(stripslashes($vattribute["name"])); // name of variant attribute should be same as the name used for creating product attributes
                            $attr_val_slug =  wc_sanitize_taxonomy_name(stripslashes($vattribute["option"]));
                            $var_attributes[$taxonomy]=$attr_val_slug;
                            echo '<p>Option written: ' . $vattribute["option"] . '</p>';
                        }
                        $objVariation->set_attributes($var_attributes);
                        $objVariation->save();
                    }
                }
                catch(Exception $e){
                    // handle exception here
                }
            }

标签: phpwordpresswoocommercecustom-wordpress-pages

解决方案


推荐阅读