Skip to content

Commit

Permalink
Merge pull request #226 from Skullsneeze/patch-1
Browse files Browse the repository at this point in the history
Update price-adjustments.md
  • Loading branch information
jeff-matthews authored Mar 18, 2024
2 parents d04fda8 + c22d2c1 commit 74072c2
Showing 1 changed file with 127 additions and 16 deletions.
143 changes: 127 additions & 16 deletions src/pages/development/components/price-adjustments.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ class Adjustment implements AdjustmentInterface
}
```

The `ADJUSTMENT_CODE` constant is a unique code for the adjustment which it gets added to the `Magento\Framework\Pricing\Adjustment\Collection` collection.
The `ADJUSTMENT_CODE` constant is a unique code for the adjustment which is added to the `Magento\Framework\Pricing\Adjustment\Collection` collection.

The adjustment logic is defined in `extractAdjustment` and `applyAdjustment` functions.
The adjustment logic is defined in the `extractAdjustment` and `applyAdjustment` functions.

Price adjustments affect storefront product prices.

Price adjustments **will not** affect quote item prices, so when a product is added to the cart, any price adjustments defined above are discarded.

## Add price adjustments for quote items

To adjustments prices for quote items, a custom quote total is added:
To add price adjustments for quote items, a custom quote total is added:

Add the following to `VENDOR/MODULE/etc/sales.xml`:

Expand Down Expand Up @@ -168,15 +168,15 @@ class Surcharge extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
const COLLECTOR_TYPE_CODE = 'custom-surcharge';

/**
* Custom constructor.
* Surcharge constructor.
*/
public function __construct()
{
$this->setCode(self::COLLECTOR_TYPE_CODE);
}

/**
* Collect address discount amount
* Collect totals including custom surcharge.
*
* @param Quote $quote
* @param ShippingAssignmentInterface $shippingAssignment
Expand Down Expand Up @@ -240,7 +240,7 @@ class Surcharge extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal

return [
'code' => $this->getCode(),
'title' => __('Custom Total'),
'title' => __('Custom Surcharge Total'),
'value' => $amount
];
}
Expand All @@ -250,12 +250,12 @@ class Surcharge extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
*/
public function getLabel()
{
return __('Custom Surchange');
return __('Custom Surcharge');
}
}
```

The `COLLECTOR_TYPE_CODE` constant is a unique name of the custom total. It can be accessed with `Magento\Quote\Model\Quote\Address\Total::getTotalAmount`, and set with `Magento\Quote\Model\Quote\Address\Total::setTotalAmount`.
The `COLLECTOR_TYPE_CODE` constant is a unique name of the custom total. The custom total can be retrieved with `Magento\Quote\Model\Quote\Address\Total::getTotalAmount`, and set with `Magento\Quote\Model\Quote\Address\Total::setTotalAmount`.

### Display price-adjusted totals on the cart page

Expand All @@ -277,6 +277,7 @@ First, add the new total:
<item name="children" xsi:type="array">
<item name="custom-surcharge" xsi:type="array">
<item name="component" xsi:type="string">VENDOR_MODULE/js/view/cart/totals/surcharge</item>
<item name="sortOrder" xsi:type="string">25</item>
<item name="config" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Custom Surcharge</item>
</item>
Expand All @@ -298,8 +299,10 @@ Then, define a new component: `VENDOR_MODULE/js/view/cart/totals/surcharge`:
```javascript
define([
'Magento_Checkout/js/view/summary/abstract-total',
'Magento_Checkout/js/model/quote'
], function (Component, quote) {
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/totals',
'mage/translate'
], function (Component, quote, totals, $t) {
'use strict';

return Component.extend({
Expand All @@ -325,19 +328,25 @@ define([
return null;
}

return 'Custom Surcharge';
return $t('Custom Surcharge');
},

/**
* @return {Number}
*/
getPureValue: function () {
var price = 0;
for (var i=0; i < window.checkoutConfig.quoteItemData.length; i++) {
price += window.checkoutConfig.quoteItemData[i].qty * 1.79;
let amount = 0,
customSurchargeTotal;

if (this.totals()) {
customSurchargeTotal = totals.getSegment('custom-surcharge');

if (customSurchargeTotal) {
amount = customSurchargeTotal.value;
}
}

return price;
return amount;
},

/**
Expand All @@ -352,6 +361,8 @@ define([

Then create the template `VENDOR_MODULE/summary/surcharge`:

`VENDOR/MODULE/view/frontend/web/template/summary/surcharge.html`:

```html
<!-- ko if: isDisplayed() -->
<tr class="totals surcharge">
Expand Down Expand Up @@ -387,8 +398,9 @@ To display the price-adjusted total on the checkout page, add it to the `totals`
<item name="children" xsi:type="array">
<item name="totals" xsi:type="array">
<item name="children" xsi:type="array">
<item name="discount" xsi:type="array">
<item name="custom-surcharge" xsi:type="array">
<item name="component" xsi:type="string">VENDOR_MODULE/js/view/cart/totals/surcharge</item>
<item name="sortOrder" xsi:type="string">25</item>
<item name="config" xsi:type="array">
<item name="title" xsi:type="string" translate="true">Custom Surcharge</item>
</item>
Expand All @@ -412,3 +424,102 @@ To display the price-adjusted total on the checkout page, add it to the `totals`
The `VENDOR_MODULE/js/view/cart/totals/surcharge` component was defined earlier in the article.

If all has gone smoothly, when run, you should see the adjusted price reflected in the shopping cart.

### Display price adjustment in order totals in the admin UI

To display the price adjustment as a separate row in the order totals on the order detail page within the admin UI, add it to the `order_totals` block.

`VENDOR/MODULE/view/adminhtml/layout/sales_order_view.xml`:

```xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="order_totals">
<block class="VENDOR\MODULE\Block\Adminhtml\Sales\Order\Totals" name="custom_surcharge_total">
<action method="setBeforeCondition">
<argument name="condition" xsi:type="string">tax</argument>
</action>
</block>
</referenceBlock>
</body>
</page>
```

You can adjust the value for `condition` in the `setBeforeCondition` action to change before which row the adjustment is added.

The `order_totals` block triggers the `initTotals()` method for each child block.

Next we need to define our block.

`VENDOR/MODULE/Block/Adminhtml/Sales/Order/Totals`:

```php
namespace VENDOR\MODULE\Block\Adminhtml\Sales\Order;

use Magento\Framework\DataObject;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Sales\Model\Order;
use VENDOR\MODULE\Pricing\Adjustment;
use VENDOR\MODULE\Model\Quote\Surcharge;

class Totals extends Template
{

/**
* @var Surcharge
*/
private Surcharge $surcharge;

/**
* @param Context $context
* @param Surcharge $surcharge
* @param array $data
*/
public function __construct(
Context $context,
Surcharge $surcharge,
array $data = []
) {
parent::__construct($context, $data);
$this->surcharge = $surcharge;
}

/**
* @return $this
*/
public function initTotals()
{
$adjustmentValue = 0;
$items = $this->getParentBlock()->getSource()->getAllItems();

foreach ($items as $item) {
$adjustmentValue += $item->getQtyOrdered() * Adjustment::ADJUSTMENT_VALUE;
}

if ($adjustmentValue) {
$totals = $this->getParentBlock()->getTotals();
$total = new DataObject(
[
'code' => Surcharge::COLLECTOR_TYPE_CODE,
'label' => $this->surcharge->getLabel(),
'value' => $adjustmentValue,
'base_value' => $adjustmentValue
]
);

if (isset($totals['grand_total_incl'])) {
$this->getParentBlock()->addTotalBefore($total, 'grand_total');
} else {
$this->getParentBlock()->addTotalBefore($total, $this->getBeforeCondition());
}
}

return $this;
}
}

```

If all went well you should now see a new row as part of the order totals in the admin UI whenever your price adjustment is applied.

0 comments on commit 74072c2

Please sign in to comment.