Need help with optimization
-
I create my plugin for import products in woocommerce.
I get data about products through the open API of my site.The plugin works fine in a local environment and on most servers.
But there are customers who have encountered a problem. When uploading images for a product, an error occurs:
PHP Fatal error: Out of memory (allocated 232787968) (tried to allocate 33554440 bytes) in wp-includes/functions.php on line 2631The error completely interrupts the script. At the same time, for some time this client did not have such a problem, and he was able to unload more than 7000 products without any problems
Image loading function
public static function saveFiles($arFiles, $type = 'previewUrl', $post_id = 0) { $settings = new Natali_Model_Settings(); $isSingle = (int)$settings->get('image_color'); try { $localFiles = []; foreach ($arFiles as &$file) { if ($type === 'previewUrl') { $url = str_replace( 'https://static.natali37.ru/', 'https://static.natali37.ru/media/600/', $file['url']); } else { $url = $file['url']; } if ($isSingle) { if ($file['mainColorImage']) { $localFiles[crc32($url)] = self::getFileID($url, $post_id); } } else { $localFiles[crc32($url)] = self::getFileID($url, $post_id); } } return self::$files = $localFiles; } catch (\Exception $error) { Natali_Log::set($error->getMessage(), $error->getCode()); } }private static function getFileID($url, $post_id = 0) { if ($existId = self::findByName($url)) { return $existId; } return media_sideload_image($url, $post_id, null, 'id'); }private static function findByName($localFile) { $arFile = explode('/', $localFile); [$fileName, $fileExt] = explode('.', array_pop($arFile)); $find = new \WP_Query([ 'post_type' => 'attachment', 'name' => $fileName ]); if ($find->have_posts()) { $attachments = $find->get_posts(); foreach ($attachments as $attach) { return $attach->ID; } } return false; }And although the error occurs. Almost all images get into media files and are attached to the product. But since the code is interrupted by an error. Then I fail to insert images into the product itself through the command
@var $this->wc_api \Automattic\WooCommerce\Client $this->wc_api->put("products/$this->wc_id", $this->wc_data)You can view the full plugin code https://wordpress.org/plugins/natali/
The topic ‘Need help with optimization’ is closed to new replies.