Jump to content

my php code to work up a p-shop category img src is ugly


JuanTomas

Recommended Posts

I have a catalog with many thousands of similar items, and I don't want to bother to attach images to every single one.  Instead, I use a generic image, assigned to the category.  I've developed a couple of overrides so far, so that when viewing a product, (whether in product view, blocknewproducts module, etc.) the generic category image is substituted for the (nonexistent) product image.  It all looks very nice.

 

What doesn't look so nice is the way I have to scrounge around to get the URL of the category image so I can pass it to the template.  Here's the logic (Prestashop 1.6.1.1):

 

$product['cat_image_url']=str_replace('http:', 'http:/',
                    str_replace('//','/',
                        str_replace(
                            _PS_ROOT_DIR_,
                            _PS_BASE_URL_.__PS_BASE_URI__,
                            _PS_CAT_IMG_DIR_.$category->id_category.'-small_default.jpg'
                        )
                    )
                );

 

That's three, count 'em three, nested str_replace calls.  The problem is, PS_BASE_URL has a trailing slash, and PS_BASE_URI has a leading slash.  That makes a double slash in the middle of the URL.  To clean that up, you need to replace the double slash with a single.  But now you've wrecked the double slash after 'http:' so you have to go back and add another slash after 'http:'. 

 

Browsers seem to tolerate the double-slash after PS_BASE_URL, treating it as a single slash.  Whatever, I'm uptight and I like to do things intentionally, not lean on the other guy to deal with my sloppy work.  Anybody know a cleaner way, in Prestashop or PHP, to get the category image URL, given a category object?

Edited by JuanTomas (see edit history)
Link to comment
Share on other sites

Okay, I was able to improve it somewhat, getting rid of one of the str_replace calls:

 

$product['cat_image']=str_replace(
                            _PS_ROOT_DIR_,
                            _PS_BASE_URL_.'/'.str_replace('/','', __PS_BASE_URI__),
                            _PS_CAT_IMG_DIR_.$category->id_category.'-small_default.jpg'
                        );

 

 

(I just noticed, this won't work if there's a slash inside __PS_BASE_URI__.  So if your shop is at e.g. www.mydomain.com/xxx/myshop you'll need to use the uglier version in my first post.)

 

That's an acceptable way to find the category image URL, given a prestashop category object.  I still suspect there may be a way that's "more standard", given this is probably a pretty common thing to want to do.
              

Edited by JuanTomas (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...