Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function setFileForMatrixify()
- {
- $skuList = json_decode(request()->input('ids'));
- $today = date('Y-m-d');
- $this->path = 'public/shopify/';
- // Initialize CSV file
- $filename = "automatic-shopify-{$today}.csv";
- $file = fopen(storage_path("app/{$this->path}{$filename}"), 'w');
- fputcsv($file, $this->headers());
- // Process in small chunks (50 SKUs at a time)
- $chunkSize = 50;
- $totalChunks = ceil(count($skuList) / $chunkSize);
- for ($i = 0; $i < $totalChunks; $i++) {
- $skuChunk = array_slice($skuList, $i * $chunkSize, $chunkSize);
- // Fetch products for this chunk
- $products = Product::whereIn('sku', $skuChunk)
- ->where('is_active', 1)
- ->select('id', 'category_id')
- ->get();
- foreach ($products as $product) {
- // Use switch for PHP 7.2 compatibility
- switch ((int)$product->category_id) {
- case 1:
- case 3:
- $method = 'primary';
- break;
- case 2:
- $method = 'secondary';
- break;
- case 4:
- $method = 'protein';
- break;
- case 5:
- $method = 'lifeSience';
- break;
- case 6:
- $method = 'flow';
- break;
- default:
- $method = null;
- break;
- }
- if ($method) {
- $content = $this->{$method}($product->id);
- foreach ($content as $row) {
- if ($this->isValidRow($row)) {
- fputcsv($file, $this->escapeRow($row));
- }
- }
- }
- }
- // Free up memory after each chunk
- unset($products, $content);
- gc_collect_cycles();
- }
- fclose($file);
- return response()->json(['filename' => $filename]);
- }
Advertisement
Comments
-
Comment was deleted
-
- Hi Kyo,
- I read attached code and It can be optimized by both PHP or MySQL table indexes.
- May I help you to optimize this code. I am available to give you a demo, it will be free of cost.
- You may try at least. I strongly hope that you will not be disappointed and it will not be your waste of time.
- Thank you.
Add Comment
Please, Sign In to add comment
Advertisement