我正在尝试扩展Mage_Catalog_Block_Product_View,我已经将它设置在我本地目录中的版本中,一切正常,但我没有得到我想要的结果。然后我看到另一个类也扩展了那个类。这似乎重置了我的课上设置的所有内容。我知道我的类工作得很好,并且正在执行。只是有另一个类是我正在扩展的同一个类的子类,并将parent::method调用到我要重写的同一个方法,这个类在我的类之后执行,当它执行时,它运行的是我重写的原始方法,而不是我的方法
这就是函数
class Mage_Review_Block_Product_View extends Mage_Catalog_Block_Product_View
protected function _prepareLayout()
{
$this->getLayout()->createBlock('catalog/breadcrumbs');
$headBlock = $this->getLayout()->getBlock('head');
if ($headBlock) {
$title = $this->getProduct()->getMetaTitle();
if ($title) {
$headBlock->setTitle($title);
}
$keyword = $this->getProduct()->getMetaKeyword();
$currentCategory = Mage::registry('current_category');
if ($keyword) {
$headBlock->setKeywords($keyword);
} elseif($currentCategory) {
$headBlock->setKeywords($this->getProduct()->getName());
}
$description = $this->getProduct()->getMetaDescription();
if ($description) {
$headBlock->setDescription( ($description) );
} else {
$headBlock->setDescription( $this->getProduct()->getDescription() );
}
}
return parent::_prepareLayout();
}我正在尝试修改它只有一点与以下,请记住,我知道有一个标题前缀和后缀,但我需要它只为产品页面,我还需要添加文字的描述。
class MyCompany_Catalog_Block_Product_View extends Mage_Catalog_Block_Product_View
protected function _prepareLayout()
{
$storeId = Mage::app()->getStore()->getId();
$this->getLayout()->createBlock('catalog/breadcrumbs');
$headBlock = $this->getLayout()->getBlock('head');
if ($headBlock) {
$title = $this->getProduct()->getMetaTitle();
if ($title) {
if($storeId == 2){
$title = "Pool Supplies Fast - " .$title;
$headBlock->setTitle($title);
}
$headBlock->setTitle($title);
}else{
$path = Mage::helper('catalog')->getBreadcrumbPath();
foreach ($path as $name => $breadcrumb) {
$title[] = $breadcrumb['label'];
}
$newTitle = "Pool Supplies Fast - " . join($this->getTitleSeparator(), array_reverse($title));
$headBlock->setTitle($newTitle);
}
$keyword = $this->getProduct()->getMetaKeyword();
$currentCategory = Mage::registry('current_category');
if ($keyword) {
$headBlock->setKeywords($keyword);
} elseif($currentCategory) {
$headBlock->setKeywords($this->getProduct()->getName());
}
$description = $this->getProduct()->getMetaDescription();
if ($description) {
if($storeId == 2){
$description = "Pool Supplies Fast - ". $this->getProduct()->getName() . " - " . $description;
$headBlock->setDescription( ($description) );
}else{
$headBlock->setDescription( ($description) );
}
} else {
if($storeId == 2){
$description = "Pool Supplies Fast: ". $this->getProduct()->getName() . " - " . $this->getProduct()->getDescription();
$headBlock->setDescription( ($description) );
}else{
$headBlock->setDescription( $this->getProduct()->getDescription() );
}
}
}
return Mage_Catalog_Block_Product_Abstract::_prepareLayout();
}这可以很好地执行,但是我注意到下面的Mage_Review_Block_Product_View_List扩展类,它扩展了Mage_Review_Block_Product_View,也扩展了Mage_Catalog_Block_Product_View。在这个类内部,它们也调用_prepareLayout,并用parent::_prepareLayout()调用父类
class Mage_Review_Block_Product_View_List extends Mage_Review_Block_Product_View
protected function _prepareLayout()
{
parent::_prepareLayout();
if ($toolbar = $this->getLayout()->getBlock('product_review_list.toolbar')) {
$toolbar->setCollection($this->getReviewsCollection());
$this->setChild('toolbar', $toolbar);
}
return $this;
}显然,这只是调用了我正在扩展的同一个类,并运行了我重写的函数,但它没有到达我的类,因为它不在我的类层次结构中,而且由于它是在我的类之后调用的,所以父类中的所有内容都覆盖了我设置的内容。
我不确定扩展这种类型的类和方法的最佳方式,必须有一个好的方法来做到这一点,当我试图覆盖这些从抽象类派生的准备方法时,我一直发现我遇到了问题,似乎有太多的类覆盖它们并调用parent::method。
覆盖这些函数的最佳方式是什么?
发布于 2010-04-22 20:20:27
这里有三个选项:
app/code/local/文件夹中,以便Magento找到它而不是默认的父类。因此,您需要创建目录app/code/local/Mage/Catalog/Block/Product/并将View.php复制到其中。然而,这对于升级来说是个问题,因为你必须合并Varien对类所做的任何更改。希望这会有帮助,乔
https://stackoverflow.com/questions/2690311
复制相似问题