GetSkuFromOrderItem.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventorySales\Model;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\InventorySalesApi\Model\GetSkuFromOrderItemInterface;
  10. use Magento\Sales\Api\Data\OrderItemInterface;
  11. use Magento\InventoryCatalogApi\Model\GetSkusByProductIdsInterface;
  12. use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface;
  13. class GetSkuFromOrderItem implements GetSkuFromOrderItemInterface
  14. {
  15. /**
  16. * @var GetSkusByProductIdsInterface
  17. */
  18. private $getSkusByProductIds;
  19. /**
  20. * @var IsSourceItemManagementAllowedForProductTypeInterface
  21. */
  22. private $isSourceItemManagementAllowedForProductType;
  23. /**
  24. * @param GetSkusByProductIdsInterface $getSkusByProductIds
  25. * @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
  26. */
  27. public function __construct(
  28. GetSkusByProductIdsInterface $getSkusByProductIds,
  29. IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
  30. ) {
  31. $this->getSkusByProductIds = $getSkusByProductIds;
  32. $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function execute(OrderItemInterface $orderItem): string
  38. {
  39. try {
  40. $itemSku = $orderItem->getSku();
  41. if ($this->isSourceItemManagementAllowedForProductType->execute($orderItem->getProductType())) {
  42. $itemSku = $this->getSkusByProductIds->execute(
  43. [$orderItem->getProductId()]
  44. )[$orderItem->getProductId()];
  45. }
  46. } catch (NoSuchEntityException $e) {
  47. $itemSku = $orderItem->getSku();
  48. }
  49. return $itemSku;
  50. }
  51. }