Home > front end >  WooCommerce import data from Json File
WooCommerce import data from Json File

Time:09-29

Hello I have a problem with a plugin I created for importing from a json file in WooCommerce. It collects the data and if the product does not exist it creates it with its characteristics (putting it in draft) otherwise it only updates the price. Up to about 100/150 products everything works perfectly. At some point I have memory_limit and max_excution_time errors. I tried increasing the values ​​up to 1GB. But at some point it crashes. The code is:

public function process_products(){

    $file_exist = ABSPATH . '/data/import/data.json';
    if (!file_exists($file_exist)) {
      return;
    };

    $data = file_get_contents($file_exist);
  
    $json_data = json_decode($data, true);

   
  
    $articoli = $json_data["dsDati"]["TArticolo"];
    $articoli_prezzi = $json_data["dsDati"]["TArticoloPrezzo"];
  
    $post_author = wp_get_current_user()->ID;
  
  
    $products = [];
  
    foreach ($articoli as $key => $articolo) {
  
  
      $products[$key]['title'] = $articolo["DescrizioneArticolo"];
      $products[$key]['sku_prodotto'] = $articolo["IdArticolo"];
      $products[$key]['aliquota'] = $articolo["AliquotaIva"];
      $products[$key]['quatity_stock'] = str_replace(' ', '', $articolo["QtaDisp"]);
  
  
  
      foreach ($articoli_prezzi as $key2 => $articolo_prezzo) {
        if ($products[$key]['sku_prodotto'] != null && $articolo_prezzo["Nome"] == 'L1'  && $products[$key]['sku_prodotto'] == $articolo_prezzo["IdArticolo"]) {
          $products[$key]['price'] =  str_replace(' ', '', $articolo_prezzo["Valore"]);
        }
      };
  
  
  
      // cicliamo i prodotti da inserire
      /**
       * #  array(5) {
       *    ["title"]=>
       *    string(39) "ASTA TELESCOPICA DA 32 A 62 CON ZOCCOLO"
       *    ["sku_prodotto"]=>
       *    string(8) "00003590"
       *    ["aliquota"]=>
       *    string(2) "22"
       *    ["quatity_stock"]=>
       *    string(2) "50"
       *    ["price"]=>
       *    string(4) "8.00"
       *  }
       **/
      foreach ($products as $key => $product) {
  
        $product_id_exist = wc_get_product_id_by_sku($product["sku_prodotto"]);
  
        // verifichiamo se il prodotto esiste
  
        // prodotto esistente
        if ($product_id_exist != 0) {
          // inseriamo il prodotto
          $product_to_update = new \WC_Product($product_id_exist);
  
          // aggiorniamo prezzo
          $product_to_update->set_regular_price($product["price"]);
  
          //aggiorniamo quantita
          $product_to_update->set_stock_quantity($product["quatity_stock"]);
  
  
          $product_to_update->save();
  
          continue;
        } else {
  
          // inseriamo il prodotto
          $new_product = new \WC_Product;
          $new_product->set_name($product["title"]);
          $new_product->set_sku($product["sku_prodotto"]);
          $new_product->set_status('draft');
          $new_product->set_regular_price($product["price"]);
          $new_product->set_manage_stock('yes');
          $new_product->set_stock_quantity(absint($product["quatity_stock"]));
          $new_product->save();
  
        }
      }
  
    };
  
   }

I have created a cron that checks every night at midnight. The problem is that there are over 600 products. Is there a way to solve the problem? Thanks for those who have the patience to answer

CodePudding user response:

Easy solution that we use in our plugin is splitting the import in multiple steps and calling the cron more often.

Make a working copy of data.json (lets call it temporary.json), import 100 products, remove them from temporary.json and in next run check if the "temporary.json" file exist. If so use it. If not, use the default one.


Thats the only solution we found ( that is working on every environment.. especially if you write plugin to wordpress plugin shop, where users mostly can't change their server limits ).

  • Related