Sh404sef/Virtuemart Ajax Fail (Quick Fix)

virtuemart_sh404sef

I ran into this problem the other day and thought I would post the solution I used.

The problem

You’ve built a site in Joomla and you are using the Virtuemart E-Commerce component.  Everything is working fine.  Then you turn on the Sh404sef component so you get search engine friendly (SEF) urls.  As you navigate through your online shop all the urls look fine…until you try to use a Virtuemart ajax function.

In my case it was the dropdown that lets you change the color of a product (a.k.a. the dropdown that lets you choose a child item) on its flypage.

Looking at my Firebug console, I could see that when the dropdown was changed, an ajax call was made to

http://mydevurl/index.php?product_id=8&option=com_virtuemart&page=
   shop.product_details&flypage=flypage.tpl&Itemid=38&category_id=1&only_page=1

which was picked up by Sh404 and redirected to its SEF url leaving the product page unchanged.

How could I get Sh404 to continue to do a great job on the non-ajax urls, but bypass the ajax call altogether?

The solution

The solution comes from a combination of the “only_page=1″ tacked to the end of the ajax call and the Virtuemart custom Sh404 file (/components/com_sh404sef/sef_ext/com_virtuemart.php in your joomla install).

For those of you who don’t know what the sef_ext folder in Sh404 is used for, here’s the long version.  The short version is this: each file in this folder tells Sh404 how to encode & decode the urls generated by the component with the same name.  I.e. the com_virtuemart.php file tells Sh404 how to build (and decipher) Virtuemart-specific urls.

The other part of the fix comes from my assumption (yes, I should probably research this more)  that “only_page=1″ is used by Virtuemart to signal that it should only return the component content, as opposed to the entire page – a common practice in ajax programming.

Using this knowledge, we should be able to edit the com_virtuemart.php file and tell it to not redirect if the given url has “only_page=1″.  I did this by inserting the following code block at line 26 (just after the language file is loaded):

// [HACK] -> bypass this file if we're making an ajax request
$only_page = JRequest::getInt('only_page',0);
if($only_page == 1) return;
// [/HACK]

Adding this code put me be back in business – with user friendly urls and ajax functionality.

Note: There are definitely ways to make this block shorter, and to be honest, it could probably go at the top of the file.  (insert obligatory it-worked-and-I’m-too-busy-to-go-back-and-play-around-with-it-but-not-too-busy-to-blog-about-it excuse here)

Tags:

7 Responses to “Sh404sef/Virtuemart Ajax Fail (Quick Fix)”

  1. I’ve got a virtuemart cart (1.1.4 with joomla 1.5) that I’ve inherited and I’m helping my developer trouble shoot some issues we are having with the interaction of child products/attributes, SEF, broken breadcrumbs and missing pictures.

    The store is at http://www.pitchinforagoodcause.org/index.php/buy-gkr-souvenirs.html.
    Take a look specifically at the womens boutique, chose a shirt size and watch what happens. I don’t want it to do that – insert the attribute name into the breadcrumb, break the breadcrumb and break the links/lightbox to the detail images.

    I also don’t want to loose the child products so the client can keep a more accurate inventory.

    My developer tried your work around but it didn’t change anything. Do you have another suggestion?

  2. John says:

    Hi Marjorie,

    I took a look at your site and it seems the extra images don’t show up because they are using a relative url (i.e. “images/stories/tee_images/t_heyman_backside_small.jpg” ). Since sh404 is building sef urls, the browser is trying to load an image at “/Woman-s-Boutique/T.-Heyman-Tees-Real-Men-Call/images/stories/tee_images/t_heyman_backside_small.jpg”, which doesn’t exist.

    I can also see (in Firebug) an error saying “Lightbox is not defined” but I have a hunch it will be fixed if you get the extra images loaded properly.

    One way to do this would be to find the point in your template where the extra images html is generated and add a forward slash (/) to the beginning of the image path. This is what’s happening the first time you load the page.

    As for the breadcrumbs, you could probably put a condition in your flypage that will leave out the child product title.

    Bear in mind that I can’t see your code, so I’m just making a few educated guesses.

    I hope that helps. Let us know how you get on.

  3. Kenny says:

    Hey, I face the almost same problem as you. When I try to select the child product in the drop down list, the images product description cannot be loaded. I tried to disable sh404sef and selec the child product, everything work perfectly.

    Hence, the problem is between virtuemart and sh404sef.

    I tried to put in your hack as following in com_virtuemart.php.

    // —————— load language file – adjust as needed —————————————-

    $shLangIso = shLoadPluginLanguage( ‘com_virtuemart’, $shLangIso, ‘_PHPSHOP_LIST_ALL_PRODUCTS’);

    // —————— load language file – adjust as needed —————————————-

    // [HACK] -> bypass this file if we’re making an ajax request
    $only_page = JRequest::getInt(‘only_page’,0);
    if($only_page == 1) return;
    // [/HACK]

    After that when I try to select the child product, I found that it still redirect to the new sef url in firebug.I tried to clear all the sef url and tried again, but the result is the same.

    Please kindly advise whether I did anything wrong on applying your hack. You can try to access the page as following.

    http://www.fashion-clicks.com/Order-Now/Women-s-Clothing/Clothing-Accessories/Elegant-Ribbon-Satin-Shining-Belt.html

  4. Allen says:

    Not sure if this is the same problem or if it’s even related but the problem I have with child products is it appears sh404SEF stops working.

    When I’m on the parent product the URL is fine, but when I select a child product the URL goes back to the ugly default Joomla/VirtueMart style with no SEF at all. Plus the links in the breadcrumb are not correct, each link acts as those it’s the base, no parent category link.

    I tried adding your hack but it didn’t help.

    You can see the problem at the following:
    http://nichelabs.com/_production/MatTech/Anti%20Fatigue%20Mats/Kitchen-Anti-Fatigue-Mats.html

    If you select a “type of mat” you’ll notice the change is the URL and then look at the links for each stage of the breadcrumb and you’ll notice they are incorrect as well.

  5. Col says:

    This didn’t work for me at all, i did manage to finally get things looking great, and use some creative thinking. When i was changing the child option via the menu it would always retain the last one I looked at, and never then allowed it to change, when you added the item to the cart it was always the wrong item, however success came when i added the parent SKU as an option in SH404, which for me is a relevant worded SKU so its all now working perfect, thanks for the initial help though, and now i get nice seo urls with the sku being a name and number, like nature1-filename.html which im happy with, it works, and looks good thats all matters to me.

    checkout http://www.sunshinewalls.com for a look

  6. Luca says:

    Hi,
    thanks for your help! I was going crazy to debug this error!!
    Really appreciated!!
    Luca

  7. Kitey says:

    KUDOS :)

Leave a Reply