John Davidson

php - add to cart button disappear if I use quntity feild

0 comments
Message:


I'm trying to add "quantity field" for each product in shop page


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 = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
// Access the cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['product_id'];
$quantity = $cart_item['quantity'];
}
// If we have match update quantity
if($product_id == $product->get_ID()) {
$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; If I use the quantity field and pressed add to cart button, the button will disappear, and ONLY when deleting the cart the add to cart button will show up again.


What I'm doing wrong?



My Answer:
a quantity field to my add to cart button in PHP, but when I do so, the add to cart button disappears. Here is my code:

php







It seems like the issue might be with the styling of the quantity field or the submit button. I would recommend checking the CSS styles applied to these elements to see if there is any conflicting styling that might be causing the button to disappear.

Additionally, you can try adding some inline styling to the quantity field and submit button to see if that helps:

php







If the issue persists, you may need to further investigate the code in your cart.php file to see if there are any conditions that might be causing the add to cart button to disappear based on the quantity field value.

Rate this post

4 of 5 based on 6045 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us