init
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Checkout\Model;
|
||||
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Cart extends \Magento\Checkout\Model\Cart
|
||||
{
|
||||
/**
|
||||
* Update cart items information
|
||||
*
|
||||
* @param array $data
|
||||
* @return $this
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @SuppressWarnings(PHPMD.NPathComplexity)
|
||||
*/
|
||||
public function updateItems($data)
|
||||
{
|
||||
$infoDataObject = new \Magento\Framework\DataObject($data);
|
||||
$this->_eventManager->dispatch(
|
||||
'checkout_cart_update_items_before',
|
||||
['cart' => $this, 'info' => $infoDataObject]
|
||||
);
|
||||
|
||||
$qtyRecalculatedFlag = false;
|
||||
foreach ($data as $itemId => $itemInfo) {
|
||||
$item = $this->getQuote()->getItemById($itemId);
|
||||
if (!$item) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!empty($itemInfo['remove']) || isset($itemInfo['qty']) && $itemInfo['qty'] == '0') {
|
||||
$this->removeItem($itemId);
|
||||
continue;
|
||||
}
|
||||
|
||||
$qty = isset($itemInfo['qty']) ? (double)$itemInfo['qty'] : false;
|
||||
if ($qty > 0) {
|
||||
$item->clearMessage();
|
||||
$item->setHasError(false);
|
||||
$item->setQty($qty);
|
||||
|
||||
if ($item->getHasError()) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(__($item->getMessage()));
|
||||
}
|
||||
|
||||
if (isset($itemInfo['before_suggest_qty']) && $itemInfo['before_suggest_qty'] != $qty) {
|
||||
$qtyRecalculatedFlag = true;
|
||||
$this->messageManager->addNoticeMessage(
|
||||
__('Quantity was recalculated from %1 to %2', $itemInfo['before_suggest_qty'], $qty),
|
||||
'quote_item' . $item->getId()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($itemInfo['extension_attributes']) && is_array($itemInfo['extension_attributes'])) {
|
||||
foreach ($itemInfo['extension_attributes'] as $attribute => $val) {
|
||||
if ($val!='' || $val!=null) {
|
||||
$item->setData($attribute, $val);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($itemInfo['discount_percent'])) {
|
||||
$item->setDiscountAmount($item->getRowTotal()*$itemInfo['discount_percent']);
|
||||
$item->setDiscountPercent($itemInfo['discount_percent']);
|
||||
}
|
||||
|
||||
// $item->setRowTotal($item->getRowTotal());
|
||||
// $item->setDiscountPercent($itemInfo['discount_percent']);
|
||||
}
|
||||
|
||||
isset($data['salesperson_id']) && $this->getQuote()->setData('salesperson_id', $data['salesperson_id']);
|
||||
isset($data['customer_note']) && $this->getQuote()->setCustomerNote($data['customer_note']);
|
||||
|
||||
if ($qtyRecalculatedFlag) {
|
||||
$this->messageManager->addNoticeMessage(
|
||||
__('We adjusted product quantities to fit the required increments.')
|
||||
);
|
||||
}
|
||||
|
||||
$this->_eventManager->dispatch(
|
||||
'checkout_cart_update_items_after',
|
||||
['cart' => $this, 'info' => $infoDataObject]
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get request quantity
|
||||
*
|
||||
* @param Product $product
|
||||
* @param \Magento\Framework\DataObject|int|array $request
|
||||
* @return int|DataObject
|
||||
* @throws LocalizedException
|
||||
*/
|
||||
protected function getQtyRequest($product, $request = 0)
|
||||
{
|
||||
$request = $this->_getProductRequest($request);
|
||||
$stockItem = $this->stockRegistry->getStockItem($product->getId(), $product->getStore()->getWebsiteId());
|
||||
$minimumQty = $stockItem->getMinSaleQty();
|
||||
//If product quantity is not specified in request and there is set minimal qty for it
|
||||
if ($minimumQty
|
||||
&& $minimumQty > 0
|
||||
&& !$request->getQty()
|
||||
) {
|
||||
$request->setQty($minimumQty);
|
||||
}
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add product to shopping cart (quote)
|
||||
*
|
||||
* @param int|Product $productInfo
|
||||
* @param \Magento\Framework\DataObject|int|array $requestInfo
|
||||
* @return $this
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
*/
|
||||
public function addProduct($productInfo, $requestInfo = null)
|
||||
{
|
||||
$product = $this->_getProduct($productInfo);
|
||||
$productId = $product->getId();
|
||||
|
||||
if ($productId) {
|
||||
$request = $this->getQtyRequest($product, $requestInfo);
|
||||
try {
|
||||
$this->_eventManager->dispatch(
|
||||
'checkout_cart_product_add_before',
|
||||
['info' => $requestInfo, 'product' => $product]
|
||||
);
|
||||
$result = $this->getQuote()->addProduct($product, $request);
|
||||
} catch (\Magento\Framework\Exception\LocalizedException $e) {
|
||||
$this->_checkoutSession->setUseNotice(false);
|
||||
$result = $e->getMessage();
|
||||
}
|
||||
/**
|
||||
* String we can get if prepare process has error
|
||||
*/
|
||||
if (is_string($result)) {
|
||||
if ($product->hasOptionsValidationFail()) {
|
||||
$redirectUrl = $product->getUrlModel()->getUrl(
|
||||
$product,
|
||||
['_query' => ['startcustomization' => 1]]
|
||||
);
|
||||
} else {
|
||||
$redirectUrl = $product->getProductUrl();
|
||||
}
|
||||
$this->_checkoutSession->setRedirectUrl($redirectUrl);
|
||||
if ($this->_checkoutSession->getUseNotice() === null) {
|
||||
$this->_checkoutSession->setUseNotice(true);
|
||||
}
|
||||
throw new \Magento\Framework\Exception\LocalizedException(__($result));
|
||||
}
|
||||
} else {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(__('The product does not exist.'));
|
||||
}
|
||||
|
||||
$this->_eventManager->dispatch(
|
||||
'checkout_cart_product_add_after',
|
||||
['quote_item' => $result, 'product' => $product]
|
||||
);
|
||||
$this->_checkoutSession->setLastAddedProductId($productId);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function truncate()
|
||||
{
|
||||
$this->getQuote()->removeAllItems();
|
||||
$this->getQuote()->setCustomerNote(null)->setData('salesperson_id', 0);
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Checkout\Model\Cart;
|
||||
|
||||
use Magento\Framework\Api\DataObjectHelper;
|
||||
use Magento\Framework\Api\ExtensibleDataInterface;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Quote\Api;
|
||||
use Magento\Quote\Api\CartRepositoryInterface;
|
||||
use Magento\Quote\Api\CouponManagementInterface;
|
||||
use Magento\Quote\Model\Cart\Totals\ItemConverter;
|
||||
use Magento\Quote\Model\Cart\TotalsConverter;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Stemcell\Checkout\Api\Data\Quote\TotalsInterface;
|
||||
use Stemcell\Checkout\Api\Data\Quote\TotalsInterfaceFactory;
|
||||
use Stemcell\Checkout\Model\Cart\Totals\Item;
|
||||
|
||||
class CartTotalRepository extends \Magento\Quote\Model\Cart\CartTotalRepository
|
||||
{
|
||||
private TotalsInterfaceFactory $totalsInterfaceFactory;
|
||||
|
||||
/**
|
||||
* Cart totals factory.
|
||||
*
|
||||
* @var Api\Data\TotalsInterfaceFactory
|
||||
*/
|
||||
private $totalsFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Quote\Api\CartRepositoryInterface
|
||||
*/
|
||||
private $quoteRepository;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Api\DataObjectHelper
|
||||
*/
|
||||
private $dataObjectHelper;
|
||||
|
||||
/**
|
||||
* @var ItemConverter
|
||||
*/
|
||||
private $itemConverter;
|
||||
|
||||
/**
|
||||
* @var CouponManagementInterface
|
||||
*/
|
||||
protected $couponService;
|
||||
|
||||
/**
|
||||
* @var TotalsConverter
|
||||
*/
|
||||
protected $totalsConverter;
|
||||
|
||||
public function __construct(
|
||||
Api\Data\TotalsInterfaceFactory $totalsFactory,
|
||||
TotalsInterfaceFactory $totalsInterfaceFactory,
|
||||
CartRepositoryInterface $quoteRepository,
|
||||
DataObjectHelper $dataObjectHelper,
|
||||
CouponManagementInterface $couponService,
|
||||
TotalsConverter $totalsConverter,
|
||||
ItemConverter $converter
|
||||
) {
|
||||
parent::__construct($totalsFactory, $quoteRepository, $dataObjectHelper, $couponService, $totalsConverter, $converter);
|
||||
$this->totalsInterfaceFactory = $totalsInterfaceFactory;
|
||||
$this->totalsFactory = $totalsFactory;
|
||||
$this->quoteRepository = $quoteRepository;
|
||||
$this->dataObjectHelper = $dataObjectHelper;
|
||||
$this->couponService = $couponService;
|
||||
$this->totalsConverter = $totalsConverter;
|
||||
$this->itemConverter = $converter;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $cartId
|
||||
* @return TotalsInterface
|
||||
* @throws \Magento\Framework\Exception\NoSuchEntityException
|
||||
*/
|
||||
public function get($cartId): TotalsInterface
|
||||
{
|
||||
/** @var \Magento\Quote\Model\Quote $quote */
|
||||
$quote = $this->quoteRepository->getActive($cartId);
|
||||
if ($quote->isVirtual()) {
|
||||
$quote->collectTotals();
|
||||
$addressTotalsData = $quote->getBillingAddress()->getData();
|
||||
$addressTotals = $quote->getBillingAddress()->getTotals();
|
||||
} else {
|
||||
$addressTotalsData = $quote->getShippingAddress()->getData();
|
||||
$addressTotals = $quote->getShippingAddress()->getTotals();
|
||||
}
|
||||
unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
|
||||
|
||||
$quoteTotals = $this->totalsInterfaceFactory->create();
|
||||
$this->dataObjectHelper->populateWithArray(
|
||||
$quoteTotals,
|
||||
$addressTotalsData,
|
||||
TotalsInterface::class
|
||||
);
|
||||
|
||||
$items = array_map([$this->itemConverter, 'modelToDataObject'], $quote->getAllVisibleItems());
|
||||
// $logger = ObjectManager::getInstance()->get(LoggerInterface::class);
|
||||
|
||||
$calculatedTotals = $this->totalsConverter->process($addressTotals);
|
||||
$quoteTotals->setTotalSegments($calculatedTotals);
|
||||
$quoteTotals->setCouponCode($this->couponService->get($cartId));
|
||||
$quoteTotals->setItems($items);
|
||||
$quoteTotals->setDiscountAmount($quote->getData('customer_discount'));
|
||||
$quoteTotals->setItemsQty($quote->getItemsQty());
|
||||
$quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
|
||||
$quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
|
||||
return $quoteTotals;// TODO: Change the autogenerated stub
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Checkout\Model\Cart;
|
||||
|
||||
use Stemcell\Checkout\Api\Data\Quote\TotalsInterface;
|
||||
use Stemcell\Checkout\Api\Data\Quote\TotalsItemInterface;
|
||||
|
||||
class Totals extends \Magento\Quote\Model\Cart\Totals implements TotalsInterface
|
||||
{
|
||||
/**
|
||||
* Get totals by items
|
||||
*
|
||||
* @return TotalsItemInterface[]|null
|
||||
*/
|
||||
public function getItems()
|
||||
{
|
||||
return $this->getData(self::KEY_ITEMS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get totals by items
|
||||
*
|
||||
* @param TotalsItemInterface[] $items
|
||||
* @return $this
|
||||
*/
|
||||
public function setItems(array $items = null)
|
||||
{
|
||||
return $this->setData(self::KEY_ITEMS, $items);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Checkout\Model\Cart\Totals;
|
||||
|
||||
use Magento\Framework\Api\AttributeValueFactory;
|
||||
use Stemcell\Checkout\Api\Data\Quote\TotalsItemInterface;
|
||||
use \Magento\Quote\Model\Quote\ItemFactory as QuoteItemFactory;
|
||||
use \Magento\Quote\Model\Quote\Item as QuoteItem;
|
||||
use Stemcell\Customer\Api\Data\SalespersonInterface;
|
||||
use Stemcell\Customer\Model\LabRepository;
|
||||
|
||||
class Item extends \Magento\Quote\Model\Cart\Totals\Item implements TotalsItemInterface
|
||||
{
|
||||
protected $item = null;
|
||||
private SalespersonInterface $salespersonInterface;
|
||||
private LabRepository $labRepository;
|
||||
private QuoteItemFactory $itemFactory;
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
|
||||
\Magento\Framework\Api\AttributeValueFactory $attributeValueFactory,
|
||||
QuoteItemFactory $itemFactory,
|
||||
SalespersonInterface $salespersonInterface,
|
||||
LabRepository $labRepository,
|
||||
$data = []
|
||||
) {
|
||||
$this->itemFactory = $itemFactory;
|
||||
$this->salespersonInterface = $salespersonInterface;
|
||||
$this->labRepository = $labRepository;
|
||||
parent::__construct($extensionFactory, $attributeValueFactory, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return QuoteItem
|
||||
*/
|
||||
protected function getQuoteItem()
|
||||
{
|
||||
if (!$this->item) {
|
||||
$this->item = $this->itemFactory->create()->load($this->getItemId());
|
||||
}
|
||||
return $this->item;
|
||||
}
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getSku()
|
||||
{
|
||||
return $this->_get(self::SKU);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setSku(string $sku)
|
||||
{
|
||||
return $this->setData(self::SKU, $this->getQuoteItem()->getSku());
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getSalespersonName()
|
||||
{
|
||||
return $this->_get(self::SALESPERSON_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setSalespersonName(string | null $salespersonName)
|
||||
{
|
||||
|
||||
return $this->setData(self::SALESPERSON_NAME, $salespersonName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getLabBookingAccount()
|
||||
{
|
||||
return $this->_get(self::LAB_BOOKING_ACCOUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setLabBookingAccount(string $labBookingAccount)
|
||||
{
|
||||
return $this->setData(self::LAB_BOOKING_ACCOUNT, $labBookingAccount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getCdCode()
|
||||
{
|
||||
return $this->_get(self::CD_CODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setCdCode(string $cdCode)
|
||||
{
|
||||
return $this->setData(self::CD_CODE, $this->getQuoteItem()->getCd());
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getQuoteCode()
|
||||
{
|
||||
|
||||
return $this->_get(self::QUOTE_CODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setQuoteCode(string $quoteCode)
|
||||
{
|
||||
return $this->setData(self::QUOTE_CODE, $this->getQuoteItem()->getData('quote'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getPiSapId()
|
||||
{
|
||||
return $this->_get(self::PI_SAP_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setPiSapId(string $piSapId)
|
||||
{
|
||||
return $this->setData(self::PI_SAP_ID, $this->getQuoteItem()->getData(self::PI_SAP_ID));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getCustomerDiscountPercent()
|
||||
{
|
||||
return $this->_get(self::CUSTOMER_DISCOUNT_PERCENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setCustomerDiscountPercent(string $customerDiscountPercent)
|
||||
{
|
||||
return $this->setData(self::CUSTOMER_DISCOUNT_PERCENT, $customerDiscountPercent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getNote()
|
||||
{
|
||||
return $this->_get(self::NOTE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setNote(string $note)
|
||||
{
|
||||
return $this->setData(self::NOTE, $note);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Checkout\Model;
|
||||
|
||||
use Magento\Captcha\Api\CaptchaConfigPostProcessorInterface;
|
||||
use Magento\Catalog\Helper\Product\ConfigurationPool;
|
||||
use Magento\Checkout\Helper\Data as CheckoutHelper;
|
||||
use Magento\Checkout\Model\Cart;
|
||||
use Magento\Checkout\Model\Session as CheckoutSession;
|
||||
use Magento\Customer\Api\AddressMetadataInterface;
|
||||
use Magento\Customer\Api\CustomerRepositoryInterface as CustomerRepository;
|
||||
use Magento\Customer\Model\Address\CustomerAddressDataProvider;
|
||||
use Magento\Customer\Model\Session as CustomerSession;
|
||||
use Magento\Customer\Model\Url as CustomerUrlManager;
|
||||
use Magento\Eav\Api\AttributeOptionManagementInterface;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\Http\Context as HttpContext;
|
||||
use Magento\Framework\Data\Form\FormKey;
|
||||
use Magento\Framework\Escaper;
|
||||
use Magento\Framework\Locale\FormatInterface as LocaleFormat;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Quote\Api\CartItemRepositoryInterface as QuoteItemRepository;
|
||||
use Magento\Quote\Api\CartTotalRepositoryInterface;
|
||||
use Magento\Quote\Api\ShippingMethodManagementInterface as ShippingMethodManager;
|
||||
use Magento\Quote\Model\Cart\Totals\Item;
|
||||
use Magento\Quote\Model\Quote\ItemFactory;
|
||||
use Magento\Quote\Model\QuoteIdMaskFactory;
|
||||
use Stemcell\Customer\Api\SalespersonRepositoryInterface;
|
||||
use Stemcell\Customer\Model\LabRepository;
|
||||
|
||||
class DefaultConfigProvider extends \Magento\Checkout\Model\DefaultConfigProvider
|
||||
{
|
||||
/**
|
||||
* @var CheckoutSession
|
||||
*/
|
||||
protected $checkoutSession;
|
||||
private ItemFactory $quoteItem;
|
||||
private SalespersonRepositoryInterface $salespersonRepository;
|
||||
private LabRepository $labRepository;
|
||||
|
||||
public function __construct(
|
||||
CheckoutHelper $checkoutHelper,
|
||||
CheckoutSession $checkoutSession,
|
||||
CustomerRepository $customerRepository,
|
||||
CustomerSession $customerSession,
|
||||
CustomerUrlManager $customerUrlManager,
|
||||
HttpContext $httpContext,
|
||||
\Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
|
||||
QuoteItemRepository $quoteItemRepository,
|
||||
ShippingMethodManager $shippingMethodManager,
|
||||
ConfigurationPool $configurationPool,
|
||||
QuoteIdMaskFactory $quoteIdMaskFactory,
|
||||
LocaleFormat $localeFormat,
|
||||
\Magento\Customer\Model\Address\Mapper $addressMapper,
|
||||
\Magento\Customer\Model\Address\Config $addressConfig,
|
||||
FormKey $formKey,
|
||||
\Magento\Catalog\Helper\Image $imageHelper,
|
||||
\Magento\Framework\View\ConfigInterface $viewConfig,
|
||||
\Magento\Directory\Model\Country\Postcode\ConfigInterface $postCodesConfig,
|
||||
Cart\ImageProvider $imageProvider,
|
||||
\Magento\Directory\Helper\Data $directoryHelper,
|
||||
CartTotalRepositoryInterface $cartTotalRepository,
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
\Magento\Shipping\Model\Config $shippingMethodConfig,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
||||
\Magento\Quote\Api\PaymentMethodManagementInterface $paymentMethodManagement,
|
||||
UrlInterface $urlBuilder,
|
||||
CaptchaConfigPostProcessorInterface $configPostProcessor,
|
||||
ItemFactory $quoteItem,
|
||||
SalespersonRepositoryInterface $salespersonRepository,
|
||||
LabRepository $labRepository,
|
||||
AddressMetadataInterface $addressMetadata = null,
|
||||
AttributeOptionManagementInterface $attributeOptionManager = null,
|
||||
CustomerAddressDataProvider $customerAddressData = null,
|
||||
Escaper $escaper = null
|
||||
)
|
||||
{
|
||||
parent::__construct($checkoutHelper, $checkoutSession, $customerRepository, $customerSession, $customerUrlManager, $httpContext, $quoteRepository, $quoteItemRepository, $shippingMethodManager, $configurationPool, $quoteIdMaskFactory, $localeFormat, $addressMapper, $addressConfig, $formKey, $imageHelper, $viewConfig, $postCodesConfig, $imageProvider, $directoryHelper, $cartTotalRepository, $scopeConfig, $shippingMethodConfig, $storeManager, $paymentMethodManagement, $urlBuilder, $configPostProcessor, $addressMetadata, $attributeOptionManager, $customerAddressData, $escaper);
|
||||
$this->checkoutSession = $checkoutSession;
|
||||
$this->quoteItem = $quoteItem;
|
||||
$this->salespersonRepository = $salespersonRepository;
|
||||
$this->labRepository = $labRepository;
|
||||
}
|
||||
|
||||
protected function addAdditionalItemsData(Item $item)
|
||||
{
|
||||
$data = [];
|
||||
$quoteItem = $this->quoteItem->create()->load($item->getItemId());
|
||||
$data['sku'] = $quoteItem->getSku();
|
||||
if ($quoteItem->getSalesPersonId()) {
|
||||
$salesperson = $this->salespersonRepository->get($quoteItem->getSalesPersonId());
|
||||
$data['salesperson_name'] = $salesperson->getName();
|
||||
}
|
||||
if ($quoteItem->getLabCode()) {
|
||||
$lab = $this->labRepository->getByLabCode($quoteItem->getLabCode());
|
||||
$data['booking_account'] = $lab->getBookingAccount();
|
||||
}
|
||||
$data['cd_code'] = $quoteItem->getCd();
|
||||
$data['quote_code'] = $quoteItem->getData('quote');
|
||||
$data['pi_sap_id'] = $quoteItem->getData('pi_sap_id');
|
||||
return $data;
|
||||
}
|
||||
protected function getTotalsData()
|
||||
{
|
||||
/** @var \Magento\Quote\Api\Data\TotalsInterface $totals */
|
||||
$totals = $this->cartTotalRepository->get($this->checkoutSession->getQuote()->getId());
|
||||
$items = [];
|
||||
/** @var Item $item */
|
||||
foreach ($totals->getItems() as $item) {
|
||||
$items[] = array_merge($item->__toArray(), $this->addAdditionalItemsData($item));
|
||||
}
|
||||
$totalSegmentsData = [];
|
||||
/** @var \Magento\Quote\Model\Cart\TotalSegment $totalSegment */
|
||||
foreach ($totals->getTotalSegments() as $totalSegment) {
|
||||
$totalSegmentArray = $totalSegment->toArray();
|
||||
if (is_object($totalSegment->getExtensionAttributes())) {
|
||||
$totalSegmentArray['extension_attributes'] = $totalSegment->getExtensionAttributes()->__toArray();
|
||||
}
|
||||
$totalSegmentsData[] = $totalSegmentArray;
|
||||
}
|
||||
$totals->setItems($items);
|
||||
$totals->setTotalSegments($totalSegmentsData);
|
||||
$totalsArray = $totals->toArray();
|
||||
if (is_object($totals->getExtensionAttributes())) {
|
||||
$totalsArray['extension_attributes'] = $totals->getExtensionAttributes()->__toArray();
|
||||
}
|
||||
return $totalsArray;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user