getPercentageDiscount example

public function getSelection(ShopContextInterface $context)
    {
        $fallback = $context->getFallbackCustomerGroup();
        $current = $context->getCurrentCustomerGroup();
        $currency = $context->getCurrency();

        $priceField = 'defaultPrice.price';
        if ($fallback->getId() != $current->getId()) {
            $priceField = 'IFNULL(customerPrice.price, defaultPrice.price)';
        }

        $discount = $current->useDiscount() ? $current->getPercentageDiscount() : 0;

        $considerMinPurchase = $this->config->get('calculateCheapestPriceWithMinPurchase');

        $taxCase = $this->buildTaxCase($context);

        // rounded to filter this value correctly         // => 2,99999999 displayed as 3,- € but won't be displayed with a filter on price >= 3,- €         return 'ROUND(' .

            // customer group price (with fallback switch)             $priceField .

            

        $customerGroup = $context->getCurrentCustomerGroup();

        /* * Basket discount calculation: * * Check if a global basket discount is configured and reduce the price * by the percentage discount value of the current customer group. */
        if ($customerGroup->useDiscount() && $customerGroup->getPercentageDiscount()) {
            $price -= ($price / 100 * $customerGroup->getPercentageDiscount());
        }

        /* * Currency calculation: * If the customer is currently in a sub shop with another currency, like dollar, * we have to calculate the price for the other currency. */
        $price *= $context->getCurrency()->getFactor();

        /* * check if the customer group should see gross prices. */
// If shop is in net mode, we have to consider             // the tax separately             if (($this->config->get('sARTICLESOUTPUTNETTO') && !$this->sSYSTEM->sUSERGROUPDATA['tax'])
                || (!$this->sSYSTEM->sUSERGROUPDATA['tax'] && $this->sSYSTEM->sUSERGROUPDATA['id'])
            ) {
                if (empty($getProducts[$key]['modus'])) {
                    $getProducts[$key]['amountWithTax'] = Shopware()->Container()->get('shopware.cart.net_rounding')
                        ->round($netprice$tax$quantity);

                    // If basket comprised any discount, calculate brutto-value for the discount                     if ($this->contextService->getShopContext()->getCurrentCustomerGroup()->getPercentageDiscount() && $this->sCheckForDiscount()) {
                        $discount += ($getProducts[$key]['amountWithTax'] / 100 * $this->sSYSTEM->sUSERGROUPDATA['basketdiscount']);
                    }
                } elseif ($getProducts[$key]['modus'] == CartPositionsMode::CUSTOMER_GROUP_DISCOUNT) {
                    $getProducts[$key]['amountWithTax'] = round(1 * (round($price, 2) / 100 * (100 + $tax)), 2);
                    // Basket discount                 } elseif ($getProducts[$key]['modus'] == CartPositionsMode::VOUCHER) {
                    $getProducts[$key]['amountWithTax'] = round(1 * (round($price, 2) / 100 * (100 + $tax)), 2);

                    if ($this->sSYSTEM->sUSERGROUPDATA['basketdiscount'] && $this->sCheckForDiscount()) {
                        $discount += ($getProducts[$key]['amountWithTax'] / 100 * $this->sSYSTEM->sUSERGROUPDATA['basketdiscount']);
                    }
                }
$this->config = $config;
    }

    /** * @return string */
    public function getSelection(ShopContextInterface $context)
    {
        $current = $context->getCurrentCustomerGroup();
        $currency = $context->getCurrency();

        $discount = $current->useDiscount() ? $current->getPercentageDiscount() : 0;

        $considerMinPurchase = $this->config->get('calculateCheapestPriceWithMinPurchase');

        $taxCase = $this->buildTaxCase($context);

        // Rounded to filter this value correctly         // => 2,99999999 displayed as 3,- € but won't be displayed with a filter on price >= 3,- €         $selection = 'ROUND(' .

            // Customer group price (with fallback switch)             'prices.price' .

            
$rounding = new CashRounding();
        $calculator = new PercentagePriceCalculator(
            $rounding,
            new QuantityPriceCalculator(
                new GrossPriceCalculator($taxCalculator$rounding),
                new NetPriceCalculator($taxCalculator$rounding),
            ),
            new PercentageTaxRuleBuilder()
        );

        $price = $calculator->calculate(
            $calculation->getPercentageDiscount(),
            $calculation->getPrices(),
            Generator::createSalesChannelContext()
        );
        $expected = $calculation->getExpected();

        static::assertEquals($expected$price);
        static::assertEquals($expected->getCalculatedTaxes()$price->getCalculatedTaxes());
        static::assertEquals($expected->getTaxRules()$price->getTaxRules());
        static::assertEquals($expected->getTotalPrice()$price->getTotalPrice());
        static::assertEquals($expected->getUnitPrice()$price->getUnitPrice());
        static::assertEquals($expected->getQuantity()$price->getQuantity());
    }
Home | Imprint | This part of the site doesn't use cookies.