Johsas
Johsas

Reputation: 79

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 localhost 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 (/img/p/3/7/37.jpg) 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?

 <?php
    //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;

    $product->add();
    $product->addToCategories(array(3));
    StockAvailable::setQuantity($product->id,'',10);


    //Add  main product image
    $id_product = $product->id;
    $url = 'http://www.webadress.com/image/product/color/0959.jpg';
    $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())
    {
        $image->associateTo($shops);
        if (!AdminImportController::copyImg($id_product, $image->id, $url, 'products', false))
        {
            $image->delete();
        }
    }
    ?>

enter image description here

Upvotes: 1

Views: 1878

Answers (1)

ts.
ts.

Reputation: 10709

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

Upvotes: 2

Related Questions