Custom URL for magento extension - php

I am building a magento extension that will need a custom url for the frontend that is set via the admin panel config setting for the extension.
Along the lines of the "set admin url" setting in the system settings (which can also be set in the app/etc/local.xml file as well).
basically i have in my config.xml file
<frontend>
<routers>
<extensionname>
<use>standard</use>
<args>
<module>My_Extensionname</module>
<frontName>extensionname</frontName>
</args>
</extensionname>
</routers>
</frontend>
This creates the url site.com/extensionname
But I want to be able to set the url in the system/settings tab in the admin panel
I have looked through the core code and seen glimpses of code that does (a predispatch models controllers etc i think) this for the default admin url key setting
How would i go about this? Would i set up an observer to catch the request to url? or observer settings change and programmatically create a url rewrite?
What about the content & root template as well in the layout/extensionname.xml?
<layout version="0.1.0">
<extensionname_index>
<reference name="root">
<action method="setTemplate"><template>extensionname/page.phtml</template></action>
</reference>
<reference name="content">
<block type="extensionname/extensionname" name="extensionname" template="extensionname/extensionname.phtml" />
</reference>
</extensionname_index>
Would this still be used even though i would be using a custom url from the settings

Ok could not get any of the above info (links) to work from my end (in regards to using custom routers etc) because i think my extension is not using any collections from the database as its just a landing page so wont have index/index/id values etc.
So went with a dirty hack to do the job for now. See below.
etc/config.xml (observer event when admin field setting saved)
<config>
...
<frontend>
<routers>
<myextension>
<use>standard</use>
<args>
<module>Mycompany_Myextension</module>
<frontName>myextension</frontName>
</args>
</myextension>
</routers>
...
</frontend>
<global>
...
<events>
<admin_system_config_changed_section_myextension>
<observers>
<myextension>
<type>singleton</type>
<class>myextension/observer</class>
<method>observersave</method>
</myextension>
</observers>
</admin_system_config_changed_section_myextension>
</events>
...
</global>
</config>
Model/Observer.php (Save a URL Rewrite) (EDITED)
public function observersave(Varien_Event_Observer $observer)
{
#remove the old urlrewrite
$url = Mage::getStoreConfig('myextension/general/url');
$uldURLCollection = Mage::getModel('core/url_rewrite')->getResourceCollection();
$uldURLCollection->getSelect()
->where('id_path=?', 'myextension');//EDIT: so overwrites on each save
$uldURLCollection->setPageSize(1)->load();
if ( $uldURLCollection->count() > 0 ) {
$uldURLCollection->getFirstItem()->delete();
}
#add url rewrite
$modelURLRewrite = Mage::getModel('core/url_rewrite');
$modelURLRewrite->setIdPath('myextension/'.strtolower($url))
->setTargetPath('myextension/index/index/id/'.$url.'')
->setOptions('')
->setDescription('New URL - Created as a new setting was saved')
->setRequestPath('myextension/url/'.$url.'');//EDIT: added extra rewrite url paths so rewrite can never conflict if admin setting field is set to a "key default url" like "admin" or "checkout" or "contacts" etc
$modelURLRewrite->save();
}
controllers/IndexController.php (redirects if no ID...) (EDITED)
public function preDispatch()
{
//$url = Mage::getStoreConfig('myextension/general/url');
if ( !strstr($this->getRequest()->getRequestUri(), 'myextension/index/index/id') ) {
parent::preDispatch();
}
}
public function indexAction()
{
$url = Mage::getStoreConfig('myextension/general/url');
if ( trim($this->getRequest()->getParam('id')) == '' ) {
$this->_redirect('/');//Edit: changed redirect to root
} else {
$id = $this->getRequest()->getParam('id');
if($id == $url) {
$this->loadLayout( array(
'default',
'myextension_index_index'
));//EDIT: added if statement to see if myextension/index/index/id matched admin setting, if not redirect to root
$this->renderLayout();
}else{
$this->_redirect('/');//Edit: changed redirect to root
}
}
}
so at this point i have URLs at: (EDITED)
site.com/myextension/url/myadminfieldvalue
&
site.com/myextension/index/index/id/myadminfieldvalue
(EDITED)
but in template/myextension/myextension.phtml
<?php if($current_url == ''.$base_url.'myextension/index/index'){ ?>
<p>Disabled cause i dont really want info at this url..</p>
<?php }else{ ?>
<p>show data because your accessing myextension/index/index via the rewrite /myextension/url/myadminfieldvalue.</p>
<?php } ?>
A bit more work is needed to achieve what i want but for now it works for me and hope others may find this useful.

Related

Magento translate.csv file not loading

I created a new package/theme/skin from scratch in Magento 1.9.1.
The theme and skin load fine. I tried adding translations and cannot get them to load. All cache settings are disabled.
I've added the CSV file here:
/app/design/frontend/{my_package}/{my_theme}/locale/en_US/translate.csv
translate.csv:
"testing123","Translation Worked"
header.phtml
<?= $this->__('testing123') ?>
I added {my_theme}, which for me is named "responsive" to the admin at the :
The result is the header displaying 'testing123' instead of the translated 'Translation Worked'. What have I missed? How do I troubleshoot this?
Update:
I've also added translate.csv to config.xml (and it still does not work). However, It DOES work if I move the translate.csv to app/locale/en_US but it DOES NOT work in app/design/frontend/{my_package}/{my_theme}/locale/en_US/translate.csv where I want it. How can I tell it to load from the theme folder?
config.xml:
<?xml version="1.0"?>
<config>
<modules>
<MyModule_Templates>
<version>0.1.0</version>
</MyModule_Templates>
</modules>
<frontend>
<translate>
<modules>
<MyModule_Templates>
<files>
<default>translate.csv</default>
</files>
</MyModule_Templates>
</modules>
</translate>
</frontend>
<global>
<page>
<layouts>
<mymodule_default translate="label">
<label>MyModule Default Layout</label>
<template>page/default.phtml</template>
<layout_handle>mymodule_default</layout_handle>
</mymodule_default>
</layouts>
</page>
</global>
</config>
Seems, You forgot to add translate.csv to your module's config.xml.
Please take a look.
http://inchoo.net/magento/how-to-add-your-own-translations-to-custom-modules/
i struggled with the same problem. Solution for me was to change the translation Source in the Admin Area.
Go to : Configuration -> select your Store view ( new Translation ) -> General -> under "options for localisation" uncheck the box and choose which translation you want to use. Afterwards, the translate.csv gets loaded and the translation works.
Cheers,
moktor

Magento Custom filter approch

I am implementing a custom refine search functionality on product list page in magento
There are 4 attributes which will be using like color,size etc.
for this i have created a custom module which has a block.
I want to use this block on product list catalog page.
I am trying to put following in catalog.xml in my custom theme
<block type="filter/form" name="filter.form" template="catalog/navigation/filter.phtml" />
Where filter is my custom module, but somehow it is not working.
Am i going with wrong approch , if yes, Please suggest me the steps to implement custom filtering on product list page.
blocks parts in config.xml
<global>
<blocks>
<Webonise_Filter>
<class>Webonise_Filter_Block</class>
</Webonise_Filter>
</blocks>
<helpers>
<Webonise_Filter>
<class>Webonise_Filter_Helper</class>
</Webonise_Filter>
</helpers>
</global>
I advise you to change your config to :
<global>
<blocks>
<webonisefilter>
<class>Webonise_Filter_Block</class>
</webonisefilter>
</blocks>
<helpers>
<webonisefilter>
<class>Webonise_Filter_Helper</class>
</webonisefilter>
</helpers>
</global>
And then you can call your block with :
<block type="webonisefilter/form" name="filter.form" template="catalog/navigation/filter.phtml" />
The underscore is not always correctly interpreted. The idea is to get the shortcut from you config.xml and use it in your layout.xml. You need to have a block called Webonise_Filter_Block_Form to match webonisefilter/form.

Magento: How to include a php call in the CMS pages

Following the instructions from this page:
http://www.magento.cc/how-to-use-php-on-a-cms-page.html
1) i declared the module in the app/etc/modules :
<?xml version="1.0"?>
<config>
<modules>
<Stock_status>
<active>true</active>
<codePool>local</codePool>
</Stock_status>
</modules>
</config>
2) Then i created the config.xml in app\code\local\Stock\status\Custom\etc with these contents in it:
<?xml version="1.0"?>
<config>
<global>
<blocks>
<Stock_status>
<class>Stock_status_Block</class>
</Stock_status>
</blocks>
</global>
</config>
3) afterwards i created a test.php in app/code/local/Stock/status/Custom/Block file with these contents in it:
<?php
class Stock_status_Block_Test extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
.Mage::helper('customstockstatus')->getListStatus($_product->getId()).
return $html;
}
}
and finally
4) i went in a cms page to see if it works by putting this value in:
{{block type="Stock_status_Custom/test" ListStatus="02"}}
This line of code should return me the stock status of a specific product (in this example, the product with the ID 02)
BUT it doesn't work. What have i done incorrectly? Can someone please help me?
These are an unnecessary works, you can simply call the php file by adding the following line into that cms page content
{{block type="core/template" template="page/urfile.phtml"}}
Then create file under the page directory
go to Magento Admin->CMS->Pages
Click Add New Page
Input Page Title
Input URL Key
Right Content inside (body)
paste your code.
Hope this help

Shopping Cart Page title change in Magento

I need to change shopping cart page title. but i could not find it. where i should change it. so any help appreciated.
thanks
Changing the XML will have no effect because the title is set by the controller at app/code/core/Mage/Checkout/controllers/CartController.php.
$this
->loadLayout()
->_initLayoutMessages('checkout/session')
->_initLayoutMessages('catalog/session')
->getLayout()->getBlock('head')->setTitle($this->__('Shopping Cart'));
It's never a good idea to modify core files, and overriding controllers can be tedious. Therefore, the correct and quickest place to change this is in your translation file, located at app/locale/YOUR_LANGUAGE/Mage_Checkout.csv. If you do not have this file in your relevant directory you may create it and just add this line:
"Shopping Cart","NEW TITLE HERE"
If you do have the file then simply edit that line, ensuring your new title follows the original title and comma and is enclosed in double quotes.
The right way to do it, is making an override on the checkout controllers, is so easy.
First:
Add a new module with two subdirectories: controllers and etc.
Mynamespace/Checkout/controllers
Mynamespace/Checkout/etc
Then, in the etc directory add the file: CartController.php with the next content:
require_once 'Mage/Checkout/controllers/CartController.php';
class Mynamespace_Checkout_CartController extends Mage_Checkout_CartController
{
public function indexAction()
{
$cart = $this->_getCart();
if ($cart->getQuote()->getItemsCount()) {
$cart->init();
$cart->save();
if (!$this->_getQuote()->validateMinimumAmount()) {
$minimumAmount = Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())
->toCurrency(Mage::getStoreConfig('sales/minimum_order/amount'));
$warning = Mage::getStoreConfig('sales/minimum_order/description')
? Mage::getStoreConfig('sales/minimum_order/description')
: Mage::helper('checkout')->__('Minimum order amount is %s', $minimumAmount);
$cart->getCheckoutSession()->addNotice($warning);
}
}
// Compose array of messages to add
$messages = array();
foreach ($cart->getQuote()->getMessages() as $message) {
if ($message) {
// Escape HTML entities in quote message to prevent XSS
$message->setCode(Mage::helper('core')->escapeHtml($message->getCode()));
$messages[] = $message;
}
}
$cart->getCheckoutSession()->addUniqueMessages($messages);
/**
* if customer enteres shopping cart we should mark quote
* as modified bc he can has checkout page in another window.
*/
$this->_getSession()->setCartWasUpdated(true);
Varien_Profiler::start(__METHOD__ . 'cart_display');
$this
->loadLayout()
->_initLayoutMessages('checkout/session')
->_initLayoutMessages('catalog/session')
->getLayout()->getBlock('head')->setTitle($this->__('Here it go the new title!!!!'));
$this->renderLayout();
Varien_Profiler::stop(__METHOD__ . 'cart_display');
}
}
and then, the config.xml file:
<config>
<modules>
<Mynamespace_Checkout>
<version>0.1.0</version>
</Mynamespace_Checkout>
</modules>
<frontend>
<routers>
<checkout>
<args>
<modules>
<mynamespace_sales before="Mage_Checkout">Mynamespace_Checkout</mynamespace_sales>
</modules>
</args>
</checkout>
</routers>
</frontend>
And last, the module activator: app/etc/modules/Mynamespace_Checkout.xml
<config>
<modules>
<Mynamespace_Checkout>
<active>true</active>
<codePool>local</codePool>
</Mynamespace_Checkout>
</modules>
</config>
This was tested in a Magento Enterprise 1.13.
Greetings
The title is actually set in the XML for that page. You should open the checkout.xml file in the app/design/frontend/packagename/themename/layout/ directory and place this code inside of the node in the XML:
<reference name="head">
<action method="setTitle"><title>My New Checkout Title</title></action>
</reference>
By default, I believe it grabs the label associated of the handle if the page title is not assigned explicitly (right now the label is "Shopping Cart" which is why you are getting that title).
Here is what my code looks like:
<checkout_cart_index translate="label">
<label>Shopping Cart</label>
<remove name="right"/>
<remove name="left"/>
<!-- Mage_Checkout -->
<reference name="head">
<action method="setTitle"><title>My New Checkout Title</title></action>
</reference>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<!-- More Below -->
</checkout_cart_index>
One other thing to mention, you can also make these changes in a local.xml (which is what I would recommend). The local.xml file will load on top off all the other XML files and your changes in that file will override any other XML files in the layout directory. A pretty good tutorial can be found here.
Here is a short example how to overwrite the title.
Force title
You can change the variable and overwrite the title in the specified layout.xml files.
Try this :
<reference name="head">
<action method="setTitle"><title>My New Checkout Title</title></action>
</reference>

config.xml override results in cart not visible?

I am building a second add to cart button with a different redirect on a magento platform.
The tutorial I've used is self explainatory. See here
It works okey as the redirect is in place.
The thing is: The cart itself doesnt show its page (cart.phtml) anymore.
Any idea how to fix this? Or where the problem exists?
If I remove the code underneath the cart is back again, as the redirect isn't working anymore.
File : config.xml
<global>
<routers>
<checkout>
<rewrite>
<cart>
<to>mycheckout/cart</to>
<override_actions>true</override_actions>
<actions>
<add>
<to>mycheckout/cart/add</to>
</add>
</actions>
</cart>
</rewrite>
</checkout>
</routers>
</global>
Seems like your extension does not extend the cart controller that you are overwriting making it impossible to display the methods from original controller

Categories