src/Backend/Modules/Commerce/Domain/Cart/Cart.php
/**
     * Convert the cart information to JSON. This helps to make Ajax responses easier.
     */
Convert the cart information to JSON. This helps to make Ajax responses easier.
Convert the cart information to JSON . This helps to make Ajax responses easier .
public function jsonSerialize(): array
    {
        $this->moneyFormatter = new MoneyFormatter();
        $this->recalculateCart();
        return [
            'totalQuantity' => $this->getTotalQuantity(),
            'cartRules' => array_map(function (CartRule $item) {
                return [
                    'id' => $item->getId(),
                    'title' => $item->getTitle(),
                    'code' => $item->getCode(),
                    'total' => $this->getCartRuleTotal($item) !== null ? $this->moneyFormatter->asFloat($this->getCartRuleTotal($item)) : null,
                ];
            }, $this->getCartRules()->toArray()),
            'subTotal' => $this->moneyFormatter->asFloat($this->getSubTotal()),
            'vats' => array_map(function ($item) {
                return [
                    'id' => $item['id'],
                    'title' => $item['title'],
                    'total' => $this->moneyFormatter->asFloat($item['total']), 
                ];
            }, array_values($this->getVats())),
            'total' => $this->moneyFormatter->asFloat($this->getTotal()),
            'items' => array_map(function (CartValue $cartValue) {
                return [
                    'id' => $cartValue->getId(),
                    'sku' => $cartValue->getProduct()->getSku(),
                    'name' => $cartValue->getProduct()->getTitle(),
                    'url' => $cartValue->getProduct()->getUrl(),
                    'thumbnail' => $cartValue->getProduct()->getThumbnail()->getWebPath('product_thumbnail'),
                    'category' => $cartValue->getProduct()->getCategory()->getFullCategoryPath(),
                    'brand' => $cartValue->getProduct()->getBrand()->getTitle(),
                    'price' => $this->moneyFormatter->asFloat($cartValue->getProduct()->getActivePrice(false)),
                    'quantity' => $cartValue->getQuantity(),
                    'total' => $this->moneyFormatter->asFloat($cartValue->getTotal()),
                ];
            }, $this->getValues()->toArray()),
        ];
    }
public function jsonSerialize ( ) : array { $ this -> moneyFormatter = new MoneyFormatter ( ) ; $ this -> recalculateCart ( ) ; return [ 'totalQuantity' => $ this -> getTotalQuantity ( ) , 'cartRules' => array_map ( function ( CartRule $ item ) { return [ 'id' => $ item -> getId ( ) , 'title' => $ item -> getTitle ( ) , 'code' => $ item -> getCode ( ) , 'total' => $ this -> getCartRuleTotal ( $ item ) !== null ? $ this -> moneyFormatter -> asFloat ( $ this -> getCartRuleTotal ( $ item ) ) : null , ] ; } , $ this -> getCartRules ( ) -> toArray ( ) ) , 'subTotal' => $ this -> moneyFormatter -> asFloat ( $ this -> getSubTotal ( ) ) , 'vats' => array_map ( function ( $ item ) { return [ 'id' => $ item [ 'id' ] , 'title' => $ item [ 'title' ] , 'total' => $ this -> moneyFormatter -> asFloat ( $ item [ 'total' ] ) , ] ; } , array_values ( $ this -> getVats ( ) ) ) , 'total' => $ this -> moneyFormatter -> asFloat ( $ this -> getTotal ( ) ) , 'items' => array_map ( function ( CartValue $ cartValue ) { return [ 'id' => $ cartValue -> getId ( ) , 'sku' => $ cartValue -> getProduct ( ) -> getSku ( ) , 'name' => $ cartValue -> getProduct ( ) -> getTitle ( ) , 'url' => $ cartValue -> getProduct ( ) -> getUrl ( ) , 'thumbnail' => $ cartValue -> getProduct ( ) -> getThumbnail ( ) -> getWebPath ( 'product_thumbnail' ) , 'category' => $ cartValue -> getProduct ( ) -> getCategory ( ) -> getFullCategoryPath ( ) , 'brand' => $ cartValue -> getProduct ( ) -> getBrand ( ) -> getTitle ( ) , 'price' => $ this -> moneyFormatter -> asFloat ( $ cartValue -> getProduct ( ) -> getActivePrice ( false ) ) , 'quantity' => $ cartValue -> getQuantity ( ) , 'total' => $ this -> moneyFormatter -> asFloat ( $ cartValue -> getTotal ( ) ) , ] ; } , $ this -> getValues ( ) -> toArray ( ) ) , ] ; }
Convert the cart information to JSON.
Convert the cart information to JSON .
// Convert Money to plain string