• Resolved fkoomek

    (@fkoomek)


    Hello.
    I was thinking.
    Would it be possible in any easy way to make your plugin work also on archives pages? I mean that it would use your script.
    The way how it is now, it seems that it cooperates with the Woocommerce stock function for adding items to cart with AJAX on archive pages.
    The problem is that this function doesn’t work correctly with caching plugins.
    And I would like to for example not redirect to a single product page when the product is out of stock. Woocoomerce stock AJAX script do it this way.
    Thank you.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author xootix

    (@xootix)

    Hello @fkoomek

    When the product is out of stock, the woocommerce archive page should hide the add to cart button & instead show “Out of stock”.
    This is how woocommerce works by default.
    “And I would like to for example not redirect to a single product page when the product is out of stock.”
    You need to contact woocommerce, this feature is related to woocommerce.

    I am sorry I do not understand your “problem with caching plugins” part, please add more details.

    Thread Starter fkoomek

    (@fkoomek)

    I am sorry, I explained it a little bit wrong…
    I meant the situation when a customer tries to add to cart higher amount of a product than you have in stock.
    If I was on a product page and clicked add to cart button (and there’s no more of the product in stock), I would get a pop-up message “You cannot add that amount to the basket — we have X in stock and you already have X in your basket.” I guess, this is handled by your plugin.
    But if I would try to add to cart a product on archive page it works differently –
    A customer is on archive page and clicks on add to cart button.
    The product is added to the cart.
    Then he clicks on add to cart button again to add more pieces.
    But there’s no more pieces of the product in stock. So, he is redirected to the product page with a message saying: “You cannot add that amount to the basket — we have 1 in stock and you already have 1 in your basket.”
    If the product page wasn’t cached before, it would be cached with that message. Moreover, the message even appears twice sometimes.
    So, I thought that the solution would be that when adding a product on archive page and there would be no more pieces in stock, only pop up window (like the one which pop ups on product page) would show and it would be handled by Ajax, so no caching.
    See the issue
    https://test.liberlapidum.com/wp-content/uploads/issue.gif

    Plugin Author xootix

    (@xootix)

    Hello @fkoomek

    As I said, the above behavior you mentioned is fully handled by woocommerce.
    Plugin ajax functionality is only for product page.
    It is not possible for side cart to fetch error on archive page.
    All I can provide you a snippet which would prevent page refresh & opens up the side cart. Though there will be no error display.
    Please add this to your functions.php

    add_filter( 'woocommerce_cart_redirect_after_error', function( $link, $product_id ){
    	wc_clear_notices();
    	return '';
    },10, 2);
    Thread Starter fkoomek

    (@fkoomek)

    Thank you very much for your help. I really appreciate it ??
    I tried your snippet, but without the message, it’s not the best.
    How is it possible to disable Ajax on archive pages with your plugin activated?
    When I disable it in Woocommerce settings, even then the Ajax on archive pages stay. It’s enabled by your plugin.

    Thread Starter fkoomek

    (@fkoomek)

    Hello.
    So, based on your provided snippet I used it like this:

    add_filter( 'woocommerce_cart_redirect_after_error', function( $link, $product_id ){
    $get_permalink = get_permalink( $product_id ) . '?error_notice';
    return $get_permalink;
    },10, 2);

    I added a query string to URL when there’s an error notice and I prevent this query string from caching.
    So, now a product with error notice is not cached anymore. It seems that it’s working well. Is this a good approach?

    • This reply was modified 4 years, 9 months ago by fkoomek.
    • This reply was modified 4 years, 9 months ago by fkoomek.
    • This reply was modified 4 years, 9 months ago by fkoomek.
    Plugin Author xootix

    (@xootix)

    Hello @fkoomek

    If you want to disable ajax on archive page, simply go to woocommerce settings & uncheck “Ajax add to cart behavior”. If you still see the ajax feature, something else is affecting, not our plugin. “Ajax add to cart” in side cart setting is for product page only.
    I am not sure, how adding this . '?error_notice' will resolve your caching issue.
    Good for you, if its working. ??

    Thread Starter fkoomek

    (@fkoomek)

    Well, before, the URL of product with error notice was the same as URL of product. So, if the product page wasn’t cached yet, there was a little risk that it would be cached with that error. So, now when I added that query string and prevent it from caching, product page with error notice can’t be cached.

    Anyway, I think your plugin really affects Ajax on archive. It stays there even when it’s disabled in Woocommerce. After deactivating your plugin, it dissappears. Try it. ??
    Thank you for support ??

    • This reply was modified 4 years, 9 months ago by fkoomek.
    Plugin Author xootix

    (@xootix)

    Good to know.

    If you’ve made any changes to your archive page template like adding quantity field, then it will behave like a product page.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Ajax on archives’ is closed to new replies.