I’m trying to get the total weight of the items in checkout_submit_all_after
With this code:
$orderItems = $order->getAllItems();
$weight = 0.0;
$length = 0.0;
$width = 0.0;
$height = 0.0;
$dimensions = array();
foreach ($orderItems as $item) {
$product = $item->getProduct();
$weight += (float) ($product->getData('weight')*$item->getQtyOrdered());
$length += (float) $product->getData('ts_dimensions_length');
$width += (float) $product->getData('ts_dimensions_width');
$height += (float) $product->getData('ts_dimensions_height');
}
$dimensions[] = [
'weigth' => $weight,
'length' => $length,
'width' => $width,
'height' => $height
];
$this->logger->info("Weight: ".$weight);
$encodedData = $this->_jsonHelper->jsonEncode($dimensions);
$this->logger->info("Dimensiones: ".$encodedData);
When I print weight I get 0, I found that is because I’m getting 0 products, and another funny thing, is that my dimensions don’t get summed if there are more than 1 product with same SKU. If I have 5 items with length 2, the result is 2, not 10. What I’m doing wrong here?
Please help.
Greetings!