Rate.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote\Address;
  7. use Magento\Framework\Model\AbstractModel;
  8. /**
  9. * @api
  10. * @method int getAddressId()
  11. * @method \Magento\Quote\Model\Quote\Address\Rate setAddressId(int $value)
  12. * @method string getCreatedAt()
  13. * @method \Magento\Quote\Model\Quote\Address\Rate setCreatedAt(string $value)
  14. * @method string getUpdatedAt()
  15. * @method \Magento\Quote\Model\Quote\Address\Rate setUpdatedAt(string $value)
  16. * @method string getCarrier()
  17. * @method \Magento\Quote\Model\Quote\Address\Rate setCarrier(string $value)
  18. * @method string getCarrierTitle()
  19. * @method \Magento\Quote\Model\Quote\Address\Rate setCarrierTitle(string $value)
  20. * @method string getCode()
  21. * @method \Magento\Quote\Model\Quote\Address\Rate setCode(string $value)
  22. * @method string getMethod()
  23. * @method \Magento\Quote\Model\Quote\Address\Rate setMethod(string $value)
  24. * @method string getMethodDescription()
  25. * @method \Magento\Quote\Model\Quote\Address\Rate setMethodDescription(string $value)
  26. * @method float getPrice()
  27. * @method \Magento\Quote\Model\Quote\Address\Rate setPrice(float $value)
  28. * @method string getErrorMessage()
  29. * @method \Magento\Quote\Model\Quote\Address\Rate setErrorMessage(string $value)
  30. * @method string getMethodTitle()
  31. * @method \Magento\Quote\Model\Quote\Address\Rate setMethodTitle(string $value)
  32. *
  33. * @author Magento Core Team <core@magentocommerce.com>
  34. * @since 100.0.2
  35. */
  36. class Rate extends AbstractModel
  37. {
  38. /**
  39. * @var \Magento\Quote\Model\Quote\Address
  40. */
  41. protected $_address;
  42. /**
  43. * @return void
  44. */
  45. protected function _construct()
  46. {
  47. $this->_init(\Magento\Quote\Model\ResourceModel\Quote\Address\Rate::class);
  48. }
  49. /**
  50. * @return $this
  51. */
  52. public function beforeSave()
  53. {
  54. parent::beforeSave();
  55. if ($this->getAddress()) {
  56. $this->setAddressId($this->getAddress()->getId());
  57. }
  58. return $this;
  59. }
  60. /**
  61. * @param \Magento\Quote\Model\Quote\Address $address
  62. * @return $this
  63. */
  64. public function setAddress(\Magento\Quote\Model\Quote\Address $address)
  65. {
  66. $this->_address = $address;
  67. return $this;
  68. }
  69. /**
  70. * @return \Magento\Quote\Model\Quote\Address
  71. */
  72. public function getAddress()
  73. {
  74. return $this->_address;
  75. }
  76. /**
  77. * @param \Magento\Quote\Model\Quote\Address\RateResult\AbstractResult $rate
  78. * @return $this
  79. */
  80. public function importShippingRate(\Magento\Quote\Model\Quote\Address\RateResult\AbstractResult $rate)
  81. {
  82. if ($rate instanceof \Magento\Quote\Model\Quote\Address\RateResult\Error) {
  83. $this->setCode(
  84. $rate->getCarrier() . '_error'
  85. )->setCarrier(
  86. $rate->getCarrier()
  87. )->setCarrierTitle(
  88. $rate->getCarrierTitle()
  89. )->setErrorMessage(
  90. $rate->getErrorMessage()
  91. );
  92. } elseif ($rate instanceof \Magento\Quote\Model\Quote\Address\RateResult\Method) {
  93. $this->setCode(
  94. $rate->getCarrier() . '_' . $rate->getMethod()
  95. )->setCarrier(
  96. $rate->getCarrier()
  97. )->setCarrierTitle(
  98. $rate->getCarrierTitle()
  99. )->setMethod(
  100. $rate->getMethod()
  101. )->setMethodTitle(
  102. $rate->getMethodTitle()
  103. )->setMethodDescription(
  104. $rate->getMethodDescription()
  105. )->setPrice(
  106. $rate->getPrice()
  107. );
  108. }
  109. return $this;
  110. }
  111. }