How to rewrite a Magento Block with an extension? - magento

I want to rewrite the Mage_Catalog_Block_Product_View block.
I created this file: config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<JB_CustomPrice>
<version>0.1.0</version>
</JB_CustomPrice>
</modules>
<global>
<blocks>
<customprice>
<class>JB_CustomPrice_Block</class>
</customprice>
<catalog>
<rewrite>
<product_view>JB_CustomPrice_Block_Catalog_Product_View</product_view>
</rewrite>
</catalog>
</blocks>
</config>
Then I created the block in
/app/code/local/JB/CustomPrice/Block/Catalog/Product/View.php
<?php
class JB_CustomPrice_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
{
public function getJsonConfig()
{
return 'test';
}
}
?>
Though, the product view doesn't change, even not when I call the getProduct() method. What's going wrong?

The global error was a typo, solved by using Mage::log(get_class($this)), that fixed it. The problem was that another plugin extended Mage_Catalog_Block_Product_View already. It was an old Yoast_MetaRobots plugin, which is deprecated. I disabled it and everything was fixed. Thanks for the answer!

Related

Magento - How override admin Sales Totals block (Mage_Adminhtml_Block_Sales_Totals)

I am trying to override the admin Sales Totals block (Mage_Adminhtml_Block_Sales_Totals) situated under "/app/code/core/Mage/Adminhtml/Block/Sales/Totals.php"
I declare my module like this:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<CompanyName_Adminhtml>
<version>0.1.0</version>
</CompanyName_Adminhtml>
</modules>
<global>
<blocks>
<companyname_adminhtml>
<class>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</class>
</companyname_adminhtml>
<adminhtml>
<rewrite>
<sales_totals>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</sales_totals>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
My module class path is like this:
local/CompanyName_Adminhtml/Block/Adminhtml/Sales/Totals.php
and My class code start like this:
class CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals extends Mage_Adminhtml_Block_Sales_Totals {
// block methods goes here..
}
Unfortunately the class override don't works and I don't get any errors in the log files.
I am using Magento Version 1.7.0.2
In the magento backoffice I can see my module as enabled.
Any help?
Try Overriding the individual Totals.php block which is at the location Mage/AdminHtml/Block/Sales/Order/ file rather than the main Totals.php under the Mage/AdminHtml/Block/Sales/ folder.
It worked for me. As i wanted to show one more row in the order-totals block.
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<CompanyName_Adminhtml>
<version>0.1.0</version>
</CompanyName_Adminhtml>
</modules>
<global>
<blocks>
<companyName_adminhtml>
<class>CompanyName_Adminhtml_Block</class>
</companyName_adminhtml>
<companyname_adminhtml>
<class>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</class>
</companyname_adminhtml>
<adminhtml>
<rewrite>
<sales_order_totals>CompanyName_Adminhtml_Block_Adminhtml_Sales_Totals</sales_totals>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
then class
class CompanyName_Adminhtml_Block_Adminhtml_Sales_Order_Totalsextends Mage_Adminhtml_Block_Sales_Order_Totals {
protected function _initTotals() {
parent::_initTotals();
$this->_totals['foo_total'] = new Varien_Object(array(
'code' => 'purchasewrapping_total',
'value' => $this->getSource()->getFooTotal(),
'base_value'=> $this->getSource()->getBaseFooTotal(),
'label' => $this->helper('sales')->__('Foo'),
));
return $this;
}
}`enter code here`
Mage_Adminhtml_Block_Sales_Order_Totals is extends at Mage_Adminhtml_Block_Sales_Totals
and here you can extend and write your changes

Magento - Override/extend Ebizmarts_SagePaySuite_Model_Api_Payment model class

I am trying to override/extend the Ebizmarts SagePay Suite Ebizmarts_SagePaySuite_Model_Api_Payment model class
My module looks like this:
etc/modules/Sulman_ModifySagePay.xml
<?xml version="1.0"?>
<config>
<modules>
<Sulman_ModifySagePay>
<active>true</active>
<codePool>local</codePool>
<version>1.0</version>
<depends>
<Ebizmarts_SagePaySuite />
</depends>
</Sulman_ModifySagePay>
</modules>
</config>
app/code/local/Sulman/ModifySagePay/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Sulman_ModifySagePay>
<version>0.0.1</version>
</Sulman_ModifySagePay>
</modules>
<global>
<models>
<sagepaysuite>
<rewrite>
<api_payment>Sulman_ModifySagePay_Model_Api_Payment</api_payment>
</rewrite>
</sagepaysuite>
</models>
</global>
</config>
app/code/local/Sulman/ModifySagePay/Model/Api/Payment.php
<?php
class Sulman_ModifySagePay_Model_Api_Payment extends Ebizmarts_SagePaySuite_Model_Api_Payment {
public function _construct(){
Mage::log('in my construct()', null, 'Sulman.log');
}
public function requestPost($url, $data, $returnRaw = false) {
Mage::log('in my requestPost()', null, 'Sulman.log');
}
}
Nothing gets logged and this gives me errors on the checkout page because:
When I check to see if it is instantiated it returns false (if I disable my module it returns the Ebizmarts_SagePaySuite_Model_Api_Payment object):
var_dump(Mage::getModel('sagepaysuite/api_payment'));
Other calls to this model throughout the site also fail.
I'm a little confused as it seems my rewrite is working correctly but it can't seem to find my new model class.
Anyone have any ideas where I'm going wrong?
Thanks
In your app/code/local/Sulman/ModifySagePay/etc/config.xml in the <models></models> section you need to define where your models are located:
<models>
<modifysagepay>
<class>Sulman_ModifySagePay_Model</class>
<modifysagepay>
</models>

magento can not load my custom model

Hi I have a issue which I don't understand, please someone could help me.
I'm on the 1.8.1
I have a Module in app->code->local->Mycompany
Mycampany
TestModel
etc
config.xml
Model
FirstModel.php
code in config.xml
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_TestModel>
<version>0.1.0</version>
</Mycompany_TestModel>
</modules>
<global>
<models>
<TestModel>
<class>Mycompany_TestModel_Model</class>
</TestModel>
</models>
</global>
</config>
code in FirstModel.php
class Mycompany_TestModel_Model_FirstModel extends Mage_Core_Model_Abstract {
public function output()
{
echo "get";
}
}
When I use
Mage::getModel('TestModel/FirstModel')
magento can not load the class.
I tested and it is working on my local machine.
Please help.
Plus. I also tried:
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_TestModel>
<version>0.1.0</version>
</Mycompany_TestModel>
</modules>
<global>
<models>
<testmodel> /* instead of <TestModel> */
<class>Mycompany_TestModel_Model</class>
</testmodel>
</models>
</global>
</config>
I still can not get anything in
Mage::getModel('testmodel/FirstModel')
Thanks very much.
As you use magento fatory method [Mage::getModel('testmodel/FirstModel') ]
that means you need to use it procedure to define an model.
As per as magento, when autoloader execute testmodel/FirstModel run like:
testmodel=Mycompany_TestModel_Model
and
FirsModel is wrong ,File and name should be Firstmodel as after model model folder( Mycompany/TestModel/Model) all folders and files name should first letter with uppercase case and after that all should be lower a
That means file should be
Firstmodel.php instead of FirstModel
and testmodel/FirstModel should be testmodel/firstmodel
NO underscore should be use in folders and file name

Overriding Mage_Catalog_Block_Layer_View is a bug in magento?

I'm trying to override Mage_Catalog_Block_Layer_View in magento. I have tried to override the Mage_Catalog_Block_Layer_State and it is working properly, but the layer_view rewrite does not. Is this a bug?
<global>
<blocks>
<catalog>
<rewrite>
<layer_view>Mymodule_Catalog_Block_Layer_View</layer_view>
</rewrite>
</catalog>
</blocks>
</global>
<global>
<blocks>
<catalog>
<rewrite>
<layer_state>Mymodule_Catalog_Block_Layer_State</layer_state>
</rewrite>
</catalog>
</blocks>
</global>
please help me out.I'm trying it from hours
Best way to diagnose it is to verify the classname rewrite yourself. In ./demo.php:
<?php
ini_set('display_errors',true);
error_reporting(E_ALL | E_STRICT);
include 'app/Mage.php';
Mage::setIsDeveloperMode(true);
Mage::app();
$layer = Mage::getConfig()->getBlockClassName('catalog/layer_view');
var_dump($layer);
You should see your custom module block classname being returned rather than the core block classname. If that is the case then you need to step through and determine what the issue is. This block is highly coupled to several other block instances and can be a pain to work with.
Also, note that the Mage_CatalogSearch module has a layer_view block which extends from the Mage_Catalog layer_view block, which will not use your override.
Best Way to find which class is used by your .phtml is put echo get_class($this) in it.
Since I am using Enterprise edition, my layered navigation is not use the Mage_Catalog_Block_Layer_View. Instead it uses the Enterprise_Search_Block_Catalog_Block_Layer_View class.
When I override that class every thing was sorted out for me.
I have used below mentioned code:
in config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<PKR_Advmixfilter>
<version>0.1.0</version>
</PKR_Advmixfilter>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<layer_view>PKR_Advmixfilter_Block_Layer_View</layer_view>
</rewrite>
</catalog>
</blocks>
</global>
</config>
and created a block view file.
class PKR_Advmixfilter_Block_Layer_View extends Mage_Catalog_Block_Layer_View {
public function getFilters()
{
$filters = array();
if ($categoryFilter = $this->_getCategoryFilter()) {
$filters[] = $categoryFilter;
}
$filterableAttributes = $this->_getFilterableAttributes();
foreach ($filterableAttributes as $attribute) {
$filters[] = $this->getChild($attribute->getAttributeCode() . '_filter');
}
return $filters;
}
}
It is working at my end. So there is no bug. :)

Magento: Rewrite Block is not working

I try to rewrite core file from magento.
Somehow it does not overwrite the code. I try to overwrite the function getProduct().
Tipfix/Block/Product/View.php
<?php
class WP_Tipfix_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
{
public function getProduct()
{
if (!Mage::registry('product') && $this->getProductId()) {
$product = Mage::getModel('catalog/product')->load($this->getProductId());
Mage::register('product', $product);
}
//return Mage::registry('product');
}
}
Tipfix/etc/config.xml
<blocks>
<WP_Tipfix>
<class>WP_Tipfix_Block</class>
</WP_Tipfix>
<catalog>
<rewrite>
<product_view>WP_Tipfix_Block_Catalog_Product_View</product_view>
</rewrite>
</catalog>
</blocks>
I have know idea what i'm doing wrong.
Gr.
Lex
Your class is WP_Tipfix_Block_Catalog_Product_View which means it must be in the folder WP/Tipfix/Block/Catalog/Product/View.php. You must either move your Product directory into a new directory called Catalog in that place or rename your class (both the class and in the XML) to WP_Tipfix_Block_Product_View. I recommend moving the file.
Please change the config.xml content of your module to this, and I'm sure that it should work:-
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<WP_Tipfix>
<version>1.0.0</version>
</WP_Tipfix>
</modules>
<global>
<blocks>
<wptipfix>
<class>WP_Tipfix_Block</class>
</wptipfix>
<catalog>
<rewrite>
<product_view>WP_Tipfix_Block_Catalog_Product_View</product_view>
</rewrite>
</catalog>
</blocks>
</global>
</config>
Hope it helps.
UPDATE:-
After Ben's comment, I feel that I should have mentioned that the OP must also use the solution as mentioned by Max in his answer. So the OP will need a combined effort to fix his problem.

Resources