覆盖 Magento 类
我试图以这种方式用 MyCompany_Mymodule_Block_View 覆盖 Mage_Catalog_Block_Product_View:
<?php
class MyCompany_Mymodule_Block_View extends Mage_Catalog_Block_Product_View {
/**
* Add meta information from product to head block
*
* @see Mage_Catalog_Block_Product_View::_prepareLayout()
* @return Mage_Catalog_Block_Product_View
*/
protected function _prepareLayout() {
$product = $this->getProduct();
$logged_in = Mage::getSingleton( 'customer/session' )->isLoggedIn();
$groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
if($this->checkPrivateCategories($product->getCategoryIds())){
if($logged_in && $groupId == 1){
# die('The user can see the product');
}else{
header('location: /customer/account/login');
die;
}
}
return parent::_prepareLayout();
}
private function checkPrivateCategories($categories){
if(is_array($categories)){
foreach($categories as $category){
$collection = Mage::getModel('catalog/category')->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$collection
->addAttributeToFilter('private', true)
->addIdFilter(array($category))
->load();
$data = $collection->getData();
if(!empty($data)){
if ($data[0]['private']){
return true ;
}
}
}
}
return false;
}
}
我想检查所选产品是否属于特定类别,如果答案是肯定的,我会将用户重定向到客户登录页面。
现在,如果我直接编辑核心类,代码就可以正常工作。如果我尝试使用自定义类覆盖 Mage_Catalog_Block_Product_View,我会得到产品详细信息页面的空白部分。该类已被正确调用,但没有 html 答案。
这是 Config.xml 摘录:
<blocks>
<mymodule>
<class>MyCompany_Mymodule_Block</class>
</mymodule>
<catalog>
<rewrite>
<product_view>MyCompany_Mymodule_Block_View</product_view>
</rewrite>
</catalog>
</blocks>
有人可以帮助我发现这个谜团吗?
I am trying to override the Mage_Catalog_Block_Product_View with my MyCompany_Mymodule_Block_View in this way:
<?php
class MyCompany_Mymodule_Block_View extends Mage_Catalog_Block_Product_View {
/**
* Add meta information from product to head block
*
* @see Mage_Catalog_Block_Product_View::_prepareLayout()
* @return Mage_Catalog_Block_Product_View
*/
protected function _prepareLayout() {
$product = $this->getProduct();
$logged_in = Mage::getSingleton( 'customer/session' )->isLoggedIn();
$groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
if($this->checkPrivateCategories($product->getCategoryIds())){
if($logged_in && $groupId == 1){
# die('The user can see the product');
}else{
header('location: /customer/account/login');
die;
}
}
return parent::_prepareLayout();
}
private function checkPrivateCategories($categories){
if(is_array($categories)){
foreach($categories as $category){
$collection = Mage::getModel('catalog/category')->getCollection();
/* @var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$collection
->addAttributeToFilter('private', true)
->addIdFilter(array($category))
->load();
$data = $collection->getData();
if(!empty($data)){
if ($data[0]['private']){
return true ;
}
}
}
}
return false;
}
}
I would like to check if the product selected is within a particular category, if the answer is yes I redirect the user to the customer login page.
Now, if I edit the core class directly the code works correctly. If I try to override the Mage_Catalog_Block_Product_View with my custom class I get an empty section of the product detail page. The class has been called correctly, but there is no html answer.
This is the Config.xml extract:
<blocks>
<mymodule>
<class>MyCompany_Mymodule_Block</class>
</mymodule>
<catalog>
<rewrite>
<product_view>MyCompany_Mymodule_Block_View</product_view>
</rewrite>
</catalog>
</blocks>
Does anybody can help me to discover this mistery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从技术上讲,您不应该将这种控制逻辑放入 Block 类中。如果您遵循 MVC 模式,那么最好查看控制器并检查那里的类别。
幸运的是,您甚至不需要这样做,因为有一个免费扩展已经可以做到这一点。
Technically you shouldn't be putting this sort of control logic in a Block class. If you follow the MVC pattern then it is better to watch a controller instead and check the category there.
Luckily you don't even need to do that because there is a free extension which already does.
您好,很抱歉无法完全看出出了什么问题。但请确保错误报告已打开。您还尝试注释掉一些代码并查看它是否仍然有效。
关于您的代码和方法的一些事情也很少。您尝试实现的逻辑应该位于控制器而不是视图中。控制器还触发了相当多的事件。也许有一款适合您的要求。
块类 Mage_Catalog_Block_Product_View 又扩展了几个类。而且您不想每次加载任何模板时都运行检查,特别是因为您正在查询数据库。
另外, checkPrivateCategories 方法似乎采用一组类别 id,然后使用 foreach 循环在数据库中查询每个类别 id,使用集合来查找一个实体。您应该使用一个集合查询数据库一次,然后检查所有结果。
检查 $product->getCategoryCollection()
即使这可能不是您正在寻找的答案,我希望它会有所帮助
Hi I am sorry can't not exactly see what is going wrong. But make sure error reporting is on. Also did you try commenting out some of your code and see if it still works.
Also few things on your code and your approach. The logic you try to implement should be within a controller instead of the view. Also the controller trigger's quite a few events. Maybe there is one that suits your requirements.
The block class Mage_Catalog_Block_Product_View gets extended by a few more classes. And you don't want to run your check everytime any template gets loaded, espacially since you are querying the database.
Also it looks like the method checkPrivateCategories takes an array of category ids, then uses a foreach loop to query the database for each category id using a collection to find one entity. You should instead query the database once with one collection and then check the result for all.
check $product->getCategoryCollection()
Even so that is probably not the answer you were looking for, I hope it will help