Files
2026-09-15 10:26:34 +08:00

130 lines
5.0 KiB
PHP

<?php
namespace Stemcell\Checkout\Model\Cart\Totals;
use Magento\Catalog\Helper\Product\ConfigurationPool;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Api\ExtensibleDataInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Event\ManagerInterface as EventManager;
use Magento\Framework\Exception\LocalizedException;
use Stemcell\Checkout\Api\Data\Quote\TotalsInterfaceFactory;
use Psr\Log\LoggerInterface;
use Stemcell\Customer\Api\SalespersonRepositoryInterface;
use Stemcell\Customer\Model\LabRepository;
class ItemConverter extends \Magento\Quote\Model\Cart\Totals\ItemConverter
{
private SalespersonRepositoryInterface $salespersonRepository;
private LabRepository $labRepository;
public function __construct(
private ConfigurationPool $configurationPool,
private EventManager $eventManager,
private \Stemcell\Checkout\Api\Data\Quote\TotalsItemInterfaceFactory $stemcelltotalsItemFactory,
private \Magento\Quote\Api\Data\TotalsItemInterfaceFactory $totalsItemFactory,
SalespersonRepositoryInterface $salespersonRepository,
LabRepository $labRepository,
private DataObjectHelper $dataObjectHelper,
private \Magento\Framework\Serialize\Serializer\Json $serializer
) {
parent::__construct($configurationPool, $eventManager, $totalsItemFactory, $dataObjectHelper, $serializer);
$this->salespersonRepository = $salespersonRepository;
$this->labRepository = $labRepository;
}
/**
* Retrieve formatted item options view
*
* @param \Magento\Quote\Api\Data\CartItemInterface $item
* @return string
*/
private function getFormattedOptionValue($item)
{
$optionsData = [];
/* @var $helper \Magento\Catalog\Helper\Product\Configuration */
$helper = $this->configurationPool->getByProductType('default');
$options = $this->configurationPool->getByProductType($item->getProductType())->getOptions($item);
foreach ($options as $index => $optionValue) {
$params = [
'max_length' => 55,
'cut_replacer' => ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>'
];
$option = $helper->getFormattedOptionValue($optionValue, $params);
$optionsData[$index] = $option;
$optionsData[$index]['label'] = $optionValue['label'];
}
return $this->serializer->serialize($optionsData);
}
protected function addAdditionalItemsData(\Magento\Quote\Model\Quote\Item $quoteItem)
{
$data = [];
$data['sku'] = $quoteItem->getSku();
if ($quoteItem->getSalespersonId()) {
$salesperson = $this->salespersonRepository->get($quoteItem->getSalespersonId());
$data['salesperson_name'] = $salesperson->getName();
} else {
$data['salesperson_name'] = '';
}
if ($quoteItem->getLabId()) {
try {
$lab = $this->labRepository->get($quoteItem->getLabId());
$data['lab_booking_account'] = $lab->getBookingAccount();
} catch (LocalizedException $e) {
}
}
$data['cd_code'] = $quoteItem->getCd();
$data['quote_code'] = $quoteItem->getData('quote');
$data['note'] = $quoteItem->getData('note');
$data['pi_sap_id'] = $quoteItem->getData('pi_sap_id');
if ($quoteItem->getData('customer_discount_percent')) {
$data['customer_discount_percent'] = number_format(
$quoteItem->getData('customer_discount_percent')*1.0,
2, '.', ''
).'%';
} else {
$data['customer_discount_percent'] = '';
}
if ($option = $quoteItem->getOptionByCode('simple_product')) {
$data['name'] = $option->getProduct()->getName();
}
return $data;
}
/**
* Converts a specified quote item model to a totals item data object.
*
* @param \Magento\Quote\Model\Quote\Item $item
* @return \Stemcell\Checkout\Api\Data\Quote\TotalsItemInterface
* @throws \Exception
*/
public function modelToDataObject($item)
{
$this->eventManager->dispatch('items_additional_data', ['item' => $item]);
$items = $item->toArray();
$items = array_merge($items, $this->addAdditionalItemsData($item));
$items['options'] = $this->getFormattedOptionValue($item);
// ObjectManager::getInstance()->get(LoggerInterface::class)->debug('modelToDataObject $items', $items);
unset($items[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
$itemsData = $this->stemcelltotalsItemFactory->create();
// $itemsData->setSalespersonName();
$itemsData->setItemId($item->getEntityId());
$this->dataObjectHelper->populateWithArray(
$itemsData,
$items,
\Stemcell\Checkout\Api\Data\Quote\TotalsItemInterface::class
);
return $itemsData;
}
}