php - 将 Woocommerce 中的所有产品添加到 Array
问题描述
请协助 - 我正在尝试将我的产品放入数组中以将它们写入文本文件我当前的代码将它们写入数组但没有索引以便我检索它们
function products()
{
$cache= __DIR__."/json.cacheAllProducts.txt";
$count = 0;
$full_product_list = array();
$loop = new WP_Query(array('post_type' => array('product',
'product_variation'), 'posts_per_page' => -1));
$handle = fopen($cache, 'wb') or die('no fopen');
while ($loop->have_posts()) : $loop->the_post();
$theid = get_the_ID();
$product = new WC_Product($theid);
$thetitle = get_the_title();
$produtslist = array(
'ID'=> $theid,
'price'=>$curlcontent
);
fwrite($handle,print_r(($produtslist), TRUE));
$json_cache = file_get_contents($cache);
$data2 = json_decode($json_cache);
echo $json_cache;
}
endwhile;
fclose($handle);
解决方案
function products()
{
$cache = __DIR__ . "/json.cacheAllProducts.txt";
$count = 0;
$full_product_list = array();
$loop = new WP_Query(array('post_type' => array(
'product',
'product_variation'
), 'posts_per_page' => -1));
$handle = fopen($cache, 'wb') or die('no fopen');
while ($loop->have_posts()) : $loop->the_post();
$theid = get_the_ID();
$product = new WC_Product($theid);
$thetitle = get_the_title();
$produtslist[] = array(
'ID' => $theid,
'price' => $curlcontent
);
fwrite($handle, print_r(($produtslist), true));
$json_cache = file_get_contents($cache);
$data2 = json_decode($json_cache);
echo $json_cache;
endwhile;
fclose($handle);
}
推荐阅读
- python-3.x - 具有列表值的字典理解
- php - 分配 Carbon 日期对象时使用 now() 更新日期字段
- haskell - 使用 Aeson 解析嵌套数组和对象
- python - 如何有效地将 sympy 中的术语分组以进行 lambdafication?
- r - 正则表达式删除特定字符之前的所有数字(在 R 中工作)
- r - 如何将向量除以R中的标量并绘制它
- docker - 豆荚死了。容器在 K8s 1.13 中启动
- github - 如何将协作者添加到特定的 Github 存储库并将其也显示在他们的个人资料中
- plsql - 搜索满足多个条件的数据
- python - 用于自定义创建类的 Python len() Big O 表示法