Argjent
Argjent

Reputation: 97

Insert image to prestashop database

I am making a simple script that will insert product to prestashop database directly and I can't find how to insert an image to the database.

So far my script is:

  mysqli_query($conn, 'INSERT INTO ps_product (id_supplier, id_manufacturer, id_category_default, id_shop_default, id_tax_rules_group, on_sale, online_only, quantity, minimal_quantity, price, wholesale_price, out_of_stock, active, redirect_type, available_for_order,  show_price, indexed, visibility, cache_default_attribute, date_add, date_upd, pack_stock_type,ean13,upc,unity,reference,supplier_reference,location) 
VALUES ("2", "2" , "5", "1", "1","1","1", "0", "1", "30", "30", "2", "1", "302","1","1","1","both","19",now(),now(),"3","0","0","","demo_1","","" )');


mysqli_query($conn, 'INSERT INTO ps_product_lang (id_product, id_lang, description, description_short, link_rewrite, name, available_now,meta_description,meta_keywords,meta_title,available_later) 
VALUES (LAST_INSERT_ID(),"1","long des","test","insert title","title inserted","In stock","","","","")');


mysqli_query($conn, 'INSERT INTO ps_product_shop (id_product,id_shop,id_category_default,id_tax_rules_group,price,wholesale_price,unity,active,redirect_type,indexed,cache_default_attribute,date_add,date_upd ) 
VALUES (LAST_INSERT_ID(),"1","5","1","30","30","","1","301","1","1",now(),now())');

And now I have to inset the photo in it so that the I can complete the product and publish it.

Upvotes: 3

Views: 2160

Answers (1)

Florian Lemaitre
Florian Lemaitre

Reputation: 5748

First you need to create a new entry for your image in database (table image). If you look at Prestashop Image Class it stores images under directories created from your image id. You need to split each number of your image id to get the path. Here is Prestashop Image Class method :

/**
 * Returns the path to the folder containing the image in the new filesystem
 *
 * @param mixed $id_image
 * @return string path to folder
 */
public static function getImgFolderStatic($id_image)
{
    if (!is_numeric($id_image)) {
        return false;
    }
    $folders = str_split((string)$id_image);
    return implode('/', $folders).'/';
}

In defines.inc.php _PS_IMG_DIR_ defines your image folder (default: /img/) and _PS_PROD_IMG_DIR_ defines your product image folder (default: p/). by default the folder for product image is then /img/p/.

If an image has for id 927 and for name "test.jpg" it will be stored under /img/p/9/2/7/927.jpg

First try this for a few products and then in Prestashop BackOffice go to Preferences > Images > regenerate thumbnails (at the bottom of the page). Prestashop should generate each products image sizes for you.

Please let me know if it doesn't work or if you need more instructions.

Upvotes: 6

Related Questions