I am working on module for some affiliate program.
Everything is fine , until i try to bring the ordered items in the succes page , just for testing my json that i need to sent further to another API via some JS.
The problem: I order 3 or more items , and in the result of the json , it comes just the last item from the order .
here is the code of the method where i bring all data from the order:
(I ordered 1 item from every product) and i want in $items variable to be all my ordered items
public function getOrderData()
{
$order = $this->checkoutSession->getLastRealOrder();
$orderId = $order->getIncrementId();
if ($orderId) {
//get list of categories and brands
$brands = [];
$categories = [];
$categoriesToMerge = [];
/** @var Item $item */
foreach ($order->getAllVisibleItems() as $item) {
$product = $item->getProduct();
$brandAttribute = $this->getBrandAttribute();
if ($brandAttribute && $product->getData($brandAttribute)) {
$brands[] = $product->getCustomAttribute($brandAttribute)->getValue();
}
$categoriesToMerge[] = $product->getCategoryIds();
}
$categories = array_merge([], ...$categoriesToMerge);
$brandsArr = [];
if ($brands) {
$brands = $this->brandCollectionFactory->create()
->addFieldToSelect('entity_id')
->addFieldToSelect('name')
->addFieldToFilter('entity_id', $brands);
foreach ($brands as $brand) {
$brandsArr[$brand->getEntityId()] = $brand->getName();
}
}
$categories = array_unique($categories);
$catArr = [];
if ($categories) {
$categories = $this->categoryCollectionFactory->create()
->addFieldToSelect('name')
->addFieldToFilter('entity_id', $categories);
foreach ($categories as $category) {
$catArr[$category->getId()] = $category->getName();
}
}
$itemsDesc = [];
$items = [];
foreach ($order->getAllVisibleItems() as $item) {
$itemsDesc[] = $item->getName() . 'x' . $item->getQtyOrdered();
$product = $item->getProduct();
$catIds = $product->getCategoryIds();
$productCatArr = [];
foreach ($catIds as $id) {
$productCatArr[] = $catArr[$id];
}
$items['quantity'] = (int)$item->getQtyOrdered();
$items['product_id'] = $product->getId();
$items['value'] = $item->getPrice();
$items['name'] = $product->getName();
$items['category'] = $productCatArr;
if ($this->getIsBrandAttributeEnabled()) {
$items['brand'] = $this->config->getTrackingBrand($product);
}
}
// get order total without shipping and VAT
$orderTotal = ($order->getGrandTotal() - $order->getShippingAmount())/1.19;
return [
'value' => number_format($orderTotal, 2, '.', ''),
'transaction_id' => $order->getIncrementId(),
'placed_at' => $order->getCreatedAt(),
'currency_code' => $order->getBaseCurrencyCode(),
'description' => urlencode(implode(',', $itemsDesc)),
'items' => $items
];
} else {
return null;
}
}
[![array resulted after the order , in succes page][1]][1]
Where is the problem? I tried everything. Thanks
P.S: Fixed , i forgot to push the items to the array.