Quantity Issue with Ajax | WordPress.org

[ad_1]

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="https://projectdmc.org/support/topic/quantity-issue-with-ajax/%s" data-quantity="https://projectdmc.org/support/topic/quantity-issue-with-ajax/%s" data-product_id="https://projectdmc.org/support/topic/quantity-issue-with-ajax/%s" data-product_sku="https://projectdmc.org/support/topic/quantity-issue-with-ajax/%s" class="https://projectdmc.org/support/topic/quantity-issue-with-ajax/%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="https://projectdmc.org/support/topic/quantity-issue-with-ajax/%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]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer