/** * @inheritdoc */ public function getSelection(Struct\ProductContextInterface $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(' . $priceField . ($considerMinPurchase ? ' * availableVariant.minpurchase' : '') . ' * ((100 - IFNULL(priceGroup.discount, 0)) / 100)' . ($current->displayGrossPrices() ? " * (( " . $taxCase . " + 100) / 100)" : '') . ($discount ? " * " . (100 - (double) $discount) / 100 : '') . ($currency->getFactor() ? " * " . $currency->getFactor() : '') . ', 2)'; }
/** * Helper function which calculates a single price value. * The function subtracts the percentage customer group discount if * it should be considered and decides over the global state if the * price should be calculated gross or net. * The function is used for the original price value of a price struct * and the pseudo price of a price struct. * * @param $price * @param Struct\Tax $tax * @param Struct\ProductContextInterface $context * @return float */ private function calculatePrice($price, Struct\Tax $tax, Struct\ProductContextInterface $context) { /** * Important: * We have to use the current customer group of the current user * and not the customer group of the price. * * The price could be a price of the fallback customer group * but the discounts and gross calculation should be used from * the current customer group! */ $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()) { if ($customerGroup->getPercentageDiscount() != 0) { $price = $price - $price / 100 * $customerGroup->getPercentageDiscount(); } else { $price = 0; } } /** * Currency calculation: * If the customer is currently in a sub shop with another currency, like dollar, * we have to calculate the the price for the other currency. */ $price = $price * $context->getCurrency()->getFactor(); /** * check if the customer group should see gross prices. */ if (!$customerGroup->displayGrossPrices()) { return round($price, 3); } /** * Gross calculation: * * This line contains the gross price calculation within the store front. * * The passed $context object contains a calculated Struct\Tax object which * defines which tax rules should be used for the tax calculation. * * The tax rules can be defined individual for each customer group and * individual for each area, country and state. * * For example: * - The EK customer group has different configured HIGH-TAX rules. * - In area Europe, in country Germany the global tax value are set to 19% * - But in area Europe, in country Germany, in state Bayern, the tax value are set to 20% * - But in area Europe, in country Germany, in state Berlin, the tax value are set to 18% */ $price = $price * (100 + $tax->getTax()) / 100; return round($price, 3); }