2 回答

TA貢獻1951條經(jīng)驗 獲得超3個贊
你確定你$WC_Coupon
的實例化正確嗎?無論優(yōu)惠券是如何添加到系統(tǒng)中的,一旦添加到系統(tǒng)中,它就應該像其他任何優(yōu)惠券一樣起作用。
建議您獲取 $WC_Coupon 的 var_dump 并查看它是否輸出正確的值,否則,您可能沒有為構(gòu)造函數(shù)提供正確的代碼參數(shù)。

TA貢獻1852條經(jīng)驗 獲得超1個贊
如果我正確地解釋了代碼,您想要查找兩種貨幣的兩種產(chǎn)品。這可以在您定義產(chǎn)品和貨幣后使用嵌套的 foreach 循環(huán)來完成。
$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'] . '¤cy=' . $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
分享
編輯
跟隨
- 2 回答
- 0 關注
- 181 瀏覽
添加回答
舉報