<p>Based on Robin's advice I've come to this solution...</p>

<p>First off I created simple plugin at <a href="https://pluginfactory.io/" rel="nofollow noreferrer">https://pluginfactory.io/</a> called Commerce Adjustments. Within the src folder I created a folder called 'controllers' and in that folder a file 'CartController.php' containing the following code. Now the CartArray can be adjusted and will be returned when updating the cart (/commerce-adjustments/cart/update-cart).</p>

<pre><code>&lt;?php
namespace commerceadjustments\controllers;

use Craft;
use craft\commerce\elements\Order;

use craft\commerce\controllers\CartController as Commerce_CartController;

class CartController extends Commerce_CartController
{
    protected function cartArray(Order $cart): array
    {
        $data = [];
        $data['id'] = $cart-&gt;id;
        $data['number'] = $cart-&gt;number;
        $data['couponCode'] = $cart-&gt;couponCode;
        $data['itemTotal'] = $cart-&gt;getItemTotal();
        $data['itemSubtotal'] = $cart-&gt;getItemSubtotal();
        $data['totalPaid'] = $cart-&gt;getTotalPaid();
        $data['email'] = $cart-&gt;getEmail();
        $data['isCompleted'] = $cart-&gt;isCompleted;
        $data['dateOrdered'] = $cart-&gt;dateOrdered;
        $data['datePaid'] = $cart-&gt;datePaid;
        $data['currency'] = $cart-&gt;currency;
        $data['paymentCurrency'] = $cart-&gt;paymentCurrency;
        $data['lastIp'] = $cart-&gt;lastIp;
        $data['message'] = $cart-&gt;message;
        $data['returnUrl'] = $cart-&gt;returnUrl;
        $data['cancelUrl'] = $cart-&gt;cancelUrl;
        $data['orderStatusId'] = $cart-&gt;orderStatusId;
        $data['shippingMethod'] = $cart-&gt;shippingMethodHandle;
        $data['shippingMethodId'] = $cart-&gt;getShippingMethodId();
        $data['paymentMethodId'] = $cart-&gt;gatewayId;
        $data['customerId'] = $cart-&gt;customerId;
        $data['isPaid'] = $cart-&gt;getIsPaid();
        $data['totalQty'] = $cart-&gt;getTotalQty();
        $data['pdfUrl'] = $cart-&gt;getPdfUrl() ? $cart-&gt;getPdfUrl('ajax') : '';
        $data['isEmpty'] = $cart-&gt;getIsEmpty();
        $data['itemSubtotal'] = $cart-&gt;getItemSubtotal();
        $data['totalWeight'] = $cart-&gt;getTotalWeight();
        $data['totalPrice'] = $cart-&gt;getTotalPrice();

        $data['availableShippingMethods'] = $cart-&gt;getAvailableShippingMethods();

        $data['shippingAddressId'] = $cart-&gt;shippingAddressId;
        if ($cart-&gt;getShippingAddress()) {
            $data['shippingAddress'] = $cart-&gt;shippingAddress-&gt;attributes;
        } else {
            $data['shippingAddress'] = null;
        }

        $data['billingAddressId'] = $cart-&gt;billingAddressId;
        if ($cart-&gt;getBillingAddress()) {
            $data['billingAddress'] = $cart-&gt;billingAddress-&gt;attributes;
        } else {
            $data['billingAddress'] = null;
        }

        $lineItems = [];
        foreach ($cart-&gt;lineItems as $lineItem) {
            $lineItemData = [];
            $lineItemData['id'] = $lineItem-&gt;id;
            $lineItemData['price'] = $lineItem-&gt;price;
            $lineItemData['saleAmount'] = $lineItem-&gt;saleAmount;
            $lineItemData['salePrice'] = $lineItem-&gt;salePrice;
            $lineItemData['qty'] = $lineItem-&gt;qty;
            $lineItemData['weight'] = $lineItem-&gt;weight;
            $lineItemData['length'] = $lineItem-&gt;length;
            $lineItemData['height'] = $lineItem-&gt;height;
            $lineItemData['width'] = $lineItem-&gt;width;
            $lineItemData['total'] = $lineItem-&gt;total;
            $lineItemData['qty'] = $lineItem-&gt;qty;
            $lineItemData['snapshot'] = $lineItem-&gt;snapshot;
            $lineItemData['note'] = $lineItem-&gt;note;
            $lineItemData['orderId'] = $lineItem-&gt;orderId;
            $lineItemData['purchasableId'] = $lineItem-&gt;purchasableId;
            $lineItemData['taxCategoryId'] = $lineItem-&gt;taxCategoryId;
            $lineItemData['shippingCategoryId'] = $lineItem-&gt;shippingCategoryId;
            $lineItemData['onSale'] = $lineItem-&gt;getOnSale();
            $lineItemData['options'] = $lineItem-&gt;options;
            $lineItemData['optionsSignature'] = $lineItem-&gt;getOptionsSignature();
            $lineItemData['subtotal'] = $lineItem-&gt;getSubtotal();
            $lineItemData['total'] = $lineItem-&gt;getTotal();
            $lineItems[$lineItem-&gt;id] = $lineItemData;
        }
        $data['lineItems'] = $lineItems;
        $data['totalLineItems'] = count($lineItems);

        $adjustments = [];
        foreach ($cart-&gt;adjustments as $adjustment) {
            $adjustmentData = [];
            $adjustmentData['id'] = $adjustment-&gt;id;
            $adjustmentData['type'] = $adjustment-&gt;type;
            $adjustmentData['name'] = $adjustment-&gt;name;
            $adjustmentData['description'] = $adjustment-&gt;description;
            $adjustmentData['amount'] = $adjustment-&gt;amount;
            $adjustmentData['sourceSnapshot'] = $adjustment-&gt;sourceSnapshot;
            $adjustmentData['orderId'] = $adjustment-&gt;orderId;
            $adjustments[$adjustment-&gt;type][] = $adjustmentData;
        }
        $data['adjustments'] = $adjustments;
        $data['totalAdjustments'] = count($adjustments);

        if ($cart-&gt;getErrors()) {
            $data['errors'] = $cart-&gt;getErrors();
        }

        // remove un-needed base element attributes
        $remove = ['archived', 'cancelUrl', 'lft', 'level', 'rgt', 'slug', 'uri', 'root'];
        foreach ($remove as $r) {
            unset($data[$r]);
        }

        return $data;
    }

}
</code></pre>

CC BY-SA 3.0
2018-04-30T20:36:11.583Z
2018-04-30T20:36:11.583Z
Fabian Hijlkema
<p>The most easiest solution in Your case is to create a custom Controller that extends the default <code>BaseFrontEndController</code> to call that one. The issue for Dependency Injection is the following piece of code:</p>

<p>For example when you do</p>

<pre><code>Craft::$container-&gt;set('craft\controllers\EntriesController', [
    'class' =&gt; 'prefix\namespace\controllers\FooController'
]);
</code></pre>

<p>You would overwrite the default <code>EntriesController</code> with your custom one and the function <code>createControllerByID</code> would do the following</p>

<pre><code>if (is_subclass_of($className, 'yii\base\Controller')) {
    $controller = Yii::createObject($className, [$id, $this]);
    // $controller is now my custom prefix\namespace\controllers\FooController instead of the default

    // but the line below -&gt; get_class($controller) === $className won't match
    return get_class($controller) === $className ? $controller : null;
}
</code></pre>

<p>Thus you'll receive an exception, it will work with most other classes/services/components that are created with <code>Yii::createObject</code> but not with Controllers. To change that behavior you would have to replace the <code>createControllerByID</code> in <code>yii\base\Module</code></p>

<p>Just do as <a href="https://craftcms.stackexchange.com/a/15034/5557">Brad Bell</a> already said and create a custom controller.</p>

CC BY-SA 3.0
2018-04-28T13:26:27.507Z
Hi Robin, thanks for your explanation, I'm going to work on a custom controller and come back when I have it working.
Fabian Hijlkema
CC BY-SA 3.0
2018-04-28T12:37:43.803Z
2018-04-28T12:37:43.803Z
Robin Schambach
<p>Is there a clean way to alter the cartArray that is returned when updating the cart through ajax? Which is defined in the BaseFrontEndController Craft Commerce.</p>

<p>For example by using a module? In this question <a href="https://craftcms.stackexchange.com/questions/15033/override-a-model-method-via-plugin">Override a model method via plugin</a>, Yii 2 <em>dependency injection</em> support is mentioned, but I feel like I need someone to point me into the right direction.</p>

<p>Thank you!</p>

<plugin-craftcommerce>
Overriding BaseFrontEndController in Craft Commerce
# Overriding BaseFrontEndController in Craft Commerce

(Asked by: username_0 on 2018-04-28)

Is there a clean way to alter the cartArray that is returned when updating the cart through ajax? Which is defined in the BaseFrontEndController Craft Commerce.

For example by using a module? In this question [Override a model method via plugin](https://craftcms.stackexchange.com/questions/15033/override-a-model-method-via-plugin), Yii 2 *dependency injection* support is mentioned, but I feel like I need someone to point me into the right direction.

Thank you!

**Tags:** plugin-craftcommerce


---

## 2 answers

### Answer by username_0 on 2018-04-30. Score: 0 | Accepted answer

Based on Robin's advice I've come to this solution...
First off I created simple plugin at https://pluginfactory.io/ called Commerce Adjustments. Within the src folder I created a folder called 'controllers' and in that folder a file 'CartController.php' containing the following code. Now the CartArray can be adjusted and will be returned when updating the cart (/commerce-adjustments/cart/update-cart).
id;
        $data['number'] = $cart->number;
        $data['couponCode'] = $cart->couponCode;
        $data['itemTotal'] = $cart->getItemTotal();
        $data['itemSubtotal'] = $cart->getItemSubtotal();
        $data['totalPaid'] = $cart->getTotalPaid();
        $data['email'] = $cart->getEmail();
        $data['isCompleted'] = $cart->isCompleted;
        $data['dateOrdered'] = $cart->dateOrdered;
        $data['datePaid'] = $cart->datePaid;
        $data['currency'] = $cart->currency;
        $data['paymentCurrency'] = $cart->paymentCurrency;
        $data['lastIp'] = $cart->lastIp;
        $data['message'] = $cart->message;
        $data['returnUrl'] = $cart->returnUrl;
        $data['cancelUrl'] = $cart->cancelUrl;
        $data['orderStatusId'] = $cart->orderStatusId;
        $data['shippingMethod'] = $cart->shippingMethodHandle;
        $data['shippingMethodId'] = $cart->getShippingMethodId();
        $data['paymentMethodId'] = $cart->gatewayId;
        $data['customerId'] = $cart->customerId;
        $data['isPaid'] = $cart->getIsPaid();
        $data['totalQty'] = $cart->getTotalQty();
        $data['pdfUrl'] = $cart->getPdfUrl() ? $cart->getPdfUrl('ajax') : '';
        $data['isEmpty'] = $cart->getIsEmpty();
        $data['itemSubtotal'] = $cart->getItemSubtotal();
        $data['totalWeight'] = $cart->getTotalWeight();
        $data['totalPrice'] = $cart->getTotalPrice();

        $data['availableShippingMethods'] = $cart->getAvailableShippingMethods();

        $data['shippingAddressId'] = $cart->shippingAddressId;
        if ($cart->getShippingAddress()) {
            $data['shippingAddress'] = $cart->shippingAddress->attributes;
        } else {
            $data['shippingAddress'] = null;
        }

        $data['billingAddressId'] = $cart->billingAddressId;
        if ($cart->getBillingAddress()) {
            $data['billingAddress'] = $cart->billingAddress->attributes;
        } else {
            $data['billingAddress'] = null;
        }

        $lineItems = [];
        foreach ($cart->lineItems as $lineItem) {
            $lineItemData = [];
            $lineItemData['id'] = $lineItem->id;
            $lineItemData['price'] = $lineItem->price;
            $lineItemData['saleAmount'] = $lineItem->saleAmount;
            $lineItemData['salePrice'] = $lineItem->salePrice;
            $lineItemData['qty'] = $lineItem->qty;
            $lineItemData['weight'] = $lineItem->weight;
            $lineItemData['length'] = $lineItem->length;
            $lineItemData['height'] = $lineItem->height;
            $lineItemData['width'] = $lineItem->width;
            $lineItemData['total'] = $lineItem->total;
            $lineItemData['qty'] = $lineItem->qty;
            $lineItemData['snapshot'] = $lineItem->snapshot;
            $lineItemData['note'] = $lineItem->note;
            $lineItemData['orderId'] = $lineItem->orderId;
            $lineItemData['purchasableId'] = $lineItem->purchasableId;
            $lineItemData['taxCategoryId'] = $lineItem->taxCategoryId;
            $lineItemData['shippingCategoryId'] = $lineItem->shippingCategoryId;
            $lineItemData['onSale'] = $lineItem->getOnSale();
            $lineItemData['options'] = $lineItem->options;
            $lineItemData['optionsSignature'] = $lineItem->getOptionsSignature();
            $lineItemData['subtotal'] = $lineItem->getSubtotal();
            $lineItemData['total'] = $lineItem->getTotal();
            $lineItems[$lineItem->id] = $lineItemData;
        }
        $data['lineItems'] = $lineItems;
        $data['totalLineItems'] = count($lineItems);

        $adjustments = [];
        foreach ($cart->adjustments as $adjustment) {
            $adjustmentData = [];
            $adjustmentData['id'] = $adjustment->id;
            $adjustmentData['type'] = $adjustment->type;
            $adjustmentData['name'] = $adjustment->name;
            $adjustmentData['description'] = $adjustment->description;
            $adjustmentData['amount'] = $adjustment->amount;
            $adjustmentData['sourceSnapshot'] = $adjustment->sourceSnapshot;
            $adjustmentData['orderId'] = $adjustment->orderId;
            $adjustments[$adjustment->type][] = $adjustmentData;
        }
        $data['adjustments'] = $adjustments;
        $data['totalAdjustments'] = count($adjustments);

        if ($cart->getErrors()) {
            $data['errors'] = $cart->getErrors();
        }

        // remove un-needed base element attributes
        $remove = ['archived', 'cancelUrl', 'lft', 'level', 'rgt', 'slug', 'uri', 'root'];
        foreach ($remove as $r) {
            unset($data[$r]);
        }

        return $data;
    }

}


---

### Answer by username_1 on 2018-04-28. Score: 1

The most easiest solution in Your case is to create a custom Controller that extends the default BaseFrontEndController to call that one. The issue for Dependency Injection is the following piece of code:
For example when you do
Craft::$container->set('craft\controllers\EntriesController', [
    'class' => 'prefix\namespace\controllers\FooController'
]);

You would overwrite the default EntriesController with your custom one and the function createControllerByID would do the following
if (is_subclass_of($className, 'yii\base\Controller')) {
    $controller = Yii::createObject($className, [$id, $this]);
    // $controller is now my custom prefix\namespace\controllers\FooController instead of the default

    // but the line below -> get_class($controller) === $className won't match
    return get_class($controller) === $className ? $controller : null;
}

Thus you'll receive an exception, it will work with most other classes/services/components that are created with Yii::createObject but not with Controllers. To change that behavior you would have to replace the createControllerByID in yii\base\Module
Just do as Brad Bell already said and create a custom controller.

> **Comments:**
> **username_0**: Hi Robin, thanks for your explanation, I'm going to work on a custom controller and come back when I have it working.  

---

