Magento helper methods do not work - magento

So this one is a bit odd. I'm sure it's old hat to some now, and I've just done a bad job searching it out. If that's the case I certainly apologize and ask for your indulgence and a link to the already provided answer.
So I have followed the core guides for creating a magento module, but for some reason it does work right. When I var_dump the helper class it has the correct name, but when I try to use one of the methods I've put in the class definition it throws an exception.
Furthermore, when I run get_class_methods() on the class given to me by Mage::helper('mymodule/myhelper') none of my methods are there.
So in summary:
The class is in the right place, app/code/local/MyModule/Helper/Myhelper.php
The class shows as initialized after I call Mage::helper('mymodule/myhelper')
The class methods are not listed, and as a result, break things when called.
Here's my config for reference. The rest of the module seems to be fine.
<?xml version="1.0"?>
<config>
<modules>
<APCShared_Shipping>
<version>0.1.0</version>
</APCShared_Shipping>
</modules>
<frontend>
<routers>
<apcshipping>
<use>standard</use>
<args>
<module>APCShared_Shipping</module>
<frontName>apcshipping</frontName>
</args>
</apcshipping>
</routers>
</frontend>
<global>
<blocks>
<apcshipping>
<class>APCShared_Shipping_Block</class>
</apcshipping>
</blocks>
<helpers>
<apcshipping>
<class>APCShared_Shipping_Helper</class>
</apcshipping>
</helpers>
<models>
<apcshipping>
<class>APCShared_Shipping_Model</class>
<resourceModel>shipping_resource</resourceModel>
</apcshipping>
<apcshipping_resource>
<class>APCShared_Shipping_Model_Resource</class>
<entities>
<zipcode>
<table>apc_shipping_zipcode</table>
</zipcode>
</entities>
</apcshipping_resource>
</models>
</global>
</config>

In the admin somewhere under System > Configuration > Advanced ensure your module has been registered.``
Make sure you have the class APCShared_Shipping_Helper_Data in the Helpers folder in a file called Data.php. Your class should extend Mage_Core_Helper_Abstract
Access your helper with Mage::helper('apcshipping')
Your directory structure should be something like:
- Module Folder
- etc
- config.xml
- Helper
- Data.php

In your file structure, point 1 of your summary, you are only using a modulename, not a packagename.
So it should be: app/code/local/MyCompany/MyModule/Helper/Myhelper.php instead of app/code/local/MyModule/Helper/Myhelper.php.
I'm not sure if this will make a difference, but it's always good to follow the coding convention.

Related

How do I create a Magento Helper when overriding an existing module router

I have overridden the 'customer' frontname in order to make my custom module pages display as /customer/my-page-action/. When I do that my Helper class cannot be found:
Warning: include(Mage/FranchiseSelect/Helper/Data.php): failed to open stream: No such file or directory
Here's my extension config:
<?xml version="1.0" encoding="utf-8" ?>
<config>
<modules>
<Rhino_FranchiseSelect>
<active>true</active>
<codePool>local</codePool>
</Rhino_FranchiseSelect>
</modules>
</config>
Here's my module config:
<?xml version="1.0" encoding="utf-8" ?>
<config>
<modules>
<Rhino_FranchiseSelect>
<version>0.1.0</version>
</Rhino_FranchiseSelect>
</modules>
<frontend>
<routers>
<customer>
<args>
<modules>
<franchise before="Mage_Customer">Rhino_FranchiseSelect</franchise>
</modules>
</args>
</customer>
</routers>
<events>
<customer_session_init>
<observers>
<sessioninit_handler>
<type>singleton</type>
<class>Rhino_FranchiseSelect_Model_Observer</class>
<method>redirectToFranchise</method>
</sessioninit_handler>
</observers>
</customer_session_init>
<controller_action_predispatch_customer_account_create>
<observers>
<handler>
<type>singleton</type>
<class>Rhino_FranchiseSelect_Model_Observer</class>
<method>checkForRegion</method>
</handler>
</observers>
</controller_action_predispatch_customer_account_create>
</events>
</frontend>
<global>
<resources>
<helpers>
<what_should_this_tag_name_be>
<class>Rhino_FranchiseSelect_Helper</class>
</what_should_this_tag_name_be>
</helpers>
</resources>
</global>
</config>
My helper is located here:
/app/code/local/Rhino/FranchiseSelect/Helper/Data.php
I'm trying to instantiate it like so:
$helper = Mage::helper("what-should-this-path-be");
I'm not sure what the config helper alias tag name or helper path name should be in order to get this to work. Can you help me identify how this should be structured?
I'm not 100% sure it's the additional router configuration that's triggering this error — off the top of my head there's nothing in the controller dispatch process that instantiates a helper.
The most common reason for Magento to automatically instantiate a helper is a modules="helperalias" attribute in a configuration file. This indirectly invokes a helper's __ localization method.
Without knowing why your Magento system wants to instantiate a helper, it's impossible to know what the alias should be. If I was you I'd temporarily add something debugging code to the following file
#File: app/Mage.php
public static function helper($name)
{
//poor man's logging
file_put_contents('/tmp/test.log',"$name\n",FILE_APPEND);
// PHP's equivalent of printf debugging
var_dump($name);
$registryKey = '_helper/' . $name;
if (!self::registry($registryKey)) {
$helperClass = self::getConfig()->getHelperClassName($name);
self::register($registryKey, new $helperClass);
}
return self::registry($registryKey);
}
This will tell you what the helper's alias is, which is turn is what you should name the configuration node you're looking for.
Update: Based on the comment below, you want
<global>
<helpers>
<franchiseselect>
<class>Rhino_FranchiseSelect_Helper</class>
</franchiseselect>
</helpers>
</global>
When you say
Mage::helper('franchiseselect');
What you're really saying is
Mage::helper('franchiseselect/data');
Magento substitutes the data string automatically. The franchiseselect string is the alias group name, the data string is the alias model name. When you add the node <franchiseselect>/<what_should_this_tag_name_be/> always to your XML you're defining the alias group name.
Also, your XML had an extra node, <resources/>, under the <helpers/> node — you don't need that.
As for why the Magento looked for a class under Mage — if Magento can't find a configured alias, it automatically guesses that the request was for a class under the Mage namespace. This guess is a bit of paranoid programming on the part of the original core team to ensure any misconfigurations in their own modules didn't trigger errors. When Magento couldn't find your framework helper, it tried instantiating a Mage_Framework... class.

Extending Mage Core Files Without Loosing Namespace

I would like to introduce my own entities into particular Magento module namespaces for example I might want to be able to call
Mage::getModel('catalog/brand')->load(1);
Brand is not currently a model included in the catalog module. I don't want to modify core files nor do I want to hack the core by just adding a Mage folder to the local directory.
I was thinking perhaps syntax inside of my namespaces config file similar to this:
<models>
<catalog>
<args>
<modules>
<AJW_Catalog before="Mage_Catalog">AJW_Catalog</AJW_Catalog>
</modules>
</args>
</catalog>
<ajw_catalog>
<class>AJW_Catalog_Model</class>
</ajw_catalog>
</models>
but it does not seem to work.
Does anyone know how this can be accomplished?
Maybe possible with some trickery, but not officially supported, and generally a bad idea. The before= syntax you've used only works for the routers node. There's no framework code to let you do what you're trying to do. Also, there's a strong bias in the Magento framework code towards individual modules "owning" their namespace/package name. Defining new models in an existing namespace (catalog) introduces the theoretical possibility that your code may conflicts with a future version of Magento's code.
This may be a possible fix (brain fart)
Create a module named Customnamespace_Catalog and then just rewrite the catalog module with a node that doesnt exist in the default mage module:
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Catalog>
<version>0.1.0</version>
</Namespace_Catalog>
</modules>
<global>
<models>
<catalog>
<rewrite>
<brand>Namespace_Brand_Model_Brand</brand>
</rewrite>
</catalog>
</models>
</global>
</config>
Followed by an additional module:
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Brand>
<version>0.1.0</version>
</Namespace_Brand>
</modules>
<global>
<models>
<brand>
<class>Namespace_Brand_Model</class>
</brand>
</models>
</global>
</config>
This will allow you to call Mage::getModel('catalog/brand')
echo get_class(Mage::getModel('catalog/brand'); // Namespace_Brand_Model_Brand

Overriding Magento Adminhtml Controller in app/code/local Folder

I would like to override the Magento Admin core controller in app/code/local folder.
I need to override the app/code/core/Mage/Adminhtml/controllers/CustomerController.php
in app/code/local/Mage/Adminhtml/controllers/CustomerController.php path.
i have copied the core files and created the same path in local folder.
Still the files are not loading from local folder, it only loading from core files.
I have cleared the Magento cache and set the File permission(777) for all folder in app/code/local directory.
Can any one suggest me the solution for this issues?
Any help much appreciated.
Many Thanks
Magento controllers are not autoloaded the same as other classes (blocks, models etc.), if you wish to overload it, your best bet is to configure your own controller to get checked before it in the routing.
Let me show you how i have overridden customer controller
in app/code/local/'your company'/'your module name'/etc/config.xml
enable custom module
<config>
<modules>
<company_module>
<active>true</active>
<codePool>local</codePool>
</company_module>
</modules>
</config>
Route custom module path
<frontend>
<routers>
<company_module>
<use>standard</use>
<args>
<module>company_module</module>
<frontName>customer</frontName>
</args>
</company_module>
</routers>
</frontend>
rewrite class
<global>
<rewrite>
<company_module_address>
<from><![CDATA[#^/address/#]]></from>
<to>/customer/address/</to>
</company_module_address>
</rewrite>
</global>
at app/code/local/company/module/controllers/AddressController.php
<?php
require_once 'Mage/Customer/controllers/AddressController.php';
class Company_Module_AddressController extends Mage_Customer_AddressController
{
//Show your magic here
}
Hope this helps... Thanks

Magento controller override

I am trying to overwrite a controller of an extension....that is overwriting the cart controller.
The extension currently overwriting the cart controller is:
Innoexts/Warehouse/controllers/Checkout/CartController.php
The config entry in the Innoexts module doing this is:
<frontend>
<routers>
<checkout>
<args>
<modules>
<Innoexts_Warehouse before="Mage_Checkout">Innoexts_Warehouse_Checkout</Innoexts_Warehouse>
</modules>
</args>
</checkout>
</routers>
...blah...blah...
</frontend>
The top of the innoext cartcontroller file is:
require_once 'Mage/Checkout/controllers/CartController.php';
class Innoexts_Warehouse_Checkout_CartController extends Mage_Checkout_CartController {
I want to overwrite it with this controller:
Myco/Warehousemod/controllers/Checkout/CartController.php
The top of the controller file is:
require_once 'Innoexts/Warehouse/controllers/Checkout/CartController.php';
class Myco_Warehousemod_Checkout_CartController extends Innoexts_Warehouse_Checkout_CartController {
The config entry ive created is:
<global>
...blah...blah...
<rewrite>
<myco_warehousemod_checkout_cart>
<from><![CDATA[#^/checkout/cart/#]]></from>
<to>/warehousemod/checkout_cart/</to>
</myco_warehousemod_checkout_cart>
</rewrite>
</global>
<frontend>
<routers>
<checkout>
<args>
<modules>
<Myco_Warehousemod before="Innoexts_Warehouse_Checkout">Myco_Warehousemod_Checkout</Myco_Warehousemod>
</modules>
</args>
</checkout>
</routers>
...blah...blah...
</frontend>
I am getting a 404 not found error for the checkout/cart URL now....Can anyone see where im going wrong? Online resources are very different...and confusing!! The issue may be with me trying to overwrite an overwriting controller...??
thanks in advance...
Need to remove the first rewrite:
<rewrite>
<myco_warehousemod_checkout_cart>
<from><![CDATA[#^/checkout/cart/#]]></from>
<to>/warehousemod/checkout_cart/</to>
</myco_warehousemod_checkout_cart>
</rewrite>
I think certain people should stop writing tutorials....cluttering up the web with their half-assed SEO efforts....
This part was used in the old versions of Magento (i think before 1.4), but if you want to extend a controller that has a rewrite like this in the config file, you have to do the same in your config.
<rewrite>
<myco_warehousemod_checkout_cart>
<from><![CDATA[#^/checkout/cart/#]]></from>
<to>/warehousemod/checkout_cart/</to>
</myco_warehousemod_checkout_cart>
</rewrite>
The new versions use only the part with before so you should have something like this:
<routers>
<checkout>
<args>
<modules>
<Myco_Warehousemod before="Innoexts_Warehouse">Myco_Warehousemod_Checkout</Myco_Warehousemod><!-- just the name of the module Innoexts_Warehouse the overridden folder will be taken from the part after the name of your module so Magento will look in app/local/Myco/Warehousemod/controllers/Checkout/* and load all the controllers from there -->
</modules>
</args>
</checkout>
</routers>
Just an additional notes for those who wants to know about confliction between extensions and possible solutions(just like above case), refer to the following links:
http://www.webshopapps.com/blog/2010/11/resolving-magento-extension-conflicts/
http://sweettoothrewards.com/wiki/index.php/Resolving_extension_conflicts
http://magebase.com/magento-tutorials/magento-extension-clashes-winners-and-loosers/
http://www.magestore.com/blog/2011/12/21/magento-methods-to-resolve-the-module-conflicts-in-magento/
This is a little notification on the include path of the controller.
This include path can cause errors if the Magento Compiler mode is turned on.
require_once 'Mage/Checkout/controllers/CartController.php';
Instead of that it is good to use
require_once Mage::getModuleDir('controllers', 'Mage_Checkout').DS.'CartController.php';
It will be safer.

Overriding Magento Block

I'm stuck. Been messing with this all day. To me this looks like it should work but It's not, and its not outputting any errors to magento error log.
What I tried to do was simply override the getPriceHtml() function in Catalog/Block/Product.php. The module is active from the 'Advanced' tab via the system configuration.
My config.xml in app/code/local/Brian/Pricefix/etc/config.xml:
<config>
<modules>
<Brian_Pricefix>
<version>1.0</version>
</Brian_Pricefix>
</modules>
<global>
<blocks>
<catalog>
<rewrite>
<product>Brian_Pricefix_Catalog_Block_Product</product>
</rewrite>
</catalog>
</blocks>
</global>
</config>
My Brian_Pricefix.xml in app/etc/modules:
<config>
<modules>
<Brian_Pricefix>
<active>true</active>
<codePool>local</codePool>
</Brian_Pricefix>
</modules>
</config>
My Product.php in app/code/local/Brian/Pricefix/Catalog/Block/Product.php
class Brian_Pricefix_Catalog_Block_Product extends Mage_Catalog_Block_Product
{
public function getPriceHtml($product)
{
Mage::log("IM IN YOUR MODULEZ");
$this->setTemplate('catalog/product/price_fix.phtml');
$this->setProduct($product);
return $this->toHtml();
}
}
The new module isn't taking, its not logging anything or outputting price_fix.phtml
Any suggestions? I've done a few hours or research and this appears to be the right way to extend a block, so i'm not sure whats going on. The lack of error output is frustrating.
Thanks.
Looks like Mage_Catalog_Block_Product is not used anywhere.
catalog.xml contains mainly Mage_Catalog_Block_Product_View or Mage_Catalog_Block_Product_List calls.
You config looks fine. Try overriding another block.
Can you precise the page you are testing on?

Resources