How to calculate the tier prices in Magento

本文介绍Magento中批量定价功能的实现原理和技术细节,包括通过产品类型确定价格模型的方法及如何根据数量获取相应的价格。

Magento supports tier prices feature and it allows the actual price to be determined by the quantities. This is a common sale promotion technique used by lots of Magento store owners.

Mageno product model Mage_Catalog_Model_Product has two methods which are related to tire prices:

/**
     * Get product tier price by qty
     *
     * @param   double $qty
     * @return  double
     */
    public function getTierPrice($qty=null)
    {
        return $this->getPriceModel()->getTierPrice($qty, $this);
    }

    /**
     * Count how many tier prices we have for the product
     *
     * @return  int
     */
    public function getTierPriceCount()
    {
        return $this->getPriceModel()->getTierPriceCount($this);
    }
From above-mentioned methods, we know that product's tire prices is determined by price model. Magento uses price factory design pattern to cover all kinds of product types: simple, configurable,group, virtual, downloadable.

 public function getPriceModel()
    {
        return Mage::getSingleton('catalog/product_type')->priceFactory($this->getTypeId());
    }
class Mage_Catalog_Model_Product_Type is a helper or utility class which can get all the product types information from configuration file and database.

    public static function priceFactory($productType)
    {
        if (isset(self::$_priceModels[$productType])) {
            return self::$_priceModels[$productType];
        }

        $types = self::getTypes();

        if (!empty($types[$productType]['price_model'])) {
            $priceModelName = $types[$productType]['price_model'];
        } else {
            $priceModelName = self::DEFAULT_PRICE_MODEL;
        }

        self::$_priceModels[$productType] = Mage::getModel($priceModelName);
        return self::$_priceModels[$productType];
    }
<product>
	<type>
		<simple translate="label" module="catalog">
			<label>Simple Product</label>
			<model>catalog/product_type_simple</model>
			<composite>0</composite>
			<index_priority>10</index_priority>
		</simple>
		<grouped translate="label" module="catalog">
			<label>Grouped Product</label>
			<model>catalog/product_type_grouped</model>
			<price_model>catalog/product_type_grouped_price</price_model>
			<composite>1</composite>
			<allow_product_types>
				<simple/>
				<virtual/>
			</allow_product_types>
			<index_priority>50</index_priority>
			<price_indexer>catalog/product_indexer_price_grouped</price_indexer>
		</grouped>
		<configurable translate="label" module="catalog">
			<label>Configurable Product</label>
			<model>catalog/product_type_configurable</model>
			<price_model>catalog/product_type_configurable_price</price_model>
			<composite>1</composite>
			<allow_product_types>
				<simple/>
				<virtual/>
			</allow_product_types>
			<index_priority>30</index_priority>
			<price_indexer>catalog/product_indexer_price_configurable</price_indexer>
		</configurable>
		<virtual translate="label" module="catalog">
			<label>Virtual Product</label>
			<model>catalog/product_type_virtual</model>
			<composite>0</composite>
			<index_priority>20</index_priority>
		</virtual>
	</type>
</product>

For simplicity, i prefer to use simple type product's price model to show you around.  class Mage_Catalog_Model_Product_Type_Price is the simple type product's default price model.

    public function getTierPrice($qty = null, $product)
    {
        $allGroups = Mage_Customer_Model_Group::CUST_GROUP_ALL;
        $prices = $product->getData('tier_price');
        $custGroup = $this->_getCustomerGroupId($product);
        if ($qty) {
            $prevQty = 1;
            $prevPrice = $product->getPrice();
            $prevGroup = $allGroups;
            foreach ($prices as $price) {
                if ($price['cust_group']!=$custGroup && $price['cust_group']!=$allGroups) {                    
                    continue;
                }
                if ($qty < $price['price_qty']) {                   
                    continue;
                }
                if ($price['price_qty'] < $prevQty) {                   
                    continue;
                }
                if ($price['price_qty'] == $prevQty && $prevGroup != $allGroups && $price['cust_group'] == $allGroups) {
                    // found tier qty is same as current tier qty but current tier group is ALL_GROUPS
                    continue;
                }
                if ($price['website_price'] < $prevPrice) {
                    $prevPrice  = $price['website_price'];
                    $prevQty    = $price['price_qty'];
                    $prevGroup  = $price['cust_group'];
                }
            }
            return $prevPrice;
        }
        return ($prices) ? $prices : array();
    }
The logic is very apparently because it fetches the tire prices set up by admin panel and compares the quantity to get the corresponding price. 


In Python, calculating triangles in a multigraph (a graph that can have multiple edges between any two vertices) is a bit more complex than in a simple graph, since you need to account for all possible combinations of three nodes that are connected. Here's an outline using NetworkX library, which provides a convenient way to handle graphs: ```python import networkx as nx # Assuming you have a MultiGraph 'G' G = nx.MultiGraph() # Replace this line with your actual multigraph data def count_triangles(graph): triangle_count = 0 for node1, neighbors1 in graph.adjacency_iter(): # Iterate over nodes and their neighbors for node2 in neighbors1: # For each neighbor of node1 common_neighbors = set(graph.neighbors(node1)) & set(graph.neighbors(node2)) # Find common neighbors if len(common_neighbors) >= 2: # If at least two nodes share a neighbor triangle_count += len(common_neighbors) # Count the shared neighbors as potential triangles return triangle_count // 6 # Since each shared edge contributes to 6 triangles, divide by 6 for uniqueness triangle_count = count_triangles(G) print(f"The number of triangles in the multigraph is {triangle_count}.") ``` This code iterates over all pairs of neighboring nodes and checks if they share at least two common neighbors. It counts these shared neighbors and then divides the total by 6 because each unique triangle contributes three shared edges. Note that this method has a time complexity of O(V^3), where V is the number of vertices in the graph, so it may not be efficient for very large graphs.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值