The following code works well on localhost but not on the main site! Sends only one product number.
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_loop_ajax_add_to_cart', 10, 2 );
function quantity_inputs_for_loop_ajax_add_to_cart( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
// Get the necessary classes
$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' : '',
) ) );
// Adding embeding <form> tag and the quantity field
$html = sprintf( '%s%s<a title="افزودن به سبد خرید" data-toggle="tooltip" data-original-title="افزودن به سبد خرید" rel="nofollow" href="%s" data-quantity="%s" data-product_id="%s" data-product_sku="%s" class="%s">%s</a>%s',
'<div class="cart-wrap" style="display:inline-flex; width:auto;">',
woocommerce_quantity_input( array(), $product, false ),
esc_url( $product->add_to_cart_url() ),
esc_attr( isset( $quantity ) ? $quantity : 1 ),
esc_attr( $product->get_id() ),
esc_attr( $product->get_sku() ),
esc_attr( isset( $class ) ? $class : 'button' ),
esc_html( $product->add_to_cart_text() ),
'</div>'
);
}
return $html;
}
// ------------------------------------------------------------
add_action( 'wp_footer' , 'archives_quantity_fields_script' );
function archives_quantity_fields_script(){
?>
<script type='text/javascript'>
jQuery( document ).ready( function( $ ) {
$( document ).on( 'change', '.quantity .qty', function() {
$( this ).parent( '.quantity' ).next( '.add_to_cart_button' ).attr( 'data-quantity', $( this ).val() );
//alert("Changed");
});
});
</script>
<?php
}//end: function archives_quantity_fields_script()
I replaced the following code, but only one product number will be sent again:
<script type='text/javascript' src='https://www.example.com/wp-includes/js/jquery/jquery.min.js?ver=3.5.1'>
Does not work with the following code at all!:
<script type='text/javascript'>
![1]: https://i.stack.imgur.com/0lhja.png
source https://stackoverflow.com/questions/67827282/how-to-add-js-code-to-the-main-page-of-the-site-to-add-a-number-to-the-cart-butt
No comments:
Post a Comment