Mathias Asberg
Mathias Asberg

Reputation: 3840

SKU number from WooCommerce Loop

Writing a function for WooCommerce that produce a XML with information. Having problem receiving my SKU number from different item

My code to read data is something like this

   // Query order data 
   $order = new WC_Order( $order_id ); 
   $items = $order->get_items();
   $counter = 1000;


   // Retrive Loop data 
   foreach ( $items as $item ) {
   $product_name = $item['name'];
   $product_id = $item['product_id'];
   $product_qty = $item['qty'];
   $product_variation_id = $item['variation_id'];

   // Output Loop data
   $this->xml->writeElement('Description', $product_name); // etc.. 

The problem is when it comes to receiving the SKU number on every item. Have been trying several way to do this without success, My latest was

   $sku = $product->get_sku();

Whitin the Loop but it just throw me

   Call to a member function get_sku() on a non-object

Anyone one that have a clue how to manage to receive and write out SKU within the loop on my custom XML plugin ?

Upvotes: 1

Views: 1397

Answers (1)

Reigel Gallarde
Reigel Gallarde

Reputation: 65254

how did you declare $product??

try $product = new WC_Product($item['product_id']);

before you do $sku = $product->get_sku();

this will make get_sku() available...

Upvotes: 3

Related Questions