I'm trying to get the quantity, total and weight for each item in an order.
Quantity and total work perfectly, but when I add $item->get_weight(),
it breaks my plugin.
Here is my code; can anyone explain what's up with get_weight()
?
foreach ($order->items as $item) {
fputs($file, implode("|", array(
$item->get_quantity(),
$item->get_total(),
$item->get_weight(),
)));
}
My Answer:
>get_weight() to get the weight of each item, it returns NULL.
Here's the code I'm using:
php
$order = wc_get_order( $order_id );
foreach( $order->get_items() as $item_id => $item ){
$product = $item->get_product();
$quantity = $item->get_quantity();
$total = $item->get_total();
$weight = $item->get_weight();
echo 'Product: ' . $product->get_name() . ' | Quantity: ' . $quantity . ' | Total: ' . $total . ' | Weight: ' . $weight . '
';
}
To get the weight of each item in an order, you need to make sure that the product associated with the item has a weight set in WooCommerce. If the product does not have a weight set, the `get_weight()` method will return NULL.
You can set the weight for a product in WooCommerce by editing the product in the admin dashboard and entering a weight value in the product data section.
Once you have set the weight for the products, the `get_weight()` method should return the weight of each item in the order.
Rate this post
3 of 5 based on 3581 votesComments