Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

poc add returns qty to sku custom fields #837

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion Model/Product/Sku/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
use Nosto\Tagging\Model\Service\Stock\StockService;
use Nosto\Types\Product\ProductInterface;

use Magento\Sales\Model\OrderRepository;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;
use Magento\Sales\Model\Order\ItemRepository;
use Magento\Framework\Api\SearchCriteriaBuilder;

Check warning on line 59 in Model/Product/Sku/Builder.php

View workflow job for this annotation

GitHub Actions / Code Sniffer

There must be one blank line after the last USE statement; 2 found;


// @codingStandardsIgnoreLine

class Builder
Expand Down Expand Up @@ -84,6 +90,11 @@
/** @var StockService */
private StockService $stockService;

private $orderRepository;
private $orderCollectionFactory;
private $orderItemRepository;
private $searchCriteriaBuilder;

/**
* Builder constructor.
* @param NostoDataHelper $nostoDataHelper
Expand All @@ -105,7 +116,11 @@
AttributeServiceInterface $attributeService,
AvailabilityService $availabilityService,
ImageService $imageService,
StockService $stockService
StockService $stockService,
OrderRepository $orderRepository,
OrderCollectionFactory $orderCollectionFactory,
ItemRepository $orderItemRepository,
SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->nostoDataHelper = $nostoDataHelper;
$this->nostoPriceHelper = $priceHelper;
Expand All @@ -116,6 +131,10 @@
$this->availabilityService = $availabilityService;
$this->imageService = $imageService;
$this->stockService = $stockService;
$this->orderRepository = $orderRepository;
$this->orderCollectionFactory = $orderCollectionFactory;
$this->orderItemRepository = $orderItemRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}

/**
Expand Down Expand Up @@ -174,6 +193,8 @@
}
}
}
$nostoSku->addCustomField('returns', $this->getReturnRate($product->getId()));
$nostoSku->addCustomField('##id is:', $product->getId());
if ($this->nostoDataHelper->isInventoryTaggingEnabled($store)) {
$nostoSku->setInventoryLevel($this->stockService->getQuantity($product, $store));
}
Expand Down Expand Up @@ -203,4 +224,45 @@

return ProductInterface::OUT_OF_STOCK;
}

public function getReturnRate($productId)
{
$orderCollection = $this->orderCollectionFactory->create();
$orderCollection->addFieldToFilter('status', ['in' => ['complete', 'closed']]);

$orderItems = [];
foreach ($orderCollection as $order) {
foreach ($order->getAllVisibleItems() as $item) {
$orderItems[] = $item->getProductId();
}
}

$totalOrders = count($orderItems);
$returnedOrders = $this->getReturnedOrders($productId);
return (string)$returnedOrders;
// if ($totalOrders > 0) {
// $returnRate = $returnedOrders / $totalOrders * 100;
// return $returnRate;
// }

// return 0;
}

private function getReturnedOrders($productId)
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter('product_id', $productId)
// ->addFilter('product_id', "1812")
// ->addFilter('qty_invoiced', 0, '>')
->create();

$returnedOrders = 0;
$orderItems = $this->orderItemRepository->getList($searchCriteria)->getItems();

Check failure on line 260 in Model/Product/Sku/Builder.php

View workflow job for this annotation

GitHub Actions / Phan Analysis

Call to method getItems on non-class type \Magento\Sales\Api\Data\OrderItemInterface[]

foreach ($orderItems as $item) {
$returnedOrders += $item->getQtyRefunded();
}

return $returnedOrders;
}
}
Loading