作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
谁能阐明“getTypeInstance()”方法的必要性,它可以被任何产品对象使用?
还有使用这种方法的优缺点是什么?
最佳答案
getTypeInstance 允许您检索描述产品类型的对象,其中类型是内部 magento 类型。因此,您可以使用此方法来确定产品是简单产品、捆绑产品、可配置产品等。
然后,您可以使用这些对象来确定特定于产品类型的产品信息。例如,如果您在捆绑产品对象上调用此方法,您将获得一个类为
的对象Mage_Bundle_Model_Product_Type
这个类上面有很多方法,专门用来处理捆绑商品的。例如,您有 getWeight
方法
public function getWeight($product = null)
{
if ($this->getProduct($product)->getData('weight_type')) {
return $this->getProduct($product)->getData('weight');
} else {
$weight = 0;
if ($this->getProduct($product)->hasCustomOptions()) {
$customOption = $this->getProduct($product)->getCustomOption('bundle_selection_ids');
$selectionIds = unserialize($customOption->getValue());
$selections = $this->getSelectionsByIds($selectionIds, $product);
foreach ($selections->getItems() as $selection) {
$weight += $selection->getWeight();
}
}
return $weight;
}
}
此方法具有确定捆绑产品重量的特定规则。
然后,在 catalog/product
模型 (Mage_Catalog_Model_Product
) 中,您可以看到 getWeight
只是包装了对该类型的 获取权重
public function getWeight()
{
return $this->getTypeInstance(true)->getWeight($this);
}
这是面向对象编程的一个典型例子。
那么,一天结束了吗?如果您不知道为什么需要使用此方法,则不需要使用此方法。
关于php - 在 Magento 中使用产品的 "getTypeInstance()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3138957/
谁能阐明“getTypeInstance()”方法的必要性,它可以被任何产品对象使用? 还有使用这种方法的优缺点是什么? 最佳答案 getTypeInstance 允许您检索描述产品类型的对象,其中类
我是一名优秀的程序员,十分优秀!