Search code examples
phpwordpresswoocommercehook-woocommerce

adding quantity_inputs after woocommerce_loop_add_to_cart_link hook without calling it


I added a code in function.php to add quantity section after add to cart in all pages.

But I don't want to add it also in wishlist page,

So I make it like this:

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 (!is_page('wishlist')) {
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        // Access the cart items
        $in_cart = false;
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            $product_id = $cart_item['product_id'];
            $quantity = $cart_item['quantity'];
            if($product_id == $product->get_ID()){
                $in_cart = true;
                break;
            }
        }
        // If we have match update quantity
        if($in_cart) {
            $html .= woocommerce_quantity_input( array('input_value' => $quantity), $product, false );
        } else {
            $html .= woocommerce_quantity_input( array(), $product, false );
        }
        $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    return $html;
}
}

It works, but the problem is it remove add to cart button on wishlist page

What I'm doing wrong? Can I do it without calling woocommerce_loop_add_to_cart_link hook

Because if I remove it from filter, the button appear again


Solution

  • You need to add return $html; at the bottom like this

    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 (!is_page('wishlist')) {
            if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
                $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
                // Access the cart items
                $in_cart = false;
                foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                    $product_id = $cart_item['product_id'];
                    $quantity = $cart_item['quantity'];
                    if($product_id == $product->get_ID()){
                        $in_cart = true;
                        break;
                    }
                }
                // If we have match update quantity
                if($in_cart) {
                    $html .= woocommerce_quantity_input( array('input_value' => $quantity), $product, false );
                } else {
                    $html .= woocommerce_quantity_input( array(), $product, false );
                }
                $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
                $html .= '</form>';
            }
            return $html;
        }
        return $html;
    }