1
votes

In my Woocommerce Shop, I have added a quantity box next to the add to cart button on archive pages using this code:

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {

        $html = '<a rel="nofollow" data-product_id="'. $product->id .'" onclick="addToCartLink(this,' . $product->id .')" class="add_to_cart_button product_type_simple button primary is-flat mb-0 is-small">הוסף לסל</a>';
        // $html = '<a onclick="addToCartLink(this,'. $product->id .')">הוסף לסל</a>';

        $html .= '<div class="quantity buttons_added">';
        $html .= '<input type="button"  value="-"  class="minus button is-form">';
        $html .= '<input type="number" id="quantity_'. $product->id .'" class="input-text qty text" step="1" min="0" max="9999" name="quantity" value="1" title="כמות" size="4" pattern="[0-9]*" inputmode="numeric" >';
        $html .= '<input type="button" value="+" class="plus button is-form">';
        $html .= '</div>';
    }
    return $html;
}

In cart page, I have created a custom Cross-Sells products carousel. So I need to avoid this code working in the cart page and so the Cross-Sells products add to cart buttons will be the default woocommerce ones.

How can I exclude the cart page in my code?

1

1 Answers

2
votes

You just need to add in the existing if statement this condition: ! is_cart()

So your code will be:

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() && ! is_cart() ) {

        $html = '<a rel="nofollow" data-product_id="'. $product->id .'" onclick="addToCartLink(this,' . $product->id .')" class="add_to_cart_button product_type_simple button primary is-flat mb-0 is-small">הוסף לסל</a>';
        //$html = '<a onclick="addToCartLink(this,'. $product->id .')">הוסף לסל</a>';

        $html .= '<div class="quantity buttons_added">';
        $html .= '<input type="button"  value="-"  class="minus button is-form">';
        $html .= '<input type="number" id="quantity_'. $product->id .'" class="input-text qty text" step="1" min="0" max="9999" name="quantity" value="1" title="כמות" size="4" pattern="[0-9]*" inputmode="numeric" >';
        $html .= '<input type="button" value="+" class="plus button is-form">';
        $html .= '</div>';

    }
    return $html;
}

This should solve your problem.

Related official documentation: WooCommerce Conditional tags | is_cart()