【问题标题】:Get Coupon code applied to the product in woocommerce [duplicate]在woocommerce中获取应用于产品的优惠券代码[重复]
【发布时间】:2019-12-06 17:00:02
【问题描述】:

我正在尝试检索应用于特定产品的商店优惠券代码。我使用了下面的代码,但它不起作用。

$WC_Cart = new WC_Cart();
$var = $WC_Cart->get_applied_coupons();

任何人都可以帮助我获得解决方案。提前致谢!

【问题讨论】:

  • 你能告诉你需要在哪里检索和显示申请的优惠券吗?您似乎只想获取产品-优惠券关系的报告,但我不确定。
  • 我想在订单页面显示优惠券

标签: woocommerce hook-woocommerce coupon


【解决方案1】:

我认为这将解决您的问题。我测试了代码,它的工作原理是:

  1. 回显订单号
  2. echo 已使用此订单的优惠券编号
  3. 为所有订单运行第 1 步和第 2 步

您可能需要修改它。

//function to get orders - completed, pending and processing
function lets_get_all_orders()
{
    $customer_orders = wc_get_orders( array(
    'limit'    => -1,
    'status'   => array('completed','pending','processing')
    ) );
    return $customer_orders;
}

//function to get all used coupons in the orders
function lets_get_all_used()
{
    $orders = lets_get_all_orders();

    //traverse all users and echo coupon codes
    foreach($orders as $order)
    {
        $order_discount = $order->discount_total;
        $order_used_coupons = $order->get_used_coupons();
        $order_id = $order->ID;

        //check if any coupon is used in this order
        if($order_discount>0) 
        {
            echo "Order No: $order_id <br> Used Coupons:";
            //display coupon code(s)
            foreach($order_used_coupons as $order_used_coupon)
            {
                echo " - $order_used_coupon";
                echo "<br>";
            }
        }
    }
}

如果您需要任何帮助,请告诉我。祝你有美好的一天。

【讨论】:

    猜你喜欢
    • 2014-11-26
    • 1970-01-01
    • 2018-05-11
    • 2021-01-21
    • 1970-01-01
    • 2017-07-13
    • 2012-09-11
    • 1970-01-01
    • 2020-05-27
    相关资源
    最近更新 更多