src/Frontend/Modules/Commerce/Ajax/UpdateCart.php
/**
     * Add the product options to the cart.
     */
Add the product options to the cart.
Add the product options to the cart .
private function addProductOptionsToCart(Product $product, CartValue $cartValue): void
    {
        $this->data = $this->form->getData();
        foreach ($cartValue->getCartValueOptions() as $cartValueOption) {
            $cartValue->removeCartValueOption($cartValueOption);
        }
        foreach ($product->getProductOptionsWithSubOptions() as $productOption) {
            $fieldName = 'option_' . $productOption->getId();
            $customValueFieldName = $fieldName . '_custom_value';
            if ($this->data->{$fieldName} || isset($this->data->{$customValueFieldName})) {
                $productOptionValue = $this->data->{$fieldName};
                try {
                    $cartValueOption = new CartValueOption();
                    $cartValueOption->setCartValue($cartValue);
                    $cartValueOption->setProductOption($productOption);
                    $cartValueOption->setName($productOption->getTitle());
                    if ($productOptionValue instanceof ProductOptionValue) {
                        $cartValueOption->setImpactType($productOptionValue->getImpactType());
                    }
                    if ($productOption->isCustomValueAllowed() && $this->data->{$customValueFieldName}) {
                        $cartValueOption->setValue($this->data->{$customValueFieldName});
                        $cartValueOption->setPrice($productOption->getCustomValuePrice());
                        $cartValueOption->setVat($product->getVat());
                        $cartValueOption->setVatPrice($productOption->getCustomValuePrice()->multiply($product->getVat()->getAsPercentage()));
                    } else {
                        switch ($productOption->getType()) {
                            case ProductOption::DISPLAY_TYPE_TEXT:
                                $value = $productOptionValue;
                                break;
                            case ProductOption::DISPLAY_TYPE_BETWEEN:
                                $value = $this->data->{$customValueFieldName};
                                break;
                            default:
                                $value = $productOptionValue->getTitle();
                                break;
                        }
                        $cartValueOption->setValue($value);
                        $cartValueOption->setVat($product->getVat());
                        $cartValueOption->setPrice(0);
                        $cartValueOption->setVatPrice(0);
                        if ($productOptionValue instanceof ProductOptionValue) {
                            $cartValueOption->setProductOptionValue($productOptionValue);
                            $cartValueOption->setVat($productOptionValue->getVat());
                            if ($productOptionValue->getPercentage() > 0) {
                                $productOptionValuePrice = $this->getBasePrice()->multiply(($productOptionValue->getPercentage() / 100));
                                $cartValueOption->setPrice($productOptionValuePrice);
                                $cartValueOption->setVatPrice($productOptionValuePrice->multiply($productOptionValue->getVat()->getAsPercentage()));
                            } else {
                                $cartValueOption->setPrice($productOptionValue->getPrice());
                                $cartValueOption->setVatPrice($productOptionValue->getVatPrice());
                            }
                        }
                    }
                    if ($productOption->isSquareUnitType() && $product->usesDimensions()) {
                        $square = ceil(($this->getWidth() / 100) * ($this->getHeight() / 100));
                        $cartValueOption->setPrice($cartValueOption->getPrice()->multiply($square));
                        $cartValueOption->setVatPrice($cartValueOption->getVatPrice()->multiply($square));
                    }
                    $cartValue->addCartValueOption($cartValueOption);
                    if (!$productOptionValue instanceof ProductOptionValue || $productOptionValue->isImpactTypeAdd()) {
                        $this->addTotalPrice($cartValueOption->getPrice());
                    } else {
                        $this->addTotalPrice($cartValueOption->getPrice() * -1);
                    }
                } catch (NonUniqueResultException $e) {
                    continue;
                }
            }
        }
    }
private function addProductOptionsToCart ( Product $ product , CartValue $ cartValue ) : void { $ this -> data = $ this -> form -> getData ( ) ; foreach ( $ cartValue -> getCartValueOptions ( ) as $ cartValueOption ) { $ cartValue -> removeCartValueOption ( $ cartValueOption ) ; } foreach ( $ product -> getProductOptionsWithSubOptions ( ) as $ productOption ) { $ fieldName = 'option_' . $ productOption -> getId ( ) ; $ customValueFieldName = $ fieldName . '_custom_value' ; if ( $ this -> data -> { $ fieldName } || isset ( $ this -> data -> { $ customValueFieldName } ) ) { $ productOptionValue = $ this -> data -> { $ fieldName } ; try { $ cartValueOption = new CartValueOption ( ) ; $ cartValueOption -> setCartValue ( $ cartValue ) ; $ cartValueOption -> setProductOption ( $ productOption ) ; $ cartValueOption -> setName ( $ productOption -> getTitle ( ) ) ; if ( $ productOptionValue instanceof ProductOptionValue ) { $ cartValueOption -> setImpactType ( $ productOptionValue -> getImpactType ( ) ) ; } if ( $ productOption -> isCustomValueAllowed ( ) && $ this -> data -> { $ customValueFieldName } ) { $ cartValueOption -> setValue ( $ this -> data -> { $ customValueFieldName } ) ; $ cartValueOption -> setPrice ( $ productOption -> getCustomValuePrice ( ) ) ; $ cartValueOption -> setVat ( $ product -> getVat ( ) ) ; $ cartValueOption -> setVatPrice ( $ productOption -> getCustomValuePrice ( ) -> multiply ( $ product -> getVat ( ) -> getAsPercentage ( ) ) ) ; } else { switch ( $ productOption -> getType ( ) ) { case ProductOption :: DISPLAY_TYPE_TEXT : $ value = $ productOptionValue ; break ; case ProductOption :: DISPLAY_TYPE_BETWEEN : $ value = $ this -> data -> { $ customValueFieldName } ; break ; default : $ value = $ productOptionValue -> getTitle ( ) ; break ; } $ cartValueOption -> setValue ( $ value ) ; $ cartValueOption -> setVat ( $ product -> getVat ( ) ) ; $ cartValueOption -> setPrice ( 0 ) ; $ cartValueOption -> setVatPrice ( 0 ) ; if ( $ productOptionValue instanceof ProductOptionValue ) { $ cartValueOption -> setProductOptionValue ( $ productOptionValue ) ; $ cartValueOption -> setVat ( $ productOptionValue -> getVat ( ) ) ; if ( $ productOptionValue -> getPercentage ( ) > 0 ) { $ productOptionValuePrice = $ this -> getBasePrice ( ) -> multiply ( ( $ productOptionValue -> getPercentage ( ) / 100 ) ) ; $ cartValueOption -> setPrice ( $ productOptionValuePrice ) ; $ cartValueOption -> setVatPrice ( $ productOptionValuePrice -> multiply ( $ productOptionValue -> getVat ( ) -> getAsPercentage ( ) ) ) ; } else { $ cartValueOption -> setPrice ( $ productOptionValue -> getPrice ( ) ) ; $ cartValueOption -> setVatPrice ( $ productOptionValue -> getVatPrice ( ) ) ; } } } if ( $ productOption -> isSquareUnitType ( ) && $ product -> usesDimensions ( ) ) { $ square = ceil ( ( $ this -> getWidth ( ) / 100 ) * ( $ this -> getHeight ( ) / 100 ) ) ; $ cartValueOption -> setPrice ( $ cartValueOption -> getPrice ( ) -> multiply ( $ square ) ) ; $ cartValueOption -> setVatPrice ( $ cartValueOption -> getVatPrice ( ) -> multiply ( $ square ) ) ; } $ cartValue -> addCartValueOption ( $ cartValueOption ) ; if ( ! $ productOptionValue instanceof ProductOptionValue || $ productOptionValue -> isImpactTypeAdd ( ) ) { $ this -> addTotalPrice ( $ cartValueOption -> getPrice ( ) ) ; } else { $ this -> addTotalPrice ( $ cartValueOption -> getPrice ( ) * - 1 ) ; } } catch ( NonUniqueResultException $ e ) { continue ; } } } }
Add the product options to the cart.
Add the product options to the cart .
/**
                 * @var ProductOptionValue|string $productOptionValue
                 */ // Only do extra calculations based on square unit type // @TODO assumed unit is given in MM