亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 206 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號