为了账号安全,请及时绑定邮箱和手机立即绑定

Woocommerce:检测单击“添加到购物车”按钮的位置并运行不同的代码

Woocommerce:检测单击“添加到购物车”按钮的位置并运行不同的代码

PHP
开心每一天1111 2022-07-22 10:36:14
在电子商务商店中:主页上显示了一些项目,每个项目下方都有一个“添加到购物车”按钮。单击此按钮时,该项目将添加到购物车。如果再次单击此按钮,购物车中已存在的商品的数量会增加 1。我相信这是循环。到目前为止,一切都很好。在单一产品页面上,有一个“加入购物车”按钮。单击此按钮时,该项目将被添加到购物车。还有一个数量输入文本框,可用于更改数量。这也很好。问题:我需要区分在循环中单击的“添加到购物车”按钮(当前在主页上,但也可以在其他页面上使用,例如存档页面等)与单击的“添加到购物车”按钮在单一产品页面上。基于这种差异化,这是我需要做的:如果单击循环中出现的“添加到购物车”按钮,请使用 获取购物车中已存在的此商品 的数量$cart_item_key,将其递增 1 并将其发送到将执行额外处理并保存详细信息的自定义函数再次购物车。如果单击了单个产品页面中出现的“添加到购物车”按钮,请使用 获取购物车中已存在的该商品 的数量$cart_item_key,将其乘以 3 并将其发送到将进行额外处理并保存的自定义函数再次购物车的详细信息。在上述两种情况下,数量都会根据不同的逻辑进行更改,并且需要将此数量发送到自定义函数调用。我尝试了什么:我尝试了以下代码: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();}上面代码的问题是,如果我没有if... else if...逻辑,那么无论“添加到购物车”按钮位于何处,都会执行代码。换句话说,无论我单击位于循环(主页、存档页面或任何使用循环的页面)中的“添加到购物车”按钮,还是单击位于单个产品页面中的“添加到购物车”按钮,上面的代码在没有if... else if...逻辑的情况下执行。因此,我想在单击位于循环中的“添加到购物车”按钮时运行单独的代码(无论其位置如何,无论是主页、档案等),并在“添加到购物车”按钮时运行不同的代码单击位于“单一产品”页面上的 。我怎样才能做到这一点?期待这样的事情:如果单击出现在循环内的按钮-> 执行此操作。如果单击单个产品页面中出现的按钮 -> 执行此操作。
查看完整描述

3 回答

?
DIEA

TA贡献1820条经验 获得超3个赞

你可以这样试试:


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.


查看完整回答
反对 回复 2022-07-22
?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

您可以使用wp_get_referercheck_ajax_referer()例如:

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);}

请参考:Wordpress Nonce 相关函数


查看完整回答
反对 回复 2022-07-22
?
暮色呼如

TA贡献1853条经验 获得超9个赞

你可以使用is_product(),is_product_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);

}


查看完整回答
反对 回复 2022-07-22
  • 3 回答
  • 0 关注
  • 245 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号