How can I round up percentage to two decimals?

by brij2136   Last Updated June 12, 2019 21:07 PM

I'm looking for a way to round the percentage from Drupal 8 Commerce Shipping Price Matrix module to cents in USD. Is this possible?

I found this function in the commerce_shipping_price_matrix/src/ Plugin/Commerce/ShippingMethod/ShippingMethodBase.php file:

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());
  }

This function in the commerce_shipping_price_matrix/src/Plugin/Commerce/ShippingMethod/ PriceMatrix.php file:

 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;
    }
  }

And this in the commerce_shipping/src/ShippingRate.php file:

class ShippingRate {

  /**
   * The ID.
   *
   * @var string
   */
  protected $id;

  /**
   * The shipping service.
   *
   * @var \Drupal\commerce_shipping\ShippingService
   */
  protected $service;

  /**
   * The amount.
   *
   * @var \Drupal\commerce_price\Price
   */
  protected $amount;

  /**
   * The delivery date.
   *
   * @var \Drupal\Core\Datetime\DrupalDateTime
   */
  protected $deliveryDate;

  /**
   * The delivery terms.
   *
   * @var string
   */
  protected $deliveryTerms;

  /**
   * Constructs a new ShippingRate instance.
   *
   * @param string $id
   *   The ID.
   * @param \Drupal\commerce_shipping\ShippingService $service
   *   The shipping service.
   * @param \Drupal\commerce_price\Price $amount
   *   The amount.
   * @param \Drupal\Core\Datetime\DrupalDateTime $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 \Drupal\commerce_shipping\ShippingService
   *   The shipping service.
   */
  public function getService() {
    return $this->service;
  }

  /**
   * Gets the amount.
   *
   * @return \Drupal\commerce_price\Price
   *   The amount.
   */
  public function getAmount() {
    return $this->amount;
  }

  /**
   * Gets the delivery date, if known.
   *
   * @return \Drupal\Core\Datetime\DrupalDateTime|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;
  }

}

I don't know if I'm on the right track and I don't know how to write a hook function for this.

My example is this:

Price matrix set-up: For any amount over $40, charge 17% shipping.

If I have 3 products at $19.99 ea., my subtotal is $59.97. The shipping at 17% is $10.1949. My order currently is showing all the decimals. But I need it to round the shipping to $10.19. If it were $10.1959, I would need it to round to $10.20.

Any help would be greatly appreciated. Thanks.



Related Questions


Updated July 14, 2015 14:03 PM

Updated August 15, 2015 17:03 PM

Updated May 14, 2015 15:03 PM

Updated April 07, 2015 18:03 PM

Updated June 02, 2015 14:03 PM