Advertisement
bheng8200

setFileForMatrixify

Mar 9th, 2025 (edited)
220
1
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 1 0
  1. public function setFileForMatrixify()
  2. {
  3.     $skuList = json_decode(request()->input('ids'));
  4.     $today = date('Y-m-d');
  5.     $this->path = 'public/shopify/';
  6.    
  7.     // Initialize CSV file
  8.     $filename = "automatic-shopify-{$today}.csv";
  9.     $file = fopen(storage_path("app/{$this->path}{$filename}"), 'w');
  10.     fputcsv($file, $this->headers());
  11.  
  12.     // Process in small chunks (50 SKUs at a time)
  13.     $chunkSize = 50;
  14.     $totalChunks = ceil(count($skuList) / $chunkSize);
  15.  
  16.     for ($i = 0; $i < $totalChunks; $i++) {
  17.         $skuChunk = array_slice($skuList, $i * $chunkSize, $chunkSize);
  18.  
  19.         // Fetch products for this chunk
  20.         $products = Product::whereIn('sku', $skuChunk)
  21.             ->where('is_active', 1)
  22.             ->select('id', 'category_id')
  23.             ->get();
  24.  
  25.         foreach ($products as $product) {
  26.             // Use switch for PHP 7.2 compatibility
  27.             switch ((int)$product->category_id) {
  28.                 case 1:
  29.                 case 3:
  30.                     $method = 'primary';
  31.                     break;
  32.                 case 2:
  33.                     $method = 'secondary';
  34.                     break;
  35.                 case 4:
  36.                     $method = 'protein';
  37.                     break;
  38.                 case 5:
  39.                     $method = 'lifeSience';
  40.                     break;
  41.                 case 6:
  42.                     $method = 'flow';
  43.                     break;
  44.                 default:
  45.                     $method = null;
  46.                     break;
  47.             }
  48.  
  49.             if ($method) {
  50.                 $content = $this->{$method}($product->id);
  51.                 foreach ($content as $row) {
  52.                     if ($this->isValidRow($row)) {
  53.                         fputcsv($file, $this->escapeRow($row));
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.  
  59.         // Free up memory after each chunk
  60.         unset($products, $content);
  61.         gc_collect_cycles();
  62.     }
  63.  
  64.     fclose($file);
  65.     return response()->json(['filename' => $filename]);
  66. }
Advertisement
Comments
  • niceahsan
    5 hours (edited)
    Comment was deleted
  • niceahsan
    4 hours
    # text 0.30 KB | 0 0
    1. Hi Kyo,
    2. I read attached code and It can be optimized by both PHP or MySQL table indexes.
    3. May I help you to optimize this code. I am available to give you a demo, it will be free of cost.
    4. You may try at least. I strongly hope that you will not be disappointed and it will not be your waste of time.
    5. Thank you.
Add Comment
Please, Sign In to add comment
Advertisement