在变量与WooCommerce

时间:2017-10-16 20:29:37

标签: php json wordpress woocommerce advanced-custom-fields

我正在尝试从json文件中获取数据以加载到我的WooCommerce网站。我想从我抓取的网站产品的匹配名称中获取价格。

我需要产品的名称以匹配我添加到我在Wordpress产品页面中添加的产品页面的高级自定义字段值,然后在名称与我添加的属性匹配时获取价格。

以下代码部分有效,但由于某种原因,对高级自定义字段值的调用无效。它显示文本的值,而不是将它与json中的名称字段匹配任何建议吗?

$str = file_get_contents('http://gold.explorethatstore.com/wp-content/themes/divi-ETS-child-theme/run_results_apmex.json');

// decode JSON
$json = json_decode($str, true);

// default value
$coinPrice = "Not Available";
$vendorName = the_field('apmex_vendor_name');
// loop the json array
foreach ($json['coin'] as $value){
    // check the condition
    if ($value['name'] == $vendorName){
        $coinPrice = $value['price']; // get the price
        break; // exit the loop
    }
}

echo $coinPrice;

1 个答案:

答案 0 :(得分:1)

在代码中使用$vendorName = the_field('apmex_vendor_name');将无效,因为ACF函数the_field()相当于echo get_field();,如果您想在变量中设置值,则无法使用$vendorName = get_field('apmex_vendor_name');

相反,您应该只使用get_field()

function moneyform($number, $symbol = true) {
    return str_replace(".00", "", money_format(($symbol? '%.2n' : "%!n"), $number));
}

moneyform(1300999);
-->$1,300,999

moneyform(2500.99);
-->$2,500.99

moneyform(2500.99, false);
-->2,500.99

现在它应该有用......

你的其余代码似乎是正确的(我已经测试过了)......