Commit 8d698c10 authored by Valentino Međimorec's avatar Valentino Međimorec Committed by Valentino Međimorec
Browse files

Issue #3271303 by valic: Integration with Commerce fee

parent b00c568b
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -13,5 +13,5 @@ dependencies:

test_dependencies:
  - commerce_shipping:commerce_shipping

php: 7.1
  - commerce_shipping:commerce_fee
php: 7.4
+10 −0
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@
 * Main module file.
 */

use Drupal\commerce_currency_resolver\Plugin\Commerce\Fee\OrderFixedAmount;
use Drupal\commerce_currency_resolver\Plugin\Commerce\Fee\OrderItemFixedAmount;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\commerce_currency_resolver\Plugin\Commerce\PromotionOffer\OrderItemFixedAmountOff;
@@ -35,6 +37,14 @@ function commerce_currency_resolver_commerce_promotion_offer_info_alter(array &$
  $definitions['order_fixed_amount_off']['class'] = OrderFixedAmountOff::class;
}

/**
 * Implements hook_commerce_fee_info_alter().
 */
function commerce_currency_resolver_commerce_fee_info_alter(array &$definitions) {
  $definitions['order_item_fixed_amount']['class'] = OrderItemFixedAmount::class;
  $definitions['order_fixed_amount']['class'] = OrderFixedAmount::class;
}

/**
 * Implements hook_commerce_condition_info_alter().
 */
+45 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_currency_resolver\Plugin\Commerce\Fee;

use Drupal\commerce_currency_resolver\Plugin\Commerce\CommerceCurrencyResolverAmountTrait;
use Drupal\commerce_fee\Plugin\Commerce\Fee\OrderFixedAmount as BaseOrderFixedAmount;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_fee\Entity\FeeInterface;
use Drupal\Core\Entity\EntityInterface;

class OrderFixedAmount extends BaseOrderFixedAmount {

  use CommerceCurrencyResolverAmountTrait;

  /**
   * {@inheritdoc}
   */
  public function apply(EntityInterface $entity, FeeInterface $fee) {
    $this->assertEntity($entity);

    // Nothing to do. Go to parent.
    if (!$this->shouldCurrencyRefresh($this->getAmount()->getCurrencyCode())) {
      return parent::apply($entity, $fee);
    }

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $entity;
    $amount = $this->getPrice($this->getAmount());

    // Split the amount between order items.
    $amounts = $this->splitter->split($order, $amount);

    foreach ($order->getItems() as $order_item) {
      if (isset($amounts[$order_item->id()])) {
        $order_item->addAdjustment(new Adjustment([
          'type' => 'fee',
          'label' => $fee->getDisplayName() ?: $this->t('Fee'),
          'amount' => $amounts[$order_item->id()],
          'source_id' => $fee->id(),
        ]));
      }
    }
  }

}
+41 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\commerce_currency_resolver\Plugin\Commerce\Fee;

use Drupal\commerce_currency_resolver\Plugin\Commerce\CommerceCurrencyResolverAmountTrait;
use Drupal\commerce_fee\Plugin\Commerce\Fee\OrderItemFixedAmount as BaseOrderItemFixedAmount;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_fee\Entity\FeeInterface;
use Drupal\Core\Entity\EntityInterface;

class OrderItemFixedAmount extends BaseOrderItemFixedAmount {

  use CommerceCurrencyResolverAmountTrait;

  /**
   * {@inheritdoc}
   */
  public function apply(EntityInterface $entity, FeeInterface $fee) {
    $this->assertEntity($entity);

    // Nothing to do. Go to parent.
    if (!$this->shouldCurrencyRefresh($this->getAmount()->getCurrencyCode())) {
      return parent::apply($entity, $fee);
    }

    /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
    $order_item = $entity;
    $amount = $this->getPrice($this->getAmount());

    $adjustment_amount = $amount->multiply($order_item->getQuantity());
    $adjustment_amount = $this->rounder->round($adjustment_amount);

    $order_item->addAdjustment(new Adjustment([
      'type' => 'fee',
      'label' => $fee->getDisplayName() ?: $this->t('Fee'),
      'amount' => $adjustment_amount,
      'source_id' => $fee->id(),
    ]));
  }

}