Skip to content

Instantly share code, notes, and snippets.

@webaware
Last active August 5, 2025 09:31
Show Gist options
  • Save webaware/6260468 to your computer and use it in GitHub Desktop.
Save webaware/6260468 to your computer and use it in GitHub Desktop.

Revisions

  1. webaware revised this gist Mar 30, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ function custom_woo_loop_add_to_cart_link($button, $product) {
    $button = ob_get_clean();

    // modify button so that AJAX add-to-cart script finds it
    $replacement = sprintf('data-product_id="%d" data-quantity="1" $1 add_to_cart_button product_type_simple ', $product->id);
    $replacement = sprintf('data-product_id="%d" data-quantity="1" $1 ajax_add_to_cart add_to_cart_button product_type_simple ', $product->id);
    $button = preg_replace('/(class="single_add_to_cart_button)/', $replacement, $button);
    }
    }
  2. webaware revised this gist Mar 11, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ function custom_woo_after_shop_loop() {

    <?php /* when product quantity changes, update quantity attribute on add-to-cart button */ ?>
    $("form.cart").on("change", "input.qty", function() {
    $(this.form).find("button[data-quantity]").attr("data-quantity", this.value);
    $(this.form).find("button[data-quantity]").data("quantity", this.value);
    });

    <?php /* remove old "view cart" text, only need latest one thanks! */ ?>
  3. webaware revised this gist Mar 18, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    * start the customisation
    */
    function custom_woo_before_shop_link() {
    add_filter('woocommerce_loop_add_to_cart_link', 'custom_woo_loop_add_to_cart_link', 10);
    add_filter('woocommerce_loop_add_to_cart_link', 'custom_woo_loop_add_to_cart_link', 10, 2);
    add_action('woocommerce_after_shop_loop', 'custom_woo_after_shop_loop');
    }
    add_action('woocommerce_before_shop_loop', 'custom_woo_before_shop_link');
  4. webaware revised this gist Mar 18, 2014. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    * start the customisation
    */
    function custom_woo_before_shop_link() {
    add_filter('woocommerce_loop_add_to_cart_link', 'custom_woo_loop_add_to_cart_link', 10, 3);
    add_filter('woocommerce_loop_add_to_cart_link', 'custom_woo_loop_add_to_cart_link', 10);
    add_action('woocommerce_after_shop_loop', 'custom_woo_after_shop_loop');
    }
    add_action('woocommerce_before_shop_loop', 'custom_woo_before_shop_link');
    @@ -13,10 +13,9 @@ function custom_woo_before_shop_link() {
    * customise Add to Cart link/button for product loop
    * @param string $button
    * @param object $product
    * @param array $link
    * @return string
    */
    function custom_woo_loop_add_to_cart_link($button, $product, $link) {
    function custom_woo_loop_add_to_cart_link($button, $product) {
    // not for variable, grouped or external products
    if (!in_array($product->product_type, array('variable', 'grouped', 'external'))) {
    // only if can be purchased
  5. webaware revised this gist Nov 2, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -45,12 +45,12 @@ function custom_woo_after_shop_loop() {
    jQuery(function($) {

    <?php /* when product quantity changes, update quantity attribute on add-to-cart button */ ?>
    $("form.cart input.qty").on("change", function() {
    $(this).closest("form").find("button[data-quantity]").attr("data-quantity", this.value);
    $("form.cart").on("change", "input.qty", function() {
    $(this.form).find("button[data-quantity]").attr("data-quantity", this.value);
    });

    <?php /* remove old "view cart" text, only need latest one thanks! */ ?>
    $("body").on("adding_to_cart", function() {
    $(document.body).on("adding_to_cart", function() {
    $("a.added_to_cart").remove();
    });

  6. webaware created this gist Aug 18, 2013.
    61 changes: 61 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    <?php

    /**
    * start the customisation
    */
    function custom_woo_before_shop_link() {
    add_filter('woocommerce_loop_add_to_cart_link', 'custom_woo_loop_add_to_cart_link', 10, 3);
    add_action('woocommerce_after_shop_loop', 'custom_woo_after_shop_loop');
    }
    add_action('woocommerce_before_shop_loop', 'custom_woo_before_shop_link');

    /**
    * customise Add to Cart link/button for product loop
    * @param string $button
    * @param object $product
    * @param array $link
    * @return string
    */
    function custom_woo_loop_add_to_cart_link($button, $product, $link) {
    // not for variable, grouped or external products
    if (!in_array($product->product_type, array('variable', 'grouped', 'external'))) {
    // only if can be purchased
    if ($product->is_purchasable()) {
    // show qty +/- with button
    ob_start();
    woocommerce_simple_add_to_cart();
    $button = ob_get_clean();

    // modify button so that AJAX add-to-cart script finds it
    $replacement = sprintf('data-product_id="%d" data-quantity="1" $1 add_to_cart_button product_type_simple ', $product->id);
    $button = preg_replace('/(class="single_add_to_cart_button)/', $replacement, $button);
    }
    }

    return $button;
    }

    /**
    * add the required JavaScript
    */
    function custom_woo_after_shop_loop() {
    ?>

    <script>
    jQuery(function($) {

    <?php /* when product quantity changes, update quantity attribute on add-to-cart button */ ?>
    $("form.cart input.qty").on("change", function() {
    $(this).closest("form").find("button[data-quantity]").attr("data-quantity", this.value);
    });

    <?php /* remove old "view cart" text, only need latest one thanks! */ ?>
    $("body").on("adding_to_cart", function() {
    $("a.added_to_cart").remove();
    });

    });
    </script>

    <?php
    }