Skip to content

Instantly share code, notes, and snippets.

@swoboda
Created November 23, 2016 11:41
Show Gist options
  • Save swoboda/94b6a9ceb264f2270bc58e1bc098e17e to your computer and use it in GitHub Desktop.
Save swoboda/94b6a9ceb264f2270bc58e1bc098e17e to your computer and use it in GitHub Desktop.
woocommerce-add-to-cart-quantity
<?php
/**
* Dodajemy pole liczby dla produktów na stronę.
*/
function custom_quantity_field_archive() {
$product = wc_get_product( get_the_ID() );
if ( ! $product->is_sold_individually() && 'variable' != $product->product_type && $product->is_purchasable() ) {
woocommerce_quantity_input( array( 'min_value' => 1, 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity() ) );
}
}
add_action( 'woocommerce_after_shop_loop_item', 'custom_quantity_field_archive', 0, 9);
/**
* Dodajemy JavaScript.
*/
function custom_add_to_cart_quantity_handler() {
wc_enqueue_js( '
jQuery( ".post-type-archive-product" ).on( "click", ".quantity input", function() {
return false;
});
jQuery( ".post-type-archive-product" ).on( "change input", ".quantity .qty", function() {
var add_to_cart_button = jQuery( this ).parents( ".product" ).find( ".add_to_cart_button" );
// Dlatego aby działała opcja dodawania produktów do koszyku za pomocą AJAX
add_to_cart_button.data( "quantity", jQuery( this ).val() );
// Dlatego aby działała opcja dodawania produktów do koszyku BEZ AJAX
add_to_cart_button.attr( "href", "?add-to-cart=" + add_to_cart_button.attr( "data-product_id" ) + "&quantity=" + jQuery( this ).val() );
});
' );
}
add_action( 'init', 'custom_add_to_cart_quantity_handler' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment