Javascript Woocommerce:检测“添加到购物车”按钮的单击位置,并运行不同的代码
在电子商务商店中:Javascript Woocommerce:检测“添加到购物车”按钮的单击位置,并运行不同的代码,javascript,php,jquery,wordpress,woocommerce,Javascript,Php,Jquery,Wordpress,Woocommerce,在电子商务商店中: 主页上显示了一些项目,每个项目下面都有一个“添加到购物车”按钮。单击此按钮时,项目将添加到购物车。如果再次单击此按钮,则购物车中已存在的商品数量将增加1我相信这就是循环。到目前为止,一切顺利 在单一产品页面上,有一个“添加到购物车”按钮。单击此按钮时,项目将添加到购物车。还有一个数量输入文本框,可用于更改数量。这也很好 问题: 我需要区分在循环中单击的“添加到购物车”按钮(当前在主页上,但也可用于其他页面,如存档页面等)与在单个产品页面上单击的“添加到购物车”按钮。基于这
- 主页上显示了一些项目,每个项目下面都有一个“添加到购物车”按钮。单击此按钮时,项目将添加到购物车。如果再次单击此按钮,则购物车中已存在的商品数量将增加1我相信这就是循环。到目前为止,一切顺利
- 在单一产品页面上,有一个“添加到购物车”按钮。单击此按钮时,项目将添加到购物车。还有一个数量输入文本框,可用于更改数量。这也很好
- 如果单击循环中出现的“添加到购物车”按钮,则使用
获取购物车中已存在的该商品的数量,将其递增1,并将其发送到自定义函数,该函数将执行附加处理并再次将详细信息保存到购物车$Cart\u item\u key
- 如果单击单个产品页面中出现的“添加到购物车”按钮,则使用
获取购物车中已存在的该商品的数量,将其乘以3,并将其发送到自定义功能,该功能将执行附加处理并再次将详细信息保存到购物车$Cart\u item\u key
- 在上述两种情况下,数量都会根据不同的逻辑进行更改,并且该数量需要发送到自定义函数调用
add_action('woocommerce_add_to_cart', 'custom_action_add_to_cart', 20, 6);
function custom_action_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data)
{
$cart = WC()->cart->get_cart();
$product = wc_get_product($product_id);
// NEED TO RUN CUSTOM CODE HERE BASED ON THE CHECKS
if (add to cart within loop is clicked) {
// Get existing $quantity_from_cart from cart using $cart_item_key, but how????
$new_quantity = $quantity_from_cart + 1;
}
else if (add to cart on single product page is clicked) {
// Get existing $quantity_from_cart from cart using $cart_item_key, but how????
$new_quantity = $quantity_from_cart * 3;
}
// Need to send the $new_quantity along with the $cart_item_key to the custom function so that the data can be saved using $cart_item_key
my_custom_function($new_quantity, $cart_item_key);
}
function my_custom_function($new_quantity, $cart_item_key)
{
echo $new_quantity;
WC()->cart->cart_contents[$cart_item_key]['custom_quantity'] = $new_quantity;
WC()->cart->set_session();
}
上面代码的问题是如果我没有如果。。。否则,如果…
逻辑,则无论“添加到购物车”按钮位于何处,都会执行代码。换句话说,无论我是单击循环中的“添加到购物车”按钮(主页、归档页面或使用循环的任何页面),还是单击单个产品页面中的“添加到购物车”按钮,如果。。。否则,如果…逻辑
因此,我希望在单击循环中的“添加到购物车”按钮时运行单独的代码(无论其位置,是否为主页、档案等),并在单击单个产品页面上的“添加到购物车”按钮时运行不同的代码。我如何才能做到这一点?
期待这样的事情:
- 如果单击循环中出现的按钮->执行此操作
- 如果单击了单个产品页面中出现的按钮->则执行该操作
function custom_action_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data)
{
$cart = WC()->cart->get_cart();
$product = wc_get_product($product_id);
$referer = wp_get_referer();
// HOMEPAGE
if (strpos($referer ,'http://yourwebsite.com/') !== false) {
$new_quantity = $quantity_from_cart + 1;
}
//from some product page like http://yourwebsite.com/product/my-product-page
else if (strpos($referer ,'http://yourwebsite.com/product/') !== false) {
$new_quantity = $quantity_from_cart * 3;
}
// Need to send the $new_quantity along with the $cart_item_key to the custom function so that the data can be saved using $cart_item_key
my_custom_function($new_quantity, $cart_item_key);
}
请参阅:您可以这样尝试:
add_action('woocommerce_add_to_cart', 'custom_action_add_to_cart', 20, 6);
function custom_action_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
$cart = WC()->cart->get_cart();
$product = wc_get_product($product_id);
$referer = $_SERVER['HTTP_REFERER'];
$route = parse_url( $referer );
$path = $route['path'] ?? 'home' ;
$args = array_filter( ( explode('/', $path) ) );
if (in_array( 'product', $args) ) {
// Product Page
} elseif (in_array('product-category', $args)) {
// Product Category
} else {
// Default
}
}
但是你需要检查你的设置
Settings>Permalinks
您可以使用is\u product()
,is\u product\u category()
函数
function custom_action_add_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data)
{
$cart = WC()->cart->get_cart();
$product = wc_get_product($product_id);
if ( is_product() ) {
global $product;
$id = $product->get_id();
foreach ( WC()->cart->get_cart() as $cart_item ) {
if($cart_item['product_id'] == $id ){
$quantity_from_cart = $cart_item['quantity'];
break; // stop the loop if product is found
}
}
$new_quantity = $quantity_from_cart * 3;
}
else if (is_product_category()) {
$new_quantity = $quantity_from_cart + 1;
}
my_custom_function($new_quantity, $cart_item_key);
}
我可以想出几个解决办法。但这里有一个:
add_action( 'woocommerce_after_add_to_cart_button', 'rmg_woocommerce_after_add_to_cart_button' );
function rmg_woocommerce_after_add_to_cart_button() {
$button_location = 0;
// if (is_home() || is_front_page()) {
// // we're at the home page
// $button_location = 1;
// }
if (is_product()) {
// where at product page
$button_location = 2;
} else {
// pages other than product page
$button_location = 1;
}
echo '<input type="hidden" name="button-location" value="'. $button_location .'" />';
}
请注意,这只是一个想法,而不是一个完整的解决方案。。。您需要注意ajax按钮。手动检查主页、产品页面等是一种过分的操作。我尽量不硬编码的网页名称,而是让WP内部处理。我厌倦了你的解决方案。当我在单产品页面并将产品添加到购物车时,我希望代码
$new\u quantity=$quantity\u from\u cart*3代码>开始工作。但事实并非如此。它只是增加购物车中当前的现有数量,但不将数量乘以3。如何解决这个问题?现在试试,它会将数量乘以3。我刚刚试过你的修正方案。但不知怎么的,结果和我之前的评论是一样的。似乎出于某种奇怪的原因,代码if(is_product())
没有被执行。我似乎找不到原因!它是一个AJAX添加到购物车按钮,这有关系吗?手动检查主页、产品页面等是一种过分的做法。我试图不硬编码页面名称,而是让WP在内部处理它。似乎出于某种奇怪的原因,代码if(is_product())没有被执行。我似乎找不到原因!它是一个AJAX添加到购物车按钮是否重要?使用AJAX,您需要在请求中传递$button\u location
的值。。
$button_location = $_REQUEST['button-location'];
if ($button_location && $button_location === 2) {
// add to cart button clicked at or came from product page..
$new_quantity = $quantity_from_cart + 1;
}