JuanTomas Posted October 7, 2015 Share Posted October 7, 2015 (edited) 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 October 7, 2015 by JuanTomas (see edit history) Link to comment Share on other sites More sharing options...
JuanTomas Posted October 7, 2015 Author Share Posted October 7, 2015 (edited) 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 October 7, 2015 by JuanTomas (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now