AGREGAR CANTIDAD EN LISTA DE PRODUCTOS

/* Botón de añadir al carrito en tienda */
add_action( ‘woocommerce_after_shop_loop_item’, ‘woocommerce_template_loop_add_to_cart’, 20 );

//agregamos lac cantidades antes del boton
//Agregamos código HTML
add_filter( ‘woocommerce_loop_add_to_cart_link’, ‘dcms_add_quantity_field’, 10, 2 );
function dcms_add_quantity_field($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”>’;
$html.= woocommerce_quantity_input(array(),$product,false);
$html.='<button type=”submit” data-quantity=”1″ data-product_id=”‘.$product->get_id().'” class=”button alt ajax_add_to_cart add_to_cart_button product_type_simple”>’. esc_html($product->add_to_cart_text()).'</button>’;
$html.='</form>’;
}
return$html;
}

//Agreamos código javascript
add_action( ‘init’, ‘dcms_quantity_change’ );
function dcms_quantity_change() {
wc_enqueue_js(‘
(function( $ ) {
$(“form.cart”).on(“change”, “input.qty”, function() {
$(this.form).find(“[data-quantity]”).attr(“data-quantity”, this.value);
});
})( jQuery );
‘);
}

Entradas relacionadas