Quantity Issue with Ajax
-
Hi!,
If I filter products by ajax the custom quantity input elements are not rendered after ajax filtering is finished…
This is my custom code for adding quantities in archive page// Adding quantity for products on the archive page
add_filter('woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_loop_ajax_add_to_cart', 20, 2);
function quantity_inputs_for_loop_ajax_add_to_cart($html, $product) {
if ($product && $product->is_type('simple') && $product->is_purchasable() && !$product->is_sold_individually()) {
// Get product stock quantity and stock status
$stock_quantity = $product->get_stock_quantity();
$stock_status = $product->is_in_stock() ? 'in-stock' : 'out-stock';
// Get the necessary classes for the button
$class = implode(' ', array_filter(array(
'button',
'product_type_' . $product->get_type(),
$product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',
$product->supports('ajax_add_to_cart') ? 'ajax_add_to_cart' : '',
)));
if ($product->is_in_stock()) {
// If product is in stock, display quantity and add to cart
$html = sprintf('%s<a rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>',
woocommerce_quantity_input(array(
'min_value' => 1, // Minimum value
'max_value' => $stock_quantity, // Maximum value based on stock
'input_value' => 1, // Default value
), $product, false),
esc_url($product->add_to_cart_url()),
esc_attr(1),
esc_attr($product->get_id()),
esc_attr($product->get_sku()),
esc_attr($class),
esc_html($product->add_to_cart_text())
);
} else {
// If product is out of stock, display "View Product" button
$html = sprintf('<a href="%s" class="button view_product view-product-btn-archive">%s</a>',
esc_url(get_permalink($product->get_id())), // Link to the product page
esc_html__('View Product', 'woocommerce') // Text for the button
);
}
}
return $html;
}I will really appreciate if you guys can figure out why this occurs, thanks !
The page I need help with: [log in to see the link]
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- You must be logged in to reply to this topic.