Search code examples
apimagentointegrationcatalog

Display (tier) prices with qty increments and taxes


I need to display (tier) prices based on the qty increments of a product. E.g. a simple product, with a regular price of 50¢, no taxes and qty increments of 20 should be displayed on product views with "$10 per 20".

Without using taxes this should be quite easy. But there seems to be no "default" helper or model to do this with taxes enabled and different calulation algorithms (e.g. Mage_Tax_Model_Calculation::CALC_UNIT_BASE); expect for quotes in Mage_Tax_Model_Sales_Total_Quote_Tax and Mage_Tax_Model_Sales_Total_Quote_Subtotal.

Did I miss something here, or do I have to write the business logic to calculate the price on my own? And how I would best encapsulate it?


Solution

  • I solved my problem for now very simple. For this I used the <rewrite> element in a custom module to expand the helper Mage_Tax_Helper_Data with an additional instance method:

    class My_Module_Helper_Tax_Data extends Mage_Tax_Helper_Data {
    
        /**
         * Get product price based on stock quantity increments
         *
         * @param Mage_Catalog_Model_Product $product
         * @param float $price inputed product price
         * @param null|int $qtyIncrements inputed stock quantity increments
         * @param null|bool $includingTax return price include tax flag
         * @param null|Mage_Customer_Model_Address $shippingAddress
         * @param null|Mage_Customer_Model_Address $billingAddress
         * @param null|int $customerTaxClass customer tax class
         * @param mixed $store
         * @param bool $priceIncludesTax flag what price parameter contain tax
         * @return float
         */
        public function getQtyIncrementsPrice($product, $price, $qtyIncrements = 1,
            $includingTax = null, $shippingAddress = null, $billingAddress = null, 
            $customerTaxClass = null, $store = null, $priceIncludesTax = null) {
    
            $store = Mage::app()->getStore($store);
            $qtyIncrements *= 1;
    
            if ($this->_config->getAlgorithm($store) 
                == Mage_Tax_Model_Calculation::CALC_UNIT_BASE) {
                $price = $this->getPrice(
                    $product, $price, $includingTax, $shippingAddress, 
                    $billingAddress, $customerTaxClass, $store, $priceIncludesTax
                );
                $price *= $qtyIncrements;
                $price = $store->roundPrice($price);
            } else {
                $price *= $qtyIncrements;
                $price = $this->getPrice(
                    $product, $price, $includingTax, $shippingAddress,
                    $billingAddress, $customerTaxClass, $store, $priceIncludesTax
                );
            }
    
            return $price;
        }
    }
    

    It can be later used in a custom rewrite of methods such as Mage_Catalog_Block_Product_Price::getTierPrices().