Woocommerce:可能添加免费礼品的第n个订单?
问题描述:
我一直在想这一段时间,但迄今绘制的空白......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);
}
}
答
为此,您需要直接执行一些代码,因此您需要设置计数而不是达到目标。
您需要在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);
}
}
谢谢阿基,看起来真的很有帮助!所以我需要调整这个以适应我的网站,并在“... popup code”和“} else ...”之间添加一些内容以实际显示礼品通知? :-)可以肯定 - 我显然是一个小菜鸟!谢谢,稍后会尝试这个,所以会报告我的进度! –
把你的代码放在if条件中......如果它对你有用,那就把它投给我的答案:D所以其他人可以使用这个代码。 –
非常感谢Aki - 现在开始进行一些家居改进,所以今晚晚些时候会有代码破解..所有最好的:-) –