获取Woocommerce中的所有产品添加到阵列

问题描述 投票:0回答:1

请帮助我试图将我的产品放入一个数组,将它们写入文本文件,我当前的代码将它们写入一个数组,但没有索引,以便我退出它们

     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);
php woocommerce
1个回答
0
投票
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);
}
© www.soinside.com 2019 - 2024. All rights reserved.