Ajax在Woocommerce2.5.2中添加购物车问题


Ajax add to cart issue in Woocommerce 2.5.2

我正在使用Woocommerce,

我已经自定义了我的网站以在表格中显示产品标题,当单击时,产品通过ajax放置在Woocommerce迷你购物车小部件中,而无需刷新页面。

当我更新到"Woocommerce 2.5.2"时,Ajax添加到购物车不再有效,页面刷新。

我当前用于添加产品的链接是:

$html = $html . '<div class="numlist_thumb"><a data-product_id="' . $id1 . '" data-product_sku="' . $number1 . '" class="numbertabanchor add_to_cart_button dp-button product_type_simple" rel="nofollow" href="/?add-to-cart=' . $id1 . '">' . $number1 . '</a></div>';

关于如何在不刷新页面的情况下通过ajax添加产品的任何建议,我将不胜感激。

这是与woocommerce冲突的主题。我也面临这个问题。因此,每当woocommerce推出任何重大更新时,主题也会根据最新的woocommerce进行更新。因此,您必须根据woocommerce更新主题。

将"ajax_add_to_cart"类添加到<a>标签中

作为参考,请查看我的单一产品/添加到购物车/简单代码.php

<button type="submit" class="single_add_to_cart_button add_to_cart_button button ajax_add_to_cart button--itzel button--text-thick" data-quantity="1" data-product_id="<?php echo $product->id; ?>"><i class="button__icon icon icon-cart"></i><span><?php echo esc_html( $product->single_add_to_cart_text() ); ?></span></button>

您可以通过[wp_ajax][1]操作轻松创建添加到购物车功能,在回调函数中,您只需添加以下代码即可

add_action( "wp_ajax_custom_add_to_cart", "custom_add_to_cart_callback" );
add_action( "wp_ajax_nopriv_custom_add_to_cart", "custom_add_to_cart_callback" );
function custom_add_to_cart_callback(){
     global $woocommerce;
     $woocommerce->cart->add_to_cart( $_POST[ 'prod_id' ] );
     wp_die();
}

在此之后,您可以轻松地对服务器进行 ajax 调用,在该服务器中,您将产品 id 发布为变量prod_id并添加另一个名为 action 的变量,值为 custom_add_to_cart