Php 如何在Woocommerce产品循环中添加自定义AJAX减少按钮?
我在Woocommerce产品循环中添加了一个加号和减号按钮,加号按钮工作正常。 现在我想在用户单击减号按钮时删除一个产品 这是用于添加产品和增加产品数量的自定义按钮-Php 如何在Woocommerce产品循环中添加自定义AJAX减少按钮?,php,wordpress,function,woocommerce,cart,Php,Wordpress,Function,Woocommerce,Cart,我在Woocommerce产品循环中添加了一个加号和减号按钮,加号按钮工作正常。 现在我想在用户单击减号按钮时删除一个产品 这是用于添加产品和增加产品数量的自定义按钮- function wc_shop_demo_button() { global $product; echo '<div class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id=' . $
function wc_shop_demo_button() {
global $product;
echo '<div class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id=' . $product->get_id() . ' rel="nofollow" >Plus</div>';
echo '<div class="minus button" data-product_id=' . $product->get_id() . ' rel="nofollow" >Minus</div>';
}
add_action( 'woocommerce_after_shop_loop_item', 'wc_shop_demo_button', 20 );
add_action( 'woocommerce_after_add_to_cart_button', 'wc_shop_demo_button', 20 );
功能wc\u shop\u demo\u按钮(){
全球$产品;
回声“加”;
回声‘负’;
}
添加操作('woocommerce'u在'u shop'u loop'u项目之后,'wc'u shop'u demo'u按钮',20);
添加操作(“添加到购物车按钮后的woocommerce”按钮、“wc商店演示按钮”,20);
我的问题是,是否可以通过使用AJAX的自定义按钮来减少购物车中的产品数量?
如果是,如何显示?在WooCommerce商店页面中显示加减数量按钮:将此代码放入活动主题的functions.php中(并删除代码)
兄弟,你能告诉我如何在产品循环中完成它吗?它不起作用兄弟:(什么都没有发生。我也尝试过删除-if(!is_product())return;但是没有luckI再次编辑我的代码。它应该可以工作。我已经测试过了。
/**
* Add quantity field on the shop page.
*/
function custom_shop_page_add_quantity_field() {
/** @var WC_Product $product */
$product = wc_get_product( get_the_ID() );
if ( ! $product->is_sold_individually() && 'variable' != $product->get_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_shop_page_add_quantity_field', 12 );
/**
* Add required JavaScript.
*/
function custom_shop_page_quantity_add_to_cart_handler() {
wc_enqueue_js( '
$(".woocommerce .products").on("click", ".quantity input", function() {
return false;
});
$(".woocommerce .products").on("change input", ".quantity .qty", function() {
var add_to_cart_button = $(this).parents( ".product" ).find(".add_to_cart_button");
// For AJAX add-to-cart actions
add_to_cart_button.data("quantity", $(this).val());
// For non-AJAX add-to-cart actions
add_to_cart_button.attr("href", "?add-to-cart=" + add_to_cart_button.attr("data-product_id") + "&quantity=" + $(this).val());
});
// Trigger on Enter press
$(".woocommerce .products").on("keypress", ".quantity .qty", function(e) {
if ((e.which||e.keyCode) === 13) {
$( this ).parents(".product").find(".add_to_cart_button").trigger("click");
}
});
' );
}
add_action( 'init', 'custom_shop_page_quantity_add_to_cart_handler' );