Advertisement
Guest User

Untitled

a guest
Jun 13th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. public function selectRate(ShipmentInterface $shipment, ShippingRate $rate) {
  2. // Plugins can override this method to store additional information
  3. // on the shipment when the rate is selected (for example, the rate ID).
  4. $shipment->setShippingService($rate->getService()->getId());
  5. $shipment->setAmount($rate->getAmount());
  6. }
  7.  
  8. protected function resolveMatrix(array $matrix, $price) {
  9. $price_currency_code = $price->getCurrencyCode();
  10. $price_number = $price->getNumber();
  11.  
  12. // The price matrix must be in the same currency as the order.
  13. // We currently disable the check until we have added the currency code in
  14. // the configuration form.
  15. // if ($matrix['currency_code'] !== $price_currency_code) {
  16. // throw new Exception('The shipping price matrix must be at the same currency as the order total for calculating the shipping costs.');
  17. // }
  18.  
  19. // We detect which matrix entry the price falls under. It should be larger
  20. // or equal than the entry's threshold and smaller than the next entry's
  21. // threshold. Only larger or equal then the entry's threshold in the case of
  22. // the last entry.
  23. foreach ($matrix['values'] as $key => $value) {
  24. $bigger_than_current = Calculator::compare($price_number, $value['threshold']) !== -1;
  25.  
  26. if (isset($matrix['values'][$key+1])) {
  27. $smaller_than_next = Calculator::compare($price_number, $matrix['values'][$key+1]['threshold']) === -1;
  28. }
  29. else {
  30. $smaller_than_next = TRUE;
  31. }
  32.  
  33. // Doesn't match the current entry, move on to the next one.
  34. if (!($bigger_than_current && $smaller_than_next)) {
  35. continue;
  36. }
  37.  
  38. // If the type of the matched entry is 'fixed_amount', the cost is fixed
  39. // and it equals the entry's value.
  40. if ($value['type'] === 'fixed_amount') {
  41. return $value['value'];
  42. }
  43.  
  44. // Throw an exception if the type is neither 'fixed_amount' nor
  45. // 'percentage'.
  46. if ($value['type'] !== 'percentage') {
  47. throw new Exception(
  48. sprintf('Unsupported price matrix item "%s", 'fixed_amount' or 'percentage' expected.'),
  49. $value['type']
  50. );
  51. }
  52.  
  53. // If the type of the matched entry is 'percentage', the cost is the given
  54. // price multiplied by the percentage factor i.e. the entry's value.
  55. $cost = Calculator::multiply($price_number, $value['value']);
  56.  
  57. // Check minimum and maximum constraints.
  58. if (!empty($value['min']) && Calculator::compare($cost, $value['min']) === -1) {
  59. $cost = $value['min'];
  60. }
  61. elseif (!empty($value['max']) && Calculator::compare($cost, $value['max']) === 1){
  62. $cost = $value['max'];
  63. }
  64.  
  65. return $cost;
  66. }
  67. }
  68.  
  69. class ShippingRate {
  70.  
  71. /**
  72. * The ID.
  73. *
  74. * @var string
  75. */
  76. protected $id;
  77.  
  78. /**
  79. * The shipping service.
  80. *
  81. * @var Drupalcommerce_shippingShippingService
  82. */
  83. protected $service;
  84.  
  85. /**
  86. * The amount.
  87. *
  88. * @var Drupalcommerce_pricePrice
  89. */
  90. protected $amount;
  91.  
  92. /**
  93. * The delivery date.
  94. *
  95. * @var DrupalCoreDatetimeDrupalDateTime
  96. */
  97. protected $deliveryDate;
  98.  
  99. /**
  100. * The delivery terms.
  101. *
  102. * @var string
  103. */
  104. protected $deliveryTerms;
  105.  
  106. /**
  107. * Constructs a new ShippingRate instance.
  108. *
  109. * @param string $id
  110. * The ID.
  111. * @param Drupalcommerce_shippingShippingService $service
  112. * The shipping service.
  113. * @param Drupalcommerce_pricePrice $amount
  114. * The amount.
  115. * @param DrupalCoreDatetimeDrupalDateTime $delivery_date
  116. * The delivery date.
  117. * @param string $delivery_terms
  118. * The delivery terms.
  119. */
  120. public function __construct($id, ShippingService $service, Price $amount, DrupalDateTime $delivery_date = NULL, $delivery_terms = NULL) {
  121. $this->id = $id;
  122. $this->service = $service;
  123. $this->amount = $amount;
  124. $this->deliveryDate = $delivery_date;
  125. $this->deliveryTerms = $delivery_terms;
  126. }
  127.  
  128. /**
  129. * Gets the ID.
  130. *
  131. * @return string
  132. * The ID.
  133. */
  134. public function getId() {
  135. return $this->id;
  136. }
  137.  
  138. /**
  139. * Gets the shipping service.
  140. *
  141. * The shipping service label is meant to be displayed when presenting rates
  142. * for selection.
  143. *
  144. * @return Drupalcommerce_shippingShippingService
  145. * The shipping service.
  146. */
  147. public function getService() {
  148. return $this->service;
  149. }
  150.  
  151. /**
  152. * Gets the amount.
  153. *
  154. * @return Drupalcommerce_pricePrice
  155. * The amount.
  156. */
  157. public function getAmount() {
  158. return $this->amount;
  159. }
  160.  
  161. /**
  162. * Gets the delivery date, if known.
  163. *
  164. * @return DrupalCoreDatetimeDrupalDateTime|null
  165. * The delivery date, or NULL.
  166. */
  167. public function getDeliveryDate() {
  168. return $this->deliveryDate;
  169. }
  170.  
  171. /**
  172. * Gets the delivery terms, if known.
  173. *
  174. * Example: "Delivery in 1 to 3 business days."
  175. * Can be displayed to the end-user, if no translation is required.
  176. *
  177. * @return string|null
  178. * The delivery terms, or NULL.
  179. */
  180. public function getDeliveryTerms() {
  181. return $this->deliveryTerms;
  182. }
  183.  
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement