• 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 2631

    The 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/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,
    you can deactivate plugins that you don’t need for example imagick.so.
    Also you can rise the php memory limit if you have enough included in your hosting plan.

    Both in php.ini

    Php.ini memory_limit = 1024M
    Unfortunately I can’t control the active plugins and settings of all plugin users

    Are there any recommendations for using the media_sideload_image function. And disabling other plugins for on is not an option. Since our plugin must, in addition to importing goods, check the data of goods every day, add new products, delete old ones, etc.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Need help with optimization’ is closed to new replies.