• I’m developing a theme where we want to use the pro version of this plugin (I’m developing with the free one though). Unfortunately, when I create my own add-to-cart Button/Link, the ajax add to cart ist not really working. After I click my custom button, the page gets reloaded and then the side card is shown (on the new page). But I obviously don’t want it to reload.

    This is my filter to customize the cart button:

    add_filter('woocommerce_loop_add_to_cart_link', function ($array, $product) {
      return sprintf(
        '
        <a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="fh__product__add-to-cart %s product_type_%s">
          <img src="' . asset_path('images/shopping-bag.svg') . '" class="fh__product-icon" width="100%%" height="100%%"/>
        </a>',
        esc_url($product->add_to_cart_url()),
        esc_attr($product->get_id()),
        esc_attr($product->get_sku()),
        esc_attr(isset($quantity) ? $quantity : 1),
        $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button icon-shopping-cart' : '',
        esc_attr($product->get_type()),
        esc_html($product->add_to_cart_text())
      );
    }, 10, 2);

    I observed that my custom button does not get the class ajax_add_to_cart. The default button does get this class. But I could not find any place in your plugins code where this class gets added to the button.

    Please point me in the right direction to get this working.

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

    (@xootix)

    You need to add class “ajax_add_to_cart” & “add_to_cart_button” to your button and not in plugin’s code.
    The ajax add to cart functionality is part of woocommerce.
    Try this

    add_filter('woocommerce_loop_add_to_cart_link', function ($array, $product) {
      return sprintf(
        '
        <a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="fh__product__add-to-cart %s product_type_%s">
          <img src="' . asset_path('images/shopping-bag.svg') . '" class="fh__product-icon" width="100%%" height="100%%"/>
        </a>',
        esc_url($product->add_to_cart_url()),
        esc_attr($product->get_id()),
        esc_attr($product->get_sku()),
        esc_attr(isset($quantity) ? $quantity : 1),
        $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart add_to_cart_button icon-shopping-cart' : '',
        esc_attr($product->get_type()),
        esc_html($product->add_to_cart_text())
      );
    }, 10, 2);
    • This reply was modified 3 years, 5 months ago by xootix.
    • This reply was modified 3 years, 5 months ago by xootix.
    Thread Starter DanielEngelhardt

    (@danielengelhardt)

    Oh wow, that was a complete misunderstanding from my side. So sorry. Your approach works, thank you so much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Not working with custom add-to-cart link’ is closed to new replies.