/** * Retrieve payment method and assign additional template values * * @return $this * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function _beforeToHtml() { $methodInstance = $this->_quote->getPayment()->getMethodInstance(); $this->setPaymentMethodTitle($methodInstance->getTitle()); $this->setShippingRateRequired(true); if ($this->_quote->getIsVirtual()) { $this->setShippingRateRequired(false); } else { // prepare shipping rates $this->_address = $this->_quote->getShippingAddress(); $groups = $this->_address->getGroupedAllShippingRates(); if ($groups && $this->_address) { $this->setShippingRateGroups($groups); // determine current selected code & name foreach ($groups as $code => $rates) { foreach ($rates as $rate) { if ($this->_address->getShippingMethod() == $rate->getCode()) { $this->_currentShippingRate = $rate; break 2; } } } } $canEditShippingAddress = $this->_quote->getMayEditShippingAddress() && $this->_quote->getPayment()->getAdditionalInformation(\Magento\Paypal\Model\Express\Checkout::PAYMENT_INFO_BUTTON) == 1; // misc shipping parameters $this->setShippingMethodSubmitUrl($this->getUrl("{$this->_controllerPath}/saveShippingMethod"))->setCanEditShippingAddress($canEditShippingAddress)->setCanEditShippingMethod($this->_quote->getMayEditShippingMethod()); } $this->setEditUrl($this->getUrl("{$this->_controllerPath}/edit"))->setPlaceOrderUrl($this->getUrl("{$this->_controllerPath}/placeOrder")); return parent::_beforeToHtml(); }
/** * @param Address $address * @return bool * @SuppressWarnings(PHPMD.BooleanGetMethodName) */ public function getShippingAddressRate($address) { $rate = $address->getShippingRateByCode($address->getShippingMethod()); if ($rate) { return $rate; } return false; }
/** * Attempt to collect address shipping rates and return them for further usage in instant update API * Returns empty array if it was impossible to obtain any shipping rate * If there are shipping rates obtained, the method must return one of them as default. * * @param Address $address * @param bool $mayReturnEmpty * @param bool $calculateTax * @return array|false * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ protected function _prepareShippingOptions(Address $address, $mayReturnEmpty = false, $calculateTax = false) { $options = []; $i = 0; $iMin = false; $min = false; $userSelectedOption = null; foreach ($address->getGroupedAllShippingRates() as $group) { foreach ($group as $rate) { $amount = (double) $rate->getPrice(); if ($rate->getErrorMessage()) { continue; } $isDefault = $address->getShippingMethod() === $rate->getCode(); $amountExclTax = $this->_taxData->getShippingPrice($amount, false, $address); $amountInclTax = $this->_taxData->getShippingPrice($amount, true, $address); $options[$i] = new \Magento\Framework\DataObject(['is_default' => $isDefault, 'name' => trim("{$rate->getCarrierTitle()} - {$rate->getMethodTitle()}", ' -'), 'code' => $rate->getCode(), 'amount' => $amountExclTax]); if ($calculateTax) { $options[$i]->setTaxAmount($amountInclTax - $amountExclTax + $address->getTaxAmount() - $address->getShippingTaxAmount()); } if ($isDefault) { $userSelectedOption = $options[$i]; } if (false === $min || $amountInclTax < $min) { $min = $amountInclTax; $iMin = $i; } $i++; } } if ($mayReturnEmpty && $userSelectedOption === null) { $options[] = new \Magento\Framework\DataObject(['is_default' => true, 'name' => __('N/A'), 'code' => 'no_rate', 'amount' => 0.0]); if ($calculateTax) { $options[$i]->setTaxAmount($address->getTaxAmount()); } } elseif ($userSelectedOption === null && isset($options[$iMin])) { $options[$iMin]->setIsDefault(true); } // Magento will transfer only first 10 cheapest shipping options if there are more than 10 available. if (count($options) > 10) { usort($options, [get_class($this), 'cmpShippingOptions']); array_splice($options, 10); // User selected option will be always included in options list if ($userSelectedOption !== null && !in_array($userSelectedOption, $options)) { $options[9] = $userSelectedOption; } } return $options; }
/** * @param \Magento\Quote\Model\Quote $quote * @param Address $address * @return Address\Total */ public function collectAddressTotals(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address $address) { /** @var \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment */ $shippingAssignment = $this->shippingAssignmentFactory->create(); /** @var \Magento\Quote\Api\Data\ShippingInterface $shipping */ $shipping = $this->shippingFactory->create(); $shipping->setMethod($address->getShippingMethod()); $shipping->setAddress($address); $shippingAssignment->setShipping($shipping); $shippingAssignment->setItems($address->getAllItems()); /** @var \Magento\Quote\Model\Quote\Address\Total $total */ $total = $this->totalFactory->create('Magento\\Quote\\Model\\Quote\\Address\\Total'); $this->eventManager->dispatch('sales_quote_address_collect_totals_before', ['quote' => $quote, 'shipping_assignment' => $shippingAssignment, 'total' => $total]); foreach ($this->collectorList->getCollectors($quote->getStoreId()) as $collector) { /** @var CollectorInterface $collector */ $collector->collect($quote, $shippingAssignment, $total); } $this->eventManager->dispatch('sales_quote_address_collect_totals_after', ['quote' => $quote, 'shipping_assignment' => $shippingAssignment, 'total' => $total]); $address->addData($total->getData()); $address->setAppliedTaxes($total->getAppliedTaxes()); return $total; }
/** * @param Address $address * @return mixed */ public function getAddressShippingMethod($address) { return $address->getShippingMethod(); }