Magento - Override adminhtml template file - magento

I have read several posts on stack overflow
Overriding a Magento Adminhtml template file
Magento - overriding Adminhtml block
and a couple threads on the magento forum
http://www.magentocommerce.com/boards/viewthread/21978/
However, None of these posts attempt to do what I am trying to do
I would like to override the
app/design/adminhtml/default/default/template/widget/grid.phtml
file, as this file contains a portion of html that allows anyone to export from the sales->order view.
Note: We have disabled all of the export options for this user role in the permissions->role view
The code that displays the "Export to: " -> "CSV/Excel XML" feature is included in the path I have listed above. I would like to remove that chunk of html and override the file included with Magento.

Adminhtml uses the same theming fallback as the frontend, therefore you need only declare a custom template theme for your installation in module config XML:
<stores>
<admin>
<design>
<theme>
<template>custom</template>
</theme>
</design>
</admin>
</stores>
Then you can create app/design/adminhtml/default/custom/template/widget/grid.phtml with any customizations you like, and this file will be used in preference to the one from the default/default adminhtml theme. Your solution then would be to add an ACL check in the logic which renders the export control:
<?php if($this->getExportTypes() && {ACL LOGIC}}): ?>
<td class="export a-right">
<img src="<?php echo $this->getSkinUrl('images/icon_export.gif') ?>" alt="" class="v-middle"/> <?php echo $this->__('Export to:') ?>
<select name="<?php echo $this->getId() ?>_export" id="<?php echo $this->getId() ?>_export" style="width:8em;">
<?php foreach ($this->getExportTypes() as $_type): ?>
<option value="<?php echo $_type->getUrl() ?>"><?php echo $_type->getLabel() ?></option>
<?php endforeach; ?>
</select>
<?php echo $this->getExportButtonHtml() ?>
</td>
<?php endif; ?>
While this logic might be more appropriately implemented in the block class, the class rewrite system does not accommodate rewriting of parent classes, leaving you to rewrite every subclass. In this instance, obeying DRY outweighs embedding too much logic in templates. Moreover, the change is obvious and easily maintained.
Ideally the core team would have implemented this check in the Mage_Adminhtml_Block_Widget_Grid class or at least provided a public setter for the _exportTypes property, which would have made this logic a bit cleaner to implement.

It might seem the simplest solution to rewrite the block but that's more of a dirty hack than a clean solution. Class rewrites should be used very carefully and always avoided if possible. Otherwise you will quickly run into conflicts and also updating Magento gets a hell.
Usually you can change templates by a custom layout update (i.e. in your local.xml), but in this case it is a widget, which are not configured via layout XML.
So, enter observers: create a module that contains the following in its config.xml
<adminhtml>
<events>
<adminhtml_block_html_before>
<observers>
<yourmodulename_observer>
<class>yourmodulename/observer</class>
<method>changeWidgetTemplate</method>
</yourmodulename_observer>
</observers>
</adminhtml_block_html_before>
</events>
</adminhtml>
If you don't understand any of the above, read about Magento Events and Observers.
Now you will need the observer itself to actually change the template, but only for this block type:
class Your_Modulename_Observer
{
public function changeWidgetTemplate(Varien_Event_Observer $observer)
{
$block = $observer->getEvent()->getBlock();
if ($block instanceof Mage_Adminhtml_Block_Widget_Grid) {
// consider getting the template name from configuration
$template = '...';
$block->setTemplate($template);
}
}
}

Magento - Override adminhtml template file
add below code to config.xml file of extension (you created)
<stores>
<admin>
<design>
<theme>
<default>default</default>
<template>rwd</template>
</theme>
</design>
</admin>
</stores>
Now create rwd folder under adminhtml/default/rwd package.
and create template and layout file as you want to override.
like we want to override order comment history.phtml file.
<root>\app\design\adminhtml\default\default\template\sales\order\view\history.phtml
<root>\app\design\adminhtml\default\rwd\template\sales\order\view\history.phtml

Template definition can be found here
class Mage_Adminhtml_Block_Widget_Grid extends Mage_Adminhtml_Block_Widget
in
public function __construct($attributes=array())
So you need to rewrite sales grid block if you want to remove export csv from Sales Order Grid (use this guide if you don't know how http://www.magentocommerce.com/wiki/groups/174/changing_and_customizing_magento_code) and to change __construct to be like
public function __construct($attributes=array())
{
parent::__construct($attributes);
$this->setTemplate('...'); //here is your template
}

Related

How to get current CMS page name

I am doing via observer to get CMS name which I create.
config.xml
<events>
<adminhtml_cms_page_edit_tab_content_prepare_form>
<observers>
<my_module_edit_tab_content>
<type>singleton</type>
<class>My_Module_Model_Observer</class>
<method>changeContent</method>
</my_module_edit_tab_content>
</observers>
</adminhtml_cms_page_edit_tab_content_prepare_form>
</events>
observer.php
public function changeContent($observer) {
}
You can get CMS page Title like this
<?php
echo Mage::getSingleton('cms/page')->getTitle();
?>
How to get current CMS page name
need to use event
<cms_page_prepare_save>
<observers>
<sky_slack_page_edit_tab_content>
<type>singleton</type>
<class>NameSpace_ModuleName_Model_Observer</class>
<method>addCMS</method>
</sky_slack_page_edit_tab_content>
</observers>
</cms_page_prepare_save>
and in observer
<?php
public function addCMS($observer) {
$cmsName = $observer->getEvent()->getPage()->getTitle();
}
?>
To get the URL key / identifier of any CMS page in Magento, use the following bit of code.
<?php
$cmsPageUrlKey = Mage::getSingleton('cms/page')->getIdentifier();
?>
This will return the path that comes after the website’s URL. For example, the URL identifier for the About Us page might be about-us and not the full URL like http://www.yourwebsite.com/about-us/. If you want the full URL including your website address, then you should use the following piece of code.
<?php
$cmsPageUrl = Mage::getUrl() . Mage::getSingleton('cms/page')->getIdentifier();
?>
I think you're looking for the CMS page Title. If you're on the CMS page, use:
<?php
$current_title = Mage::getSingleton(‘cms/page’)->getTitle();
?>
If you have the identifier, use:
<?php
Mage::getModel(’cms/page’)->getResource()->getCmsPageTitleByIdentifier(’YOUR_IDE‌​NTIFIER’);
?>
(from https://magento.stackexchange.com/questions/102302/get-cms-page-description)

Magento 1.9.X add product with custom options to cart from external site

I have an external website which allows a customer to design a product, then uses an HTML form to post this data. I need to take this information and add this product (with custom options) to the customer's cart in our Magento website, but I'm not sure how to go about this.
I tried something simple at first using URL redirect, but Magento 1.9.X no longer supports adding to cart like this:
$cart_url = "website.com/checkout/cart/add/product=" . $product_id . "&qty=1" //Include custom options somehow
<form action=<?php echo $cart_url?>>
<input type="hidden" value="pid"> product id </input>
<input type="hidden" value="option1"> custom option 1</input>
</form>
Doing research shows that I can also add an item with custom options through either writing a custom Controller or Events/Observers to add the item, but as I'm new to Magento I'm not sure how to trigger events and observer functions from outside of Magento.
Any help to point me in the right direction would be appreciated.
You will have to create a custom module in magento.
Create a file app/etc/MyExtension_AddProductFromUrl.xml
<config>
<modules>
<MyExtension_AddProductFromUrl>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Checkout/>
</depends>
</MyExtension_AddProductFromUrl>
</modules>
</config>
Create a file app/code/local/MyExtension/AddProductFromUrl/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<MyExtension_AddProductFromUrl>
<version>0.1.0</version>
</MyExtension_AddProductFromUrl>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<MyExtension_AddProductFromUrl before="Mage_Checkout">MyExtension_AddProductFromUrl</MyExtension_AddProductFromUrl>
</modules>
</args>
</checkout>
</routers>
</frontend>
</config>
Create a file app/code/local/MyExtension/AddProductFromUrl/controllers/CartController.php
<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class MyExtension_AddProductFromUrl_Checkout_CartController extends Mage_Checkout_CartController {
# overloaded addAction
public function addAction() {
// generate form_key if missing or invalid
if (!($formKey = $this->getRequest()->getParam('form_key', null)) || $formKey != Mage::getSingleton('core/session')->getFormKey()) {
$this->getRequest()->setParams(array('form_key' =>Mage::getSingleton('core/session')->getFormKey()));
}
// do parent actions
parent::addAction();
}
}
?>
Also see
Magento - Add a product to the cart via query string without form_key parameter
https://magento.stackexchange.com/questions/37779/i-want-to-use-add-to-cart-via-url-in-magento-1-8-but-dont-know-which-files-to-c
I fought with this for a bit in 1.9.0.1, but St0iK's solution above worked with the following changes:
1) place the module .xml file in app/etc/modules (as opposed to app/etc)
2) In the controller file - i had to remove _Checkout_
class MyExtension_AddProductFromUrl_Checkout_CartController extends Mage_Checkout_CartController {
to
class MyExtension_AddProductFromUrl_CartController extends Mage_Checkout_CartController {
After these minor edits, it works perfectly. Not sure if these were only necessary for 1.9.0.1, but for whatever reason, they were.
To add a product to the cart, i just use the URL format
YOURSTORE.com/checkout/cart/add/product/123/qty/1
Great solution for external PPC or SEO landing pages that need a simple "buy now" button that drops right into your magento cart.

How to get checkout/cart_sidebar block in Magento from everywhere

Let me know how can I get "checkout/cart_sidebar" block as HTML into variable ?
I need to get it from my own magento controller
As I see "checkout/cart_sidebar" depends on "Mage_Checkout_Block_Cart_Sidebar" class
so may It is possible to get this template via some Mage:: Static Instance Methods
I tried a few ways I couldnt (
thanks
In your controller you could try something like this:
$block = $this->getLayout()->createBlock('checkout/cart_sidebar');
$block->setTemplate('checkout/cart/sidebar.phtml');
Depending on your configuration (Config -> Checkout -> Shopping Cart Sidebar), you can render the template with
$block->toHtml();
If you use a custom template, you could ignore the config value so it renders anytime.
Actions in layout xml configs are just a block method call.
The two below are equivlent
<block type = "checkout/cart_sidebar"
name = "cart_sidebar"
as = "cartExplorer"
template = "checkout/cart/sidebar.phtml"
before = "-">
<action method="addItemRender">
<type>configurable</type>
<block>checkout/cart_item_renderer_configurable</block>
<template>checkout/cart/sidebar/default.phtml</template>
</action>
<!-- Programatically create the block -->
<?php
$this->getLayout()
->createBlock('checkout/cart_sidebar', 'cart_sidebar')
->setTemplate('checkout/cart/sidebar.phtml');
->addItemRender(
'configurable',
'checkout/cart_item_renderer_configurable',
'checkout/cart/sidebar/default.phtml'
)
?>
<!-- This is if it was already created in a layout.xml file -->
<?php
$this->getLayout()
->getBlock('cart_sidebar')
->addItemRender(
'configurable',
'checkout/cart_item_renderer_configurable',
'checkout/cart/sidebar/default.phtml'
)
?>
Hope this helps!

Module not working in joomla 3.0

problem is self made module not working- I make my self made module in Joomla 3.0. I made a mod_products folder here we created a file called mod_products.php.
mod_products.php - code
defined('_JEXEC') or die;
require_once __DIR__ . '/helper.php';
$value = modProductsHelper::getproducts( $params );
require JModuleHelper::getLayoutPath('mod_products', $params->get('layout', 'default'));
and after it I made second file helper.php code -
class modProductsHelper{
public static function getProducts( $params ){
return 'Products';
}
}
and third one is default.php
<?php
defined('_JEXEC') or die;
if($value!='') { ?>
<ul style="margin-left: 0px;" class="clients-list slides-list slide-wrapper">
<li class="slide">
<div class="product-image"><img src="images/product3.png" width="181" height="177"></div>
</li>
</ul>
<?php } ?>
Then we install through administrator panel and gave a position to the mod_products module and display in index.php file like so:
<div class="grid_12 product_home">
<jdoc:include type="modules" name="position-3" />
</div>
But it's not being displayed on the site. Does anyone have any idea why?
Edit:mod_products.xml
<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.0" client="site" method="upgrade">
<name>mod_products</name>
<author>Joomla! Project</author>
<creationDate>July 2004</creationDate>
<copyright>Copyright (C) 2005 - 2013 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>admin#joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>3.0.0</version>
<description>MOD_PRODUCTS_XML_DESCRIPTION</description>
<files>
<filename module="mod_products">mod_products.php</filename>
<folder>tmpl</folder>
<filename>helper.php</filename>
<filename>mod_products.xml</filename>
</files>
<config>
</config>
</extension>
Right, I've created a small example for you. I think it might have been due to you calling the wrong module layout, not entirely sure.
Here is a link to download the module. Uninstall the current module you're using via the Joomla backend and install this:
http://www.mediafire.com/download/ucp3prv219430zl/mod_products.zip
Also, don't forget to assign the module to a menu item. That might have been the problem before
Enjoy
For me it seems your problem is module name.In some places you have used mod_product and in other places mod_products Please make sure you use only one.I will suggest you to change
require JModuleHelper::getLayoutPath('mod_products', $params->get('layout', 'default'));
to
require JModuleHelper::getLayoutPath('mod_product', $params->get('layout', 'default'));
Also check that you have published the module and also test it for all pages.

Change Magento frontend router to custom URL

I would like to rename the URL path of the shopping cart from checkout/cart to checkout/adverts.
I have tried the following two methods:
Catalog > URL Rewrite Management: Custom
I have also tried doing it using a custom module, with the following in my config.xml:
<global>
<rewrite>
<mynamespace_mymodule_checkout_cart>
<from><![CDATA[#^/checkout/cart#]]></from>
<to>/checkout/adverts</to>
</mynamespace_mymodule_checkout_cart>
</rewrite>
</global>
Both methods have gone to the correct URL path, but shown a 404 Error page - is there something else I need to do? Magento ver. 1.5.0.1
What happen if you add a empty controller to your custom module
<?php
require_once('Mage/Checkout/controllers/CartController.php');
class Mynamespace_Mymodule_CartController extends Mage_Checkout_CartController
{
}
As R.S said you have to extend CartController.php in your module
<?php
require_once('Mage/Checkout/controllers/CartController.php');
class Mynamespace_Mymodule_CartController extends Mage_Checkout_CartController
{
}
?>
in it copy the indexAction() of Mage/Checkout/controllers/CartController.php
Also in your local.xml write
<mymodule_cart_adverts>
// copy the xml code inside <checkout_cart_index> ... </checkout_cart_index> here
</mymodule_cart_adverts>
I havent tried it but it should work

Resources