При создании интернет магазина я раза три сталкивалась с необходимостью добавить возможность выбора количества товара не переходя на страницу самого товара.
Для простых товаров все решается добавлением кода в файл темы functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
// выбор количества при добавлении простых товаров в корзину с поддержкой AJAX на страницах категорий товаров add_action('woocommerce_before_shop_loop', 'custom_woo_before_shop_link'); function custom_woo_before_shop_link() { add_filter('woocommerce_loop_add_to_cart_link', 'custom_woo_loop_add_to_cart_link', 10, 2); add_action('woocommerce_after_shop_loop', 'custom_woo_after_shop_loop'); } // customise Add to Cart link/button for product loop function custom_woo_loop_add_to_cart_link($button, $product) { // not for variable, grouped or external products if (!in_array($product->product_type, array('variable', 'grouped', 'external'))) { // only if can be purchased if ($product->is_purchasable()) { // show qty +/- with button ob_start(); woocommerce_simple_add_to_cart(); $button = ob_get_clean(); // modify button so that AJAX add-to-cart script finds it $replacement = sprintf('data-product_id="%d" data-quantity="1" $1 ajax_add_to_cart add_to_cart_button product_type_simple ', $product->id); $button = preg_replace('/(class="single_add_to_cart_button)/', $replacement, $button); } } return $button; } // add the required JavaScript function custom_woo_after_shop_loop() { ?> <span class="prism-token prism-tag"><span class="prism-token prism-punctuation"><</span>script<span class="prism-token prism-punctuation">></span></span><span class="prism-token prism-script prism-language-javascript"> <span class="prism-token prism-function">jQuery</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-keyword">function</span><span class="prism-token prism-punctuation">(</span>$<span class="prism-token prism-punctuation">)</span> <span class="prism-token prism-punctuation">{</span> <span class="token php language-php"><span class="prism-token prism-delimiter prism-important"><?php</span> <span class="prism-token prism-comment" spellcheck="true">/* when product quantity changes, update quantity attribute on add-to-cart button */</span> <span class="prism-token prism-delimiter prism-important">?></span></span> <span class="prism-token prism-function">$</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-string">"form.cart"</span><span class="prism-token prism-punctuation">)</span><span class="prism-token prism-punctuation">.</span><span class="prism-token prism-function">on</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-string">"change"</span><span class="prism-token prism-punctuation">,</span> <span class="prism-token prism-string">"input.qty"</span><span class="prism-token prism-punctuation">,</span> <span class="prism-token prism-keyword">function</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-punctuation">)</span> <span class="prism-token prism-punctuation">{</span> <span class="prism-token prism-function">$</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-keyword">this</span><span class="prism-token prism-punctuation">.</span>form<span class="prism-token prism-punctuation">)</span><span class="prism-token prism-punctuation">.</span><span class="prism-token prism-function">find</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-string">"button[data-quantity]"</span><span class="prism-token prism-punctuation">)</span><span class="prism-token prism-punctuation">.</span><span class="prism-token prism-function">data</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-string">"quantity"</span><span class="prism-token prism-punctuation">,</span> <span class="prism-token prism-keyword">this</span><span class="prism-token prism-punctuation">.</span>value<span class="prism-token prism-punctuation">)</span><span class="prism-token prism-punctuation">;</span> <span class="prism-token prism-punctuation">}</span><span class="prism-token prism-punctuation">)</span><span class="prism-token prism-punctuation">;</span> <span class="token php language-php"><span class="prism-token prism-delimiter prism-important"><?php</span> <span class="prism-token prism-comment" spellcheck="true">/* remove old "view cart" text, only need latest one thanks! */</span> <span class="prism-token prism-delimiter prism-important">?></span></span> <span class="prism-token prism-function">$</span><span class="prism-token prism-punctuation">(</span>document<span class="prism-token prism-punctuation">.</span>body<span class="prism-token prism-punctuation">)</span><span class="prism-token prism-punctuation">.</span><span class="prism-token prism-function">on</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-string">"adding_to_cart"</span><span class="prism-token prism-punctuation">,</span> <span class="prism-token prism-keyword">function</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-punctuation">)</span> <span class="prism-token prism-punctuation">{</span> <span class="prism-token prism-function">$</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-string">"a.added_to_cart"</span><span class="prism-token prism-punctuation">)</span><span class="prism-token prism-punctuation">.</span><span class="prism-token prism-function">remove</span><span class="prism-token prism-punctuation">(</span><span class="prism-token prism-punctuation">)</span><span class="prism-token prism-punctuation">;</span> <span class="prism-token prism-punctuation">}</span><span class="prism-token prism-punctuation">)</span><span class="prism-token prism-punctuation">;</span> <span class="prism-token prism-punctuation">}</span><span class="prism-token prism-punctuation">)</span><span class="prism-token prism-punctuation">;</span> </span><span class="prism-token prism-tag"><span class="prism-token prism-punctuation"></</span>script<span class="prism-token prism-punctuation">></span></span> <span class="token php language-php"><span class="prism-token prism-delimiter prism-important"><?php</span> <span class="prism-token prism-punctuation">} </span></span> |
Данный код работает на страницах категорий и на странице магазина. Не работает для вариативных товаров, для товаров выводимых шорткодом.