简体   繁体   中英

Get All Products in Woocommerce add to Array

Please assist - I am trying to get my products into a array to write them to a text file my current code writes them to an array but with no index to in order for me to retrieve them

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM