![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/Checkout/Plugin/ |
<?php /** * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @author Ćukasz Wojciechowski <[email protected]> * @copyright Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) */ declare(strict_types=1); namespace Cnc\Checkout\Plugin; use Cnc\Checkout\Model\Config; use Magento\Checkout\Api\Data\TotalsInformationInterface; use Magento\Checkout\Model\Session as CheckoutSession; use Ecombricks\InventoryCheckout\Plugin\Model\TotalsInformationManagement as ParentTotalsInformationManagement; class TotalsInformationManagement { /** * @var CheckoutSession */ private $checkoutSession; /** * @param CheckoutSession $checkoutSession */ public function __construct( CheckoutSession $checkoutSession ) { $this->checkoutSession = $checkoutSession; } /** * @param ParentTotalsInformationManagement $subject * @param $result * @param int $cartId * @param TotalsInformationInterface $addressInformation */ public function afterCalculate( ParentTotalsInformationManagement $subject, $result, $cartId, TotalsInformationInterface $addressInformation ) { $carrierCode = $addressInformation->getShippingCarrierCode(); $methodCode = $addressInformation->getShippingMethodCode(); $this->checkoutSession->unsFrPreselectedMethod(); $this->checkoutSession->unsUsaPreselectedMethod(); if (is_string($carrierCode) && is_string($methodCode)) { $frCarrierCode = $this->parseCode($carrierCode, Config::SHIPPING_SOURCE_FR_CODE); $frMethodCode = $this->parseCode($methodCode, Config::SHIPPING_SOURCE_FR_CODE); $usaCarrierCode = $this->parseCode($carrierCode, Config::SHIPPING_SOURCE_USA_CODE); $usaMethodCode = $this->parseCode($methodCode, Config::SHIPPING_SOURCE_USA_CODE); $this->checkoutSession->setFrPreselectedMethod(sprintf('%s_%s', $frCarrierCode, $frMethodCode)); $this->checkoutSession->setUsaPreselectedMethod(sprintf('%s_%s', $usaCarrierCode, $usaMethodCode)); } return $result; } /** * @param string $code * @param string $sourceCode * @return string */ private function parseCode(string $code, string $sourceCode): string { $parsedCode = ''; $shippingMethods = explode('|', $code); foreach ($shippingMethods as $shippingMethod) { if (strpos($shippingMethod, $sourceCode) !== false) { $parsedCode = str_replace($sourceCode . ':', '', $shippingMethod); } } return $parsedCode; } }