为了账号安全,请及时绑定邮箱和手机立即绑定

手动设置 Coupon used_by Customer ID

手动设置 Coupon used_by Customer ID

PHP
倚天杖 2022-12-23 16:20:00
如果要通过 REST API 手动添加优惠券代码或从管理员创建新订单端点,我正在寻找有关在 Woocommerce 上设置优惠券 used_by particular customer user_id 的文档。但我找不到正确的方法。希望有人能指点我。下面是我到目前为止开发的代码,但它返回了 NULL。$WC_Coupon = new WC_Coupon($request['code']); $WC_Coupon->set_used_by( $request['customer_id'] );
查看完整描述

2 回答

?
饮歌长啸

TA贡献1951条经验 获得超3个赞

你确定你$WC_Coupon的实例化正确吗?无论优惠券是如何添加到系统中的,一旦添加到系统中,它就应该像其他任何优惠券一样起作用。

建议您获取 $WC_Coupon 的 var_dump 并查看它是否输出正确的值,否则,您可能没有为构造函数提供正确的代码参数。


查看完整回答
反对 回复 2022-12-23
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

如果我正确地解释了代码,您想要查找两种货币的两种产品。这可以在您定义产品和货币后使用嵌套的 foreach 循环来完成。


$cacheDirectory = $_SERVER['DOCUMENT_ROOT'] . '/cache/';

$url = 'https://remotedomain.com/?get=price';


const MAX_CACHE_TIME = 1600;


// Optional

$output = [];


$productList = [

    [

        'id'   => 10,

        'name' => 'SM',

    ],

    [

        'id'   => 20,

        'name' => 'LG',

    ]

];


$currencies = [

    'US' => 1,

    'EU' => 2,

];


foreach ($productList as $product) {

    foreach ($currencies as $currencyName => $currencyId) {

        $cacheFile = $cacheDirectory . $product['name'] . '_' . $currencyName . '.cache';


        if (!file_exists($cacheFile) || filemtime($cacheFile) > MAX_CACHE_TIME) {

            // No cache or too old

            $data = file_get_contents($url . '&product=' . $product['id'] . '&currency=' . $currencyId);

            $relevantData = substr($data, 17, 2);

            file_put_contents($cacheFile, $relevantData);

            // Optional, put the data in an array

            $output[$product['id']][$currencyId] = $relevantData;

        } else {

            $output[$product['id']][$currencyId] = file_get_contents($cacheFile);

        }


    }

}


// Read output with $output[10]['US'] for example

分享

编辑

跟随


查看完整回答
反对 回复 2022-12-23
  • 2 回答
  • 0 关注
  • 94 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信