我想扩展Mage_Catalog_Block_Product_View我已经在我的本地版本中设置了它,并且一切工作正常,但我没有得到我想要的结果。然后,我看到另一个班级也扩展了这个班级。这似乎重置了我班的任何设置。我知道我的班级工作正常并正在执行。它只是有另一个类是我正在扩展的同一个类的子类,并调用parent ::方法为我重写的同一个方法,该类在我的类之后执行,当它执行时,它正在运行原始方法,我重写,与我无关你如何覆盖由父类::方法调用另一个类的类
这是函数
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();
}
这executs很好,但后来我发现下面的类Mage_Review_Block_Product_View_List扩展延伸Mage_Review_Block_Product_View和延伸Mage_Catalog_Block_Product_View为好。这个类中,他们称之为_prepareLayout以及和调用父类与父母:: _ 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;
}
所以,很显然这只是叫我延伸的同一类和运行我overiding的功能,但它不去我的课,因为它不在我的类层次结构中,并且因为它在我的课程后被调用,所以父类中的所有东西都会覆盖我所设置的内容。
我不确定扩展这种类型和方法的最佳方式,必须有一个很好的方法来做到这一点,我试图覆盖这些准备方法时遇到问题派生自抽象类,似乎有这么多的类覆盖它们并调用parent ::方法。
覆盖这些功能的最佳方法是什么?
谢谢艾伦,我明白你必须重写在链的底部的子类,所以我想这意味着,而不是扩展Mage_Catalog_Block_Product_View我会扩展Mage_Review_Block_Product_View_List它似乎很奇怪,因为它是在评论。是否有一种简单的方法来查看应该扩展的类是什么类? 当你说“但它意味着你可能只能覆盖类的实例化”,你只是指最后一个调用该类的类。顺便说一句,我从你的文章中学到了我的Magento开发的大部分内容 – 2010-04-22 14:12:21
更新了我正在驾驶的内容,这可能会也可能不会解决你之后的问题。很高兴你喜欢这些有用的文章! – 2010-04-22 14:53:34
ahh是啊,那就是我认为你的意思,所以我想唯一的选择是扩展Foo,即使它没有意义 – 2010-04-22 17:15:25