Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function selectRate(ShipmentInterface $shipment, ShippingRate $rate) {
- // Plugins can override this method to store additional information
- // on the shipment when the rate is selected (for example, the rate ID).
- $shipment->setShippingService($rate->getService()->getId());
- $shipment->setAmount($rate->getAmount());
- }
- protected function resolveMatrix(array $matrix, $price) {
- $price_currency_code = $price->getCurrencyCode();
- $price_number = $price->getNumber();
- // The price matrix must be in the same currency as the order.
- // We currently disable the check until we have added the currency code in
- // the configuration form.
- // if ($matrix['currency_code'] !== $price_currency_code) {
- // throw new Exception('The shipping price matrix must be at the same currency as the order total for calculating the shipping costs.');
- // }
- // We detect which matrix entry the price falls under. It should be larger
- // or equal than the entry's threshold and smaller than the next entry's
- // threshold. Only larger or equal then the entry's threshold in the case of
- // the last entry.
- foreach ($matrix['values'] as $key => $value) {
- $bigger_than_current = Calculator::compare($price_number, $value['threshold']) !== -1;
- if (isset($matrix['values'][$key+1])) {
- $smaller_than_next = Calculator::compare($price_number, $matrix['values'][$key+1]['threshold']) === -1;
- }
- else {
- $smaller_than_next = TRUE;
- }
- // Doesn't match the current entry, move on to the next one.
- if (!($bigger_than_current && $smaller_than_next)) {
- continue;
- }
- // If the type of the matched entry is 'fixed_amount', the cost is fixed
- // and it equals the entry's value.
- if ($value['type'] === 'fixed_amount') {
- return $value['value'];
- }
- // Throw an exception if the type is neither 'fixed_amount' nor
- // 'percentage'.
- if ($value['type'] !== 'percentage') {
- throw new Exception(
- sprintf('Unsupported price matrix item "%s", 'fixed_amount' or 'percentage' expected.'),
- $value['type']
- );
- }
- // If the type of the matched entry is 'percentage', the cost is the given
- // price multiplied by the percentage factor i.e. the entry's value.
- $cost = Calculator::multiply($price_number, $value['value']);
- // Check minimum and maximum constraints.
- if (!empty($value['min']) && Calculator::compare($cost, $value['min']) === -1) {
- $cost = $value['min'];
- }
- elseif (!empty($value['max']) && Calculator::compare($cost, $value['max']) === 1){
- $cost = $value['max'];
- }
- return $cost;
- }
- }
- class ShippingRate {
- /**
- * The ID.
- *
- * @var string
- */
- protected $id;
- /**
- * The shipping service.
- *
- * @var Drupalcommerce_shippingShippingService
- */
- protected $service;
- /**
- * The amount.
- *
- * @var Drupalcommerce_pricePrice
- */
- protected $amount;
- /**
- * The delivery date.
- *
- * @var DrupalCoreDatetimeDrupalDateTime
- */
- protected $deliveryDate;
- /**
- * The delivery terms.
- *
- * @var string
- */
- protected $deliveryTerms;
- /**
- * Constructs a new ShippingRate instance.
- *
- * @param string $id
- * The ID.
- * @param Drupalcommerce_shippingShippingService $service
- * The shipping service.
- * @param Drupalcommerce_pricePrice $amount
- * The amount.
- * @param DrupalCoreDatetimeDrupalDateTime $delivery_date
- * The delivery date.
- * @param string $delivery_terms
- * The delivery terms.
- */
- public function __construct($id, ShippingService $service, Price $amount, DrupalDateTime $delivery_date = NULL, $delivery_terms = NULL) {
- $this->id = $id;
- $this->service = $service;
- $this->amount = $amount;
- $this->deliveryDate = $delivery_date;
- $this->deliveryTerms = $delivery_terms;
- }
- /**
- * Gets the ID.
- *
- * @return string
- * The ID.
- */
- public function getId() {
- return $this->id;
- }
- /**
- * Gets the shipping service.
- *
- * The shipping service label is meant to be displayed when presenting rates
- * for selection.
- *
- * @return Drupalcommerce_shippingShippingService
- * The shipping service.
- */
- public function getService() {
- return $this->service;
- }
- /**
- * Gets the amount.
- *
- * @return Drupalcommerce_pricePrice
- * The amount.
- */
- public function getAmount() {
- return $this->amount;
- }
- /**
- * Gets the delivery date, if known.
- *
- * @return DrupalCoreDatetimeDrupalDateTime|null
- * The delivery date, or NULL.
- */
- public function getDeliveryDate() {
- return $this->deliveryDate;
- }
- /**
- * Gets the delivery terms, if known.
- *
- * Example: "Delivery in 1 to 3 business days."
- * Can be displayed to the end-user, if no translation is required.
- *
- * @return string|null
- * The delivery terms, or NULL.
- */
- public function getDeliveryTerms() {
- return $this->deliveryTerms;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement