/** * test for method afterPrepareAdjustmentConfig */ public function testAfterPrepareAdjustmentConfig() { $this->productMock->expects($this->once())->method('getTaxClassId')->will($this->returnValue('tax-class-id')); $this->calculationServiceMock->expects($this->once())->method('getDefaultCalculatedRate')->with('tax-class-id', 1)->will($this->returnValue(99.09999999999999)); $this->calculationServiceMock->expects($this->once())->method('getCalculatedRate')->with('tax-class-id', 1)->will($this->returnValue(99.09999999999999)); $this->productMock->expects($this->once())->method('getPriceInfo')->will($this->returnValue($this->priceInfoMock)); $this->priceInfoMock->expects($this->once())->method('getAdjustment')->with($this->equalTo(\Magento\Tax\Pricing\Adjustment::ADJUSTMENT_CODE))->will($this->returnValue($this->adjustmentMock)); $this->adjustmentMock->expects($this->once())->method('isIncludedInBasePrice')->will($this->returnValue(true)); $this->taxHelperMock->expects($this->once())->method('displayPriceIncludingTax')->will($this->returnValue(true)); $this->taxHelperMock->expects($this->once())->method('displayBothPrices')->will($this->returnValue(true)); $expected = ['product' => $this->productMock, 'defaultTax' => 99.09999999999999, 'currentTax' => 99.09999999999999, 'customerId' => 1, 'includeTax' => true, 'showIncludeTax' => true, 'showBothPrices' => true]; $this->assertEquals($expected, $this->plugin->afterPrepareAdjustmentConfig($this->attributePriceMock, ['product' => $this->productMock, 'defaultTax' => 0, 'currentTax' => 0, 'customerId' => 1])); }
/** * Get Tax Adjustments for configurable product * * @param \Magento\ConfigurableProduct\Pricing\Price\AttributePrice $attribute * @param array $result * @return array */ public function afterPrepareAdjustmentConfig(\Magento\ConfigurableProduct\Pricing\Price\AttributePrice $attribute, array $result) { $product = $result['product']; $productClassId = $product->getTaxClassId(); $defaultValue = $this->taxCalculationService->getDefaultCalculatedRate($productClassId, $result['customerId']); $result['defaultTax'] = $defaultValue + $result['defaultTax']; $currentTax = $this->taxCalculationService->getCalculatedRate($productClassId, $result['customerId']); $result['currentTax'] = $currentTax + $result['currentTax']; $adjustment = $product->getPriceInfo()->getAdjustment(\Magento\Tax\Pricing\Adjustment::ADJUSTMENT_CODE); $result['includeTax'] = $adjustment->isIncludedInBasePrice(); $result['showIncludeTax'] = $this->taxHelper->displayPriceIncludingTax(); $result['showBothPrices'] = $this->taxHelper->displayBothPrices(); return $result; }
/** * Get JSON encoded configuration array which can be used for JS dynamic * price calculation depending on product options * * @return string */ public function getJsonConfig() { $config = array(); if (!$this->hasOptions()) { return $this->_jsonEncoder->encode($config); } $customerId = $this->getCustomerId(); /* @var $product \Magento\Catalog\Model\Product */ $product = $this->getProduct(); $defaultTax = $this->taxCalculationService->getDefaultCalculatedRate($product->getTaxClassId(), $customerId); $currentTax = $this->taxCalculationService->getCalculatedRate($product->getTaxClassId(), $customerId); $tierPrices = array(); $tierPricesList = $product->getPriceInfo()->getPrice('tier_price')->getTierPriceList(); foreach ($tierPricesList as $tierPrice) { $tierPrices[] = $this->_coreData->currency($tierPrice['price']->getValue(), false, false); } $config = array('productId' => $product->getId(), 'priceFormat' => $this->_localeFormat->getPriceFormat(), 'includeTax' => $this->_taxData->priceIncludesTax() ? 'true' : 'false', 'showIncludeTax' => $this->_taxData->displayPriceIncludingTax(), 'showBothPrices' => $this->_taxData->displayBothPrices(), 'productPrice' => $this->_coreData->currency($product->getPriceInfo()->getPrice('final_price')->getValue(), false, false), 'productOldPrice' => $this->_coreData->currency($product->getPriceInfo()->getPrice('regular_price')->getAmount()->getValue(), false, false), 'inclTaxPrice' => $this->_coreData->currency($product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue(), false, false), 'exclTaxPrice' => $this->_coreData->currency($product->getPriceInfo()->getPrice('final_price')->getAmount()->getBaseAmount(), false, false), 'defaultTax' => $defaultTax, 'currentTax' => $currentTax, 'idSuffix' => '_clone', 'oldPlusDisposition' => 0, 'plusDisposition' => 0, 'plusDispositionTax' => 0, 'oldMinusDisposition' => 0, 'minusDisposition' => 0, 'tierPrices' => $tierPrices); $responseObject = new \Magento\Framework\Object(); $this->_eventManager->dispatch('catalog_product_view_config', array('response_object' => $responseObject)); if (is_array($responseObject->getAdditionalOptions())) { foreach ($responseObject->getAdditionalOptions() as $option => $value) { $config[$option] = $value; } } return $this->_jsonEncoder->encode($config); }
/** * Get all tax rates JSON for all product tax classes. * * @return string */ public function getAllRatesByProductClassJson() { $result = array(); foreach ($this->productTaxClassSource->getAllOptions() as $productTaxClass) { $taxClassId = $productTaxClass['value']; $taxRate = $this->calculationService->getDefaultCalculatedRate($taxClassId, $this->currentCustomer->getCustomerId(), $this->getStore()->getId()); $result["value_{$taxClassId}"] = $taxRate; } return $this->coreHelper->jsonEncode($result); }
/** * Get product price with all tax settings processing * * @param \Magento\Catalog\Model\Product $product * @param float $price inputted product price * @param bool $includingTax return price include tax flag * @param null|Address $shippingAddress * @param null|Address $billingAddress * @param null|int $ctc customer tax class * @param null|string|bool|int|Store $store * @param bool $priceIncludesTax flag what price parameter contain tax * @param bool $roundPrice * @return float */ public function getTaxPrice($product, $price, $includingTax = null, $shippingAddress = null, $billingAddress = null, $ctc = null, $store = null, $priceIncludesTax = null, $roundPrice = true) { if (!$price) { return $price; } $store = $this->_storeManager->getStore($store); if ($this->_taxConfig->needPriceConversion($store)) { if (is_null($priceIncludesTax)) { $priceIncludesTax = $this->_taxConfig->priceIncludesTax($store); } $shippingAddressDataObject = null; if ($shippingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) { $shippingAddressDataObject = $this->_addressConverter->createAddressFromModel($shippingAddress, null, null); } $billingAddressDataObject = null; if ($billingAddress instanceof \Magento\Customer\Model\Address\AbstractAddress) { $billingAddressDataObject = $this->_addressConverter->createAddressFromModel($billingAddress, null, null); } $item = $this->_quoteDetailsItemBuilder->setQuantity(1)->setCode($product->getSku())->setShortDescription($product->getShortDescription())->setTaxClassKey($this->_taxClassKeyBuilder->setType(TaxClassKey::TYPE_ID)->setValue($product->getTaxClassId())->create())->setTaxIncluded($priceIncludesTax)->setType('product')->setUnitPrice($price)->create(); $quoteDetails = $this->_quoteDetailsBuilder->setShippingAddress($shippingAddressDataObject)->setBillingAddress($billingAddressDataObject)->setCustomerTaxClassKey($this->_taxClassKeyBuilder->setType(TaxClassKey::TYPE_ID)->setValue($ctc)->create())->setItems([$item])->setCustomerId($this->_customerSession->getCustomerId())->create(); $storeId = null; if ($store) { $storeId = $store->getId(); } $taxDetails = $this->_taxCalculationService->calculateTax($quoteDetails, $storeId); $items = $taxDetails->getItems(); $taxDetailsItem = array_shift($items); if (!is_null($includingTax)) { if ($includingTax) { $price = $taxDetailsItem->getPriceInclTax(); } else { $price = $taxDetailsItem->getPrice(); } } else { switch ($this->_taxConfig->getPriceDisplayType($store)) { case Config::DISPLAY_TYPE_EXCLUDING_TAX: case Config::DISPLAY_TYPE_BOTH: $price = $taxDetailsItem->getPrice(); break; case Config::DISPLAY_TYPE_INCLUDING_TAX: $price = $taxDetailsItem->getPriceInclTax(); break; default: break; } } } if ($roundPrice) { return $store->roundPrice($price); } else { return $price; } }