2017-09-01 99 views
0

我一直在想這一段時間,但迄今繪製的空白......Woocommerce:可能添加免費禮品的第n個訂單?

有誰知道是否有可能以一份免費的禮物完成後自動添加到任意Woocommerce產品訂單?每個產品的網站範圍還是不同的值?

I.E.
產品加入購物車是該物品
訂購的100次支付/完成
用戶要麼重定向到免費贈送頁面或彈出或接收電子郵件(無論哪個是最實用)

希望:-)這似乎相關 - 任何指針非常讚賞!

埃德

編輯: 所以,從亞紀(下)和在線搜索一些幫助後,我想出了這一點,但仍然無法得到它的工作...我缺少什麼?

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); 

function custom_process_order($order_id) { 

//First We are chceking order is paid or not with the order metafields 
$transactionId = get_post_meta($order_id,'_transaction_id', true); 
if(isset($transactionId)) 
{ 
    //getting the count of order 
    $orderCount = get_option('orderCount'); 
    if($orderCount == 99) 
    { 
     //let's reset order count option zero 
     $orderCount = 0; 

     //send email or redirect code or popup code 
$message = "You're the 100th order of this item, so please, have one on us..Free gift!"; 
echo "<script type='text/javascript'>alert('$message');</script>"; 
    }else 
    { 
     $orderCount = (int) $orderCount+1; 
    } 
    update_option('orderCount', $orderCount); 
} 
} 

回答

0

爲此,您需要直接執行一些代碼,因此您需要設置計數而不是達到目標。

您需要在update_option函數的幫助下設置訂單計數,而不是您可以執行任何操作。

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1); 

function custom_process_order($order_id) { 

//First We are chceking order is paid or not with the order metafields 
$transactionId = get_post_meta($order_id,'_transaction_id', true); 
if(isset($transactionId)) 
{ 
    //getting the count of order 
    $orderCount = get_option('orderCount'); 
    if($orderCount == 99) 
    { 
     //let's reset order count option zero 
     $orderCount = 0; 

     //send email or redirect code or popup code 

    }else 
    { 
     $orderCount = (int) $orderCount+1; 
    } 
    update_option('orderCount', $orderCount); 
} 
} 
+0

謝謝阿基,看起來真的很有幫助!所以我需要調整這個以適應我的網站,並在「... popup code」和「} else ...」之間添加一些內容以實際顯示禮品通知? :-)可以肯定 - 我顯然是一個小菜鳥!謝謝,稍後會嘗試這個,所以會報告我的進度! –

+0

把你的代碼放在if條件中......如果它對你有用,那就把它投給我的答案:D所以其他人可以使用這個代碼。 –

+0

非常感謝Aki - 現在開始進行一些家居改進,所以今晚晚些時候會有代碼破解..所有最好的:-) –

相關問題