wordpress - WC_Order does not return the product id by get_items() -
this question has answer here:
i have tried product id , product name following code:
if ( $query->have_posts() ) { $order_id = $query->posts[0]->id; $order = new wc_order( $order_id ); $items = $order->get_items(); } foreach ( $items $item ) { $product_id = $item['product_id']; $product = wc_get_product( $item_id ); $product_name = $item['name']; }
in above code got product name, return 0 $product_id. there other method this?
i can't find solution this.
my edited version:
when tried this:
$order_id = $query->posts[0]->id; $order = new wc_order( $order_id ); $items = $order->get_items(); foreach ( $items $item ) { $item_id = $item['product_id']; $product_name = $item['name']; print_r( $item ); exit; }
i got array:
wc_order_item_product object ( [extra_data:protected] => array ( [product_id] => 0 [variation_id] => 0 [quantity] => 1 [tax_class] => [subtotal] => 0 [subtotal_tax] => 0 [total] => 0 [total_tax] => 0 [taxes] => array ( [subtotal] => array ( ) [total] => array ( ) ) ) [data:protected] => array ( [order_id] => 684 [name] => dark skirt "erebos" - large, white [product_id] => 0 [variation_id] => 0 [quantity] => 1 [tax_class] => [subtotal] => 19 [subtotal_tax] => 0 [total] => 19 [total_tax] => 0 [taxes] => array ( [total] => array ( ) [subtotal] => array ( ) ) ) [cache_group:protected] => order-items [meta_type:protected] => order_item [object_type:protected] => order_item [id:protected] => 1 [changes:protected] => array ( ) [object_read:protected] => 1 [default_data:protected] => array ( [order_id] => 0 [name] => [product_id] => 0 [variation_id] => 0 [quantity] => 1 [tax_class] => [subtotal] => 0 [subtotal_tax] => 0 [total] => 0 [total_tax] => 0 [taxes] => array ( [subtotal] => array ( ) [total] => array ( ) ) ) [data_store:protected] => wc_data_store object ( [instance:wc_data_store:private] => wc_order_item_product_data_store object ( [internal_meta_keys:protected] => array ( [0] => _order_id [1] => _name [2] => _product_id [3] => _variation_id [4] => _quantity [5] => _tax_class [6] => _subtotal [7] => _subtotal_tax [8] => _total [9] => _total_tax [10] => _taxes [11] => _product_id [12] => _variation_id [13] => _qty [14] => _tax_class [15] => _line_subtotal [16] => _line_subtotal_tax [17] => _line_total [18] => _line_tax [19] => _line_tax_data ) [meta_type:protected] => order_item [object_id_field_for_meta:protected] => order_item_id ) [stores:wc_data_store:private] => array ( [coupon] => wc_coupon_data_store_cpt [customer] => wc_customer_data_store [customer-download] => wc_customer_download_data_store [customer-session] => wc_customer_data_store_session [order] => wc_order_data_store_cpt [order-refund] => wc_order_refund_data_store_cpt [order-item] => wc_order_item_data_store [order-item-coupon] => wc_order_item_coupon_data_store [order-item-fee] => wc_order_item_fee_data_store [order-item-product] => wc_order_item_product_data_store [order-item-shipping] => wc_order_item_shipping_data_store [order-item-tax] => wc_order_item_tax_data_store [payment-token] => wc_payment_token_data_store [product] => wc_product_data_store_cpt [product-grouped] => wc_product_grouped_data_store_cpt [product-variable] => wc_product_variable_data_store_cpt [product-variation] => wc_product_variation_data_store_cpt [shipping-zone] => wc_shipping_zone_data_store ) [current_class_name:wc_data_store:private] => wc_order_item_product_data_store [object_type:wc_data_store:private] => order-item-product ) [meta_data:protected] => array ( ) )
of see [data:protected] array, can see [product_id] 0. problem guess. solution this?
it should be:
foreach ( $items $item ) { $product = $item->get_product(); $product_id = $product->get_id(); }
Comments
Post a Comment