2017-10-18 109 views
6

我有一個插件,用於將主張推介優惠券代碼發送給他們輸入的電子郵件。當觀衆收到這封電子郵件時,我想創建一個流程,他們可以點擊電子郵件中的「立即購買」,並且優惠券將自動添加。通過URL中的GET方法應用優惠券折扣,甚至可以在WooCommerce中清空購物車

截至目前,對下的鏈接「立即購買」按鈕,我已經進入了以下內容:

websitename.biz/cart__trashed?code=DISCOUNTCODE 

要處理$code我已經把這個在我functions.php文件:

add_action('woocommerce_before_cart', 'discount'); 
function discount() { 
    global $woocommerce; 
    $code= $_GET["code"]; 
    if(!empty($code)){  
    if($woocommerce->cart->add_discount($code)){ 
    echo '<div class="woocommerce_message"><strong>Applied coupon!</strong></div>'; 
     } 
    } 
} 

我這裏面臨的問題是:

  • 如果沒有什麼車的時候,觀衆訪問該網站,TH電子優惠券不適用。
  • 如果有東西被添加並留在那裏(因爲cookie),那麼優惠券代碼就可以完美應用了。

我相信它是因爲購物車是空的,代碼不起作用。

只希望代碼在觀衆點擊鏈接時應用。

我該如何做到這一點?

+0

你可以通過產品ID與優惠券代碼一起,這樣,如果任何客戶端點擊該鏈接時,首先檢查產品是否存在於購物車與否。如果不是,那麼首先將您的代碼添加到購物車,然後最後應用優惠券。 –

+0

當我只有3種產品時,我發現這只是一種解決方法,但是將優惠券代碼添加到購物車並且他們可以購買他們想要的任何商品並將代碼應用到會話中,這對我來說會不會更容易?這對於客戶來說當然更容易 – Mjall2

+0

但Woocommerce不允許使用優惠券,並且當購物車是空的時候保存到購物車。但是,只要客戶點擊鏈接,您就可以編寫自定義代碼,將優惠券代碼保存在您自己的會話密鑰上,從那裏您可以跟蹤客戶是否有購物車中的任何東西。如果是,則通過從會話密鑰中提取優惠券。 –

回答

5

做到這一點應該是正確的方法:

  • 從URL時設置優惠券代碼購物車會話爲自定義數據。
  • 當客戶將第一件物品添加到購物車時,應用此優惠券代碼中的折扣。
  • 從這個優惠券刪除折扣,如果客戶空購物車

可以,其他的檔案頁,產品頁,我的帳戶頁面,或任何現有的任何URL(如店鋪頁面設置任何現有的優惠券代碼頁面)添加到這個現有的網址:
?code=DISCOUNTCODE在結束
(其中DISCOUNTCODE是您的優惠券代號名稱)

下面是代碼:

// Set coupon code as custom data in cart session 
add_action('wp_loaded', 'add_coupon_code_to_cart_session'); 
function add_coupon_code_to_cart_session() { 
    // Exit if no code in URL or if the coupon code is already set cart session 
    if(empty($_GET["code"]) || WC()->session->get('custom_discount')) return; 

    if(! WC()->session->get('custom_discount')) { 
     $coupon_code = esc_attr($_GET["code"]); 
     WC()->session->set('custom_discount', $coupon_code); 
     // If there is an existing non empty cart active session we apply the coupon 
     if(! WC()->cart->is_empty()){ 
      WC()->cart->add_discount($coupon_code); 
     } 
    } 
} 

// Add coupon code when a product is added to cart once 
add_action('woocommerce_add_to_cart', 'add_coupon_code_to_cart', 10, 6); 
function add_coupon_code_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data){ 
    $coupon_code = WC()->session->get('custom_discount'); 
    $applied_coupons = WC()->session->get('applied_coupons'); 

    if(empty($coupon_code) || in_array($coupon_code, $applied_coupons)) return; 

    WC()->cart->add_discount($coupon_code); 
} 

// Remove coupon code when user empty his cart 
add_action('woocommerce_cart_item_removed', 'check_coupon_code_cart_items_removed', 10, 6); 
function check_coupon_code_cart_items_removed($cart_item_key, $cart){ 
    $coupon_code = WC()->session->get('custom_discount'); 

    if($cart->has_discount($coupon_code) && $cart->is_empty()); 
     $cart->remove_coupon($coupon_code); 
} 

代碼放在您的活動子主題的function.php文件(或活動主題),或在任何插件文件。

這是測試和工程

+1

工程就像一個魅力 – Mjall2

0
add_action('woocommerce_before_cart', 'discount'); 
function discount() { 
    global $woocommerce; 
    $code= $_GET["code"]; 
    if(!empty($code)){  
    if(WC()->session->set('applied_coupons', $code)){ 
    echo '<div class="woocommerce_message"><strong>Applied coupon!</strong></div>'; 
     } 
    } 
} 
+0

嘿,這是行不通的,當我添加產品時,它不迴應應用優惠券,也不適用產品是優惠券添加或結帳。 – Mjall2

1

使用下面的代碼,使coupan有效的在任何情況下

add_filter('woocommerce_coupon_is_valid','coupon_always_valid',10,1); 
function coupon_always_valid($valid){ 
    $valid = true; 
    return $valid ; 
} 
+0

這會實現什麼?我應該只用這些代碼將它附加到我的functions.php文件中嗎? – Mjall2

+0

是的appand這個代碼在functions.php –

相關問題