Jump to content
Sign in to follow this  
Julien

Product images in subfolders named by product id

Recommended Posts

Hi,

My shop has about 30 products and 70 product pictures.

All pictures being stored in the same folder, I quickly understood that this would not be sustainable on the long term.
I also dislike always having to click the "Show assigned images only" button.

So, I decided to use a subfolder for each product, which is named by the product id.

Here's how I proceeded:

1) Accessed the "Cubecart_Inventory" table

2) Displayed only useful fields with a SQL request:

SELECT product_id,product_code,name  FROM `CubeCart_inventory` ORDER BY product_id ASC LIMIT 50

3) Using an FTP client, created one subfolder for each product, named by product_id in "images/source"

4) Dragged and dropped the product images into into the subfolders.

Now, how to make Cubecart use the subfolders for each product ?

I assume these are the possible alternatives:

A) Create anSQL trigger on insertions in the "CubeCart_filemanager" table, that catch the "product_id" value in table "CubeCart_image_index" and write it in the field "filepath" of table "CubeCart_filemanager"

B) Edit CubeCart's code. I could find the files concerning product images, but edition doesn't seem easy because the smarty template engine is used.
     This solution would have my preference, but I assume one difficuly is that the product not necessarily have its id yet when the images are uploaded.

C) Change some setting that I ignore.

Has someone already implemented such modificatinons?

Thanks.

Share this post


Link to post
Share on other sites

Generally, that won't work. The table CubeCart_image_index is a many-to-many glue table. That means, CubeCart allows for the same image to be assigned to more than one product (one-to-many), and more than one image to be assigned to any one product (many-to-one). Thus, many-to-many.

By using the product_id as the sub-folder name, you are now artificially limiting any given image can be assigned to only one product.

Granted, that happens in almost all cases anyway. Rarely will any given image be assigned to more than one product -- unless it is a logo, or an artist's portrait, or some other common image that would be applicable to a range of products. One solution would be to upload that one image as many times as necessary into each product-named folder.

The following query is sloppy, but it worked in my experiments. It will update the filepath to hold the product_id-named folder with a trailing slash.

Then clear CubeCart's internal cache.

UPDATE CubeCart_filemanager AS FM SET FM.filepath = CONCAT( (SELECT DISTINCT II.product_id FROM CubeCart_image_index AS II WHERE FM.file_id = II.file_id AND FM.filepath IS NULL LIMIT 1), '/') WHERE FM.filepath IS NULL

 

Share this post


Link to post
Share on other sites

Thank you for your explanations ; I better understand the philosophy behind the pictures management.

Yes, setting the filepath is the way to go and your SQL query did the trick.
Later, I should adapt/convert it into a TRIGGER to maximize comfort.

It would be nice if in the filemanager then directly pointed into the product, whilst still allowing navigation one level up to allow selection of pictures possibly shared amongst several products. I took a look at the code, but will have to dig in.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...