Commerce: Plugin::getInstance()->getOrders()->cartArray($cart); deprecated

Created on 28 Oct 2019  路  3Comments  路  Source: craftcms/commerce

This is just a question about your plans for

Plugin::getInstance()->getOrders()->cartArray($cart); 

The function is marked as deprecated but you suggest to use it in this comment https://github.com/craftcms/commerce/issues/817
and the $order->toArray() returns a different array. Thats why I ask: when is it going to be dropped? Do we need to configure the result of toArray to return the same array as cartArray did in order to receive certain values?

I would just like to know how to use this function since I made a REST API with Commerce and Carts and currently I return Carts via fractal over this function.
Do I need to copy&paste it into my transformer in the next few releases?

馃悶 bug

Most helpful comment

Would be great also if the EVENT_MODIFY_CART_INFO event would just apply universally to the $order->toArray() function, so we could get consistent cart data no matter whether it is accessed via Twig or AJAX.

All 3 comments

Would be great also if the EVENT_MODIFY_CART_INFO event would just apply universally to the $order->toArray() function, so we could get consistent cart data no matter whether it is accessed via Twig or AJAX.

Would be great also if the EVENT_MODIFY_CART_INFO event would just apply universally to the $order->toArray() function, so we could get consistent cart data no matter whether it is accessed via Twig or AJAX.

+1 to this.

I'm also missing fields we rely on with the cart returned by the commerce/payments/pay endpoint . I can transform using the EVENT_MODIFY_CART_INFO event for other endpoints but that doesn't apply to $order-toArray() inside PaymentController::actionPay.

Hey all,

According to the changelog, the cartArray() function is marked as deprecated. We use this on all of our Vue-based sites, so its important we have a similar function available. This also effects the returned response from cart action controllers, so its pretty important. I鈥檓 concerned that the alternative presented is not going to be the same structure, so it鈥檒l be a breaking change. That鈥檚 fine - either document the breaking changes when the time comes, or fix things up so its not a breaking change - your call! 馃榿

I wanted to document the specifics.

Refer to the call made in Commerce 2: Commerce::getInstance()->getOrders()->cartArray($cart); and its respective output:

Compared to a similar solution in Commerce 3: `$cart->toArray([], ['shippingAddress', 'billingAddress', 'availableShippingMethods', 'adjustments', 'lineItems.snapshot']); and its output

(Note that in templates you can use craft.commerce.orders.cartArray(), and we鈥檝e built our own similar twig function in a module to call our $cart->toArray(), but this could also be done in templates. Just a little easier to compare in PHP).

Notable differences are:
shippingMethod changed to shippingMethodHandle
Missing shippingMethodId, paymentMethodId, pdfUrl, isEmpty ,totalTax, totalTaxIncluded, totalShippingCost, totalDiscount, totalLineItems, totalAdjustments
availableShippingMethods is present, but price is missing
adjustments is present, but they鈥檙e no longer indexed by their type
lineItems is present, but they鈥檙e no longer indexed by their ID.
I鈥檓 not quite sure how to handle the transition, but wanted to bring it to your attention.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lukeholder picture lukeholder  路  3Comments

alexanderloewe picture alexanderloewe  路  4Comments

Tam picture Tam  路  7Comments

jmauzyk picture jmauzyk  路  5Comments

nfourtythree picture nfourtythree  路  6Comments