ShippingMethodConverter.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Cart;
  7. /**
  8. * Quote shipping method data.
  9. *
  10. */
  11. class ShippingMethodConverter
  12. {
  13. /**
  14. * Shipping method data factory.
  15. *
  16. * @var \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory
  17. */
  18. protected $shippingMethodDataFactory;
  19. /**
  20. * @var \Magento\Tax\Helper\Data
  21. */
  22. protected $taxHelper;
  23. /**
  24. * Constructs a shipping method converter object.
  25. *
  26. * @param \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory $shippingMethodDataFactory Shipping method factory.
  27. * @param \Magento\Store\Model\StoreManagerInterface $storeManager Store manager interface.
  28. * @param \Magento\Tax\Helper\Data $taxHelper Tax data helper.
  29. */
  30. public function __construct(
  31. \Magento\Quote\Api\Data\ShippingMethodInterfaceFactory $shippingMethodDataFactory,
  32. \Magento\Store\Model\StoreManagerInterface $storeManager,
  33. \Magento\Tax\Helper\Data $taxHelper
  34. ) {
  35. $this->shippingMethodDataFactory = $shippingMethodDataFactory;
  36. $this->storeManager = $storeManager;
  37. $this->taxHelper = $taxHelper;
  38. }
  39. /**
  40. * Converts a specified rate model to a shipping method data object.
  41. *
  42. * @param string $quoteCurrencyCode The quote currency code.
  43. * @param \Magento\Quote\Model\Quote\Address\Rate $rateModel The rate model.
  44. * @return \Magento\Quote\Api\Data\ShippingMethodInterface Shipping method data object.
  45. */
  46. public function modelToDataObject($rateModel, $quoteCurrencyCode)
  47. {
  48. /** @var \Magento\Directory\Model\Currency $currency */
  49. $currency = $this->storeManager->getStore()->getBaseCurrency();
  50. $errorMessage = $rateModel->getErrorMessage();
  51. return $this->shippingMethodDataFactory->create()
  52. ->setCarrierCode($rateModel->getCarrier())
  53. ->setMethodCode($rateModel->getMethod())
  54. ->setCarrierTitle($rateModel->getCarrierTitle())
  55. ->setMethodTitle($rateModel->getMethodTitle())
  56. ->setAmount($currency->convert($rateModel->getPrice(), $quoteCurrencyCode))
  57. ->setBaseAmount($rateModel->getPrice())
  58. ->setAvailable(empty($errorMessage))
  59. ->setErrorMessage(empty($errorMessage) ? false : $errorMessage)
  60. ->setPriceExclTax(
  61. $currency->convert($this->getShippingPriceWithFlag($rateModel, false), $quoteCurrencyCode)
  62. )
  63. ->setPriceInclTax(
  64. $currency->convert($this->getShippingPriceWithFlag($rateModel, true), $quoteCurrencyCode)
  65. );
  66. }
  67. /**
  68. * Get Shipping Price including or excluding tax
  69. *
  70. * @param \Magento\Quote\Model\Quote\Address\Rate $rateModel
  71. * @param bool $flag
  72. * @return float
  73. */
  74. private function getShippingPriceWithFlag($rateModel, $flag)
  75. {
  76. return $this->taxHelper->getShippingPrice(
  77. $rateModel->getPrice(),
  78. $flag,
  79. $rateModel->getAddress(),
  80. $rateModel->getAddress()->getQuote()->getCustomerTaxClassId()
  81. );
  82. }
  83. }