Johsas Johsas - 1 year ago 128
HTML Question

Prestashop product image is not uploaded when product is created programmatically

Using code below in module to create product and assign uploaded image to it. On

everything works fine, but when I move module to server I get problem. Module on server creates product successfully. But when I open on backend created product image section I get following image with question mark, (screenshot below code). When I get on ftp to img dir (
) there is no img directory or img itself. So it looks like there is problem creating img directory. Does anyone had problem like this and can forward me where to search problem?

//Create product
$product = new Product();
$product->ean13 = 12456;
$product->name = array((int)Configuration::get('PS_LANG_DEFAULT') => 'test');
$product->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => 'test');
$product->id_category = 3;
$product->id_category_default = 3;
$product->redirect_type = '404';
$product->price = 33;
$product->wholesale_price = 25;
$product->minimal_quantity = 1;
$product->show_price = 1;
$product->on_sale = 0;
$product->online_only = 1;
$product->meta_keywords = 'test';
$product->id_tax_rules_group = 0;


//Add main product image
$id_product = $product->id;
$url = '';
$shops = Shop::getShops(true, null, true);
$image = new Image();
$image->id_product = $id_product;
$image->position = Image::getHighestPosition($id_product) + 1;
$image->cover = true; // or false;
if (($image->validateFields(false, true)) === true &&
($image->validateFieldsLang(false, true)) === true && $image->add())
if (!AdminImportController::copyImg($id_product, $image->id, $url, 'products', false))

enter image description here

ts. ts.
Answer Source

If image is copied correctly, you should try (!AdminImportController::copyImg($id_product, $image->id, $url, 'products', true)) to regenrate miniatures

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download