2

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, Yii 2 dependency injection support is mentioned, but I feel like I need someone to point me into the right direction.

Thank you!

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.

|improve this answer|||||
  • 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 Apr 28 '18 at 13:26
0

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).

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

}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.