Импорт товаров в Simpla, загрузка фото по url
Потребовалось реализовать загрузку фото товаров, если указана прямая ссылка на источник.
Как мы знаем, при импорте можно указать фото товара через запятую в поле images или изображения.
Добавим метод в /api/Image.php
public function download_image_from_site($url, $type = 'copy') { if(strpos($url, 'http://') === false) return false; if($type == 'copy') { $name = $this->correct_filename(pathinfo($url, PATHINFO_BASENAME)); $base = pathinfo($url, PATHINFO_FILENAME); $ext = pathinfo($url, PATHINFO_EXTENSION); if(in_array(strtolower($ext), $this->allowed_extentions)) { while(file_exists($this->config->root_dir.$this->config->original_images_dir.$name)) { $name = pathinfo($name, PATHINFO_FILENAME); if(preg_match('/_([0-9]+)$/', $name, $parts)) $name = $base.'_'.($parts[1]+1).'.'.$ext; else $name = $base.'_1.'.$ext; } if(copy($url, $this->config->root_dir.$this->config->original_images_dir.$name)) return $name; } return false; } return false; }
Добавим данный метод в /simpla/ajax/import.php. Добавим код
if(strpos($image, 'http://') !== false) { $image = $this->image->download_image_from_site($image); } // Имя файла $image_filename = pathinfo($image, PATHINFO_BASENAME);