Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Model/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ public function initCheckout($reloadIfCurrencyChanged = true)
$billingAddress->save();
$shippingAddress->save();

$this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
$this->totalsCollector->collectQuoteTotals($quote);
// $this->totalsCollector->collectAddressTotals($quote, $shippingAddress);
// $this->totalsCollector->collectQuoteTotals($quote);

$quote->collectTotals();
$this->quoteRepository->save($quote);
Expand Down
57 changes: 55 additions & 2 deletions Model/Dibs/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,24 @@ class Items
protected $_itemsArray = [];

protected $addCustomOptionsToItemName = null;
/**
* @var \Magento\SalesRule\Api\RuleRepositoryInterface
*/
private $ruleRepository;

public function __construct(
\Dibs\EasyCheckout\Helper\Data $helper,
\Magento\Catalog\Helper\Product\Configuration $productConfig,
\Magento\Tax\Model\Calculation $calculationTool
\Magento\Tax\Model\Calculation $calculationTool,
\Magento\SalesRule\Api\RuleRepositoryInterface $ruleRepository
) {
$this->_helper = $helper;
$this->_productConfig = $productConfig;
$this->calculationTool = $calculationTool;

// resets all values
$this->init();
$this->ruleRepository = $ruleRepository;
}

public function init($store = null)
Expand Down Expand Up @@ -478,6 +484,48 @@ public function addDiscounts($couponCode)
return $this;
}

/**
* Check if discount was applied for whole cart
*
* @param Quote $quote
*/
private function addDiscountByCartRule(Quote $quote) : void
{
$ruleIds = $quote->getAppliedRuleIds();
if (!$ruleIds) {
return;
}

foreach (explode(',', $ruleIds) as $ruleId) {
try {
$rule = $this->ruleRepository->getById($ruleId);
} catch (\Exception $e) {
continue;
}

if ($rule->getSimpleAction() != 'cart_fixed') {
continue;
}

$discount = $quote->getSubtotal() - $quote->getSubtotalWithDiscount();
$discount = $this->addZeroes($discount);
$orderItem = new OrderItem();
$reference = $rule->getName();
$orderItem
->setReference($reference)
->setName($quote->getCouponCode() ? (string)__('Discount (%1)', $quote->getCouponCode()) : (string)__('Discount'))
->setUnit("st")
->setQuantity(1)
->setTaxRate($this->addZeroes(0)) // the tax rate i.e 25% (2500)
->setTaxAmount(0) // total tax amount
->setUnitPrice(-$discount) // excl. tax price per item
->setNetTotalAmount(-$discount) // excl. tax
->setGrossTotalAmount(-$discount); // incl. tax

$this->_cart[$reference] = $orderItem;
}
}

public function validateTotals($grandTotal)
{
//calculate Dibs total
Expand Down Expand Up @@ -591,7 +639,12 @@ public function generateOrderItemsFromQuote(Quote $quote)
$this->addShipping($shippingAddress);
}

$this->addDiscounts($quote->getCouponCode());
// If there is no discount per items, but code does exist
// it means, that discount was applied on whole cart
// @TODO: Refactor this to external model
count($this->_discounts)
? $this->addDiscounts($quote->getCouponCode())
: $this->addDiscountByCartRule($quote);

try {
$this->validateTotals($quote->getGrandTotal());
Expand Down