src/Frontend/Modules/Commerce/Ajax/UpdateCart.php
/**
     * Add or update the product in our cart.
     *
     * @return CartValue|false
     */
Add or update the product in our cart.
Add or update the product in our cart .
private function updateProduct()
    {
        $productData = $this->getRequest()->request->get('product');
        if (!is_array($productData)) {
            return false;
        }
        if (!array_key_exists('id', $productData) || !array_key_exists('amount', $productData)) {
            return false;
        }
        $overwrite = false;
        if (array_key_exists('overwrite', $productData)) {
            $overwrite = $productData['overwrite'];
        }
        try {
            $product = $this->getProductRepository()->findOneActiveByIdAndLocale($productData['id'], Locale::frontendLanguage());
        } catch (ProductNotFound $e) {
            return false;
        }
        $this->form = $this->getForm($product);
        $this->form->handleRequest($this->getRequest());
        if (!$this->form->isSubmitted() || !$this->form->isValid()) {
            $fields = [];
            foreach ($this->form->getErrors(true) as $error) {
                $fields[$error->getOrigin()->getName()] = $error->getMessage();
            }
            $this->errors = [
                'fields' => $fields,
                'url' => $product->getUrl(),
            ];
            return false;
        }
        $this->data = $this->form->getData();
        if ($this->data->amount <= $product->getOrderQuantity()) {
            $this->data->amount = $product->getOrderQuantity();
        }
        if (!array_key_exists('quote', $productData) && $product->isFromStock() && $this->data->amount > $product->getStock()) {
            $this->errors = [
                'fields' => [
                    'amount' => Language::err('GivenAmountNotInStock'),
                ],
            ];
            return false;
        }
        $cartValueRepository = $this->getCartValueRepository();
        if ($this->cartValueId) {
            $cartValue = $cartValueRepository->getByCartAndId($this->cart, $this->cartValueId);
            if (!$cartValue) {
                return false;
            }
        } elseif ($product->usesDimensions()) {
            $cartValue = new CartValue();
            $cartValue->setCart($this->cart);
            $cartValue->setProduct($product);
        } else {
            $cartValue = $cartValueRepository->getByCartAndProduct($this->cart, $product);
        }
        if ($overwrite) {
            $cartValue->setQuantity($this->data->amount);
        } else {
            $cartValue->setQuantity($cartValue->getQuantity() + $this->data->amount);
        }
        if ($product->usesDimensions()) {
            $this->addWidth($this->data->width);
            $this->addHeight($this->data->height);
            $this->addWidth($product->getExtraProductionWidth());
            $this->addHeight($product->getExtraProductionHeight());
            $this->parseProductOptionsDimension($product->getProductOptions());
            $dimension = $this->getProductDimensionRepository()
                ->findByProductAndDimensions($product, $this->getWidth(), $this->getHeight());
            if (!$dimension) {
                $this->errors = [
                    'fields' => [
                        'width' => Language::err('CantFindProductWithGivenDimensions'),
                    ],
                ];
                return false;
            }
            $this->setBasePrice($dimension->getPrice());
            $cartValue->setProductDimension($dimension);
            $cartValue->setWidth($this->data->width);
            $cartValue->setHeight($this->data->height);
            $cartValue->setOrderWidth($this->getWidth());
            $cartValue->setOrderHeight($this->getHeight());
        } else {
            $this->setBasePrice($product->getActivePrice(false));
        }
        $this->addTotalPrice($this->getBasePrice());
        $this->addProductOptionsToCart($product, $cartValue);
        $cartValue->setTotal($this->getTotalPrice()->multiply($cartValue->getQuantity()));
        $this->cart->addValue($cartValue);
        if (array_key_exists('up_sell', $productData)) {
            $this->upSellProducts($productData['up_sell']);
        }
        return $cartValue;
    }
private function updateProduct ( ) { $ productData = $ this -> getRequest ( ) -> request -> get ( 'product' ) ; if ( ! is_array ( $ productData ) ) { return false ; } if ( ! array_key_exists ( 'id' , $ productData ) || ! array_key_exists ( 'amount' , $ productData ) ) { return false ; } $ overwrite = false ; if ( array_key_exists ( 'overwrite' , $ productData ) ) { $ overwrite = $ productData [ 'overwrite' ] ; } try { $ product = $ this -> getProductRepository ( ) -> findOneActiveByIdAndLocale ( $ productData [ 'id' ] , Locale :: frontendLanguage ( ) ) ; } catch ( ProductNotFound $ e ) { return false ; } $ this -> form = $ this -> getForm ( $ product ) ; $ this -> form -> handleRequest ( $ this -> getRequest ( ) ) ; if ( ! $ this -> form -> isSubmitted ( ) || ! $ this -> form -> isValid ( ) ) { $ fields = [ ] ; foreach ( $ this -> form -> getErrors ( true ) as $ error ) { $ fields [ $ error -> getOrigin ( ) -> getName ( ) ] = $ error -> getMessage ( ) ; } $ this -> errors = [ 'fields' => $ fields , 'url' => $ product -> getUrl ( ) , ] ; return false ; } $ this -> data = $ this -> form -> getData ( ) ; if ( $ this -> data -> amount <= $ product -> getOrderQuantity ( ) ) { $ this -> data -> amount = $ product -> getOrderQuantity ( ) ; } if ( ! array_key_exists ( 'quote' , $ productData ) && $ product -> isFromStock ( ) && $ this -> data -> amount > $ product -> getStock ( ) ) { $ this -> errors = [ 'fields' => [ 'amount' => Language :: err ( 'GivenAmountNotInStock' ) , ] , ] ; return false ; } $ cartValueRepository = $ this -> getCartValueRepository ( ) ; if ( $ this -> cartValueId ) { $ cartValue = $ cartValueRepository -> getByCartAndId ( $ this -> cart , $ this -> cartValueId ) ; if ( ! $ cartValue ) { return false ; } } elseif ( $ product -> usesDimensions ( ) ) { $ cartValue = new CartValue ( ) ; $ cartValue -> setCart ( $ this -> cart ) ; $ cartValue -> setProduct ( $ product ) ; } else { $ cartValue = $ cartValueRepository -> getByCartAndProduct ( $ this -> cart , $ product ) ; } if ( $ overwrite ) { $ cartValue -> setQuantity ( $ this -> data -> amount ) ; } else { $ cartValue -> setQuantity ( $ cartValue -> getQuantity ( ) + $ this -> data -> amount ) ; } if ( $ product -> usesDimensions ( ) ) { $ this -> addWidth ( $ this -> data -> width ) ; $ this -> addHeight ( $ this -> data -> height ) ; $ this -> addWidth ( $ product -> getExtraProductionWidth ( ) ) ; $ this -> addHeight ( $ product -> getExtraProductionHeight ( ) ) ; $ this -> parseProductOptionsDimension ( $ product -> getProductOptions ( ) ) ; $ dimension = $ this -> getProductDimensionRepository ( ) -> findByProductAndDimensions ( $ product , $ this -> getWidth ( ) , $ this -> getHeight ( ) ) ; if ( ! $ dimension ) { $ this -> errors = [ 'fields' => [ 'width' => Language :: err ( 'CantFindProductWithGivenDimensions' ) , ] , ] ; return false ; } $ this -> setBasePrice ( $ dimension -> getPrice ( ) ) ; $ cartValue -> setProductDimension ( $ dimension ) ; $ cartValue -> setWidth ( $ this -> data -> width ) ; $ cartValue -> setHeight ( $ this -> data -> height ) ; $ cartValue -> setOrderWidth ( $ this -> getWidth ( ) ) ; $ cartValue -> setOrderHeight ( $ this -> getHeight ( ) ) ; } else { $ this -> setBasePrice ( $ product -> getActivePrice ( false ) ) ; } $ this -> addTotalPrice ( $ this -> getBasePrice ( ) ) ; $ this -> addProductOptionsToCart ( $ product , $ cartValue ) ; $ cartValue -> setTotal ( $ this -> getTotalPrice ( ) -> multiply ( $ cartValue -> getQuantity ( ) ) ) ; $ this -> cart -> addValue ( $ cartValue ) ; if ( array_key_exists ( 'up_sell' , $ productData ) ) { $ this -> upSellProducts ( $ productData [ 'up_sell' ] ) ; } return $ cartValue ; }
Add or update the product in our cart.
Add or update the product in our cart .
// Update or overwrite product // Retrieve our product // Get the form on which we are going to validate our stuff // Disable amount below the minimal order quantity // Validate request // Get the cart value if exists or create a new one // Set the values // Choose which product to use and set some default price // Set the given dimensions // Add extra production dimensions // Parse any given dimensions // Set the total price for later usage // Add the product options to the cart // Add our product to the cart // Handle the product upsell