commerce icon indicating copy to clipboard operation
commerce copied to clipboard

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

Open Anubarak opened this issue 6 years ago • 3 comments

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?

Anubarak avatar Oct 28 '19 09:10 Anubarak

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.

elivz avatar Feb 11 '20 16:02 elivz

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.

kswilliames avatar Mar 11 '20 01:03 kswilliames

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’m concerned that the alternative presented is not going to be the same structure, so it’ll be a breaking change. That’s 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’ve 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’re no longer indexed by their type lineItems is present, but they’re no longer indexed by their ID. I’m not quite sure how to handle the transition, but wanted to bring it to your attention.

lukeholder avatar Jun 10 '20 06:06 lukeholder