Magento: rewriting multiple URLs (from) to one target (to) using a regex? - magento

I want to rewrite some of my URLs using a regex. This is my rewrite stuff in config.xml of my module.
<rewrite>
<My_Module>
<from><![CDATA[#^/(abc)|(def)/configuration/#]]></from>
<to>/config/configuration/</to>
<complete>1</complete>
</My_Module>
</rewrite>
As you see, what I am trying to achive is to rewrite all urls that contain abc/configuration or def/configuration. This does not work. So how can I add multiple URLs to the same rewrite in Magento? I dont care if its done with a regex or a line for each rewrite in config.xml, so far I did not manage to figure it out either way.
Thanks!
Addition: I solved this now by using:
<from><![CDATA[#^/.+/configuration/#]]></from>
This did the trick in my case :) ... I am still interested how to solve it the first way though. If anybody knows, please post anyway!

Select first OR second is written like this /(first|second)/
Code bellow is working in 1.8 magento.
$2 will be replaced with what ever (.*) captures.
<config>
<global>
<rewrite>
<My_Module>
<from><![CDATA[#^/(abc|def)/configuration/(.*)#]]></from>
<to><![CDATA[/config/configuration/$2]]></to>
<complete>1</complete>
</My_Module>
</rewrite>
</global>
<config>

Related

Magmi url_key slug - how to include sku?

I'm trying to figure out the best course of action to change URL keys in the store to be name-sku.html
Currently MAGMI seems to be dictating the url key, which is fine. Some of our products are not imported through MAGMI, so I think I'd probably have to also use the updateurl event to change the url on save -- I found this here for that -- https://magento.stackexchange.com/questions/24869/manufacturer-in-product-url-key
The question is, how is it best to do this within MAGMI? I'm importing about 300,000 products currently. The URL also needs to change, as it currently just uses the default settings.
I've looked into the wiki here -- http://wiki.magmi.org/index.php?title=Value_Replacer -- I guess the following code could work for the way it is currently working -- but what do I actually type into the value replacer box?
{{ Slugger::slug({item.name}) }}
Does anyone know how to add the sku to this as well? Would I just do something like....
{{ Slugger::slug({item.name} ."-". {item.sku}) }}
Also, does anyone know if this automatically adds the dashes and the .html?
Any guidance would be extremely appreciated.
EDIT:
I have successfully tested this as working in my test environment, but it is not working as expected in my production environment. I'll go over what happens, and what I did.
1) I purged the core_url_rewrite table
2) I deleted all cache
3) I set up MAGMI (updated it to the most current version as well, and triple checked all my settings)
4) MAGMI went through fine, and the url key on about 5 products that I checked for were how I would expect
5) During the catalog url rewrites reindex, all url keys disappeared.
6) After the reindex was done, they were now in the default magento format (using just the name, and not the sku)
Any ideas?
The only difference between production and development that I can think of at the moment that may have anything to do with this is the fact that I did create a module based on the linked manufacturer-in-product-url-key question on production. Now that I think of it, maybe that's the main difference that I need to address..... I didn't think reindexing would have anything to do with that, but maybe I'm wrong.
Anyway, if anyone has any insight, I would still appreciate it. I have a feeling maybe MAGMI should also be rewriting the url_path?? I think I read something about that somewhere.
Ok, I figured this out. I think this is the best approach.
I was mistaken in my question. I'm actually looking to add the mpn, not the sku. Either way, that doesn't change the process.
So, the best thing to do is to use the Value Replacer in MAGMI, and to create a custom module that updates the url on the Magento catalog_product_save_before event.
Here's exactly what I did.....
MAGMI settings (Value Replacer)
Replaced attributes: url_key
New value for url_key: {{ Slugger::slug({item.name}.' '.{item.mpn}) }}
URLKeyChange (Magento Module)
app/code/local/My/URLKeyChange/etc/config.xml
<config>
<global>
<models>
<LeathornURLRewrite>
<class>Leathorn_URLKeyChange_Model</class>
</LeathornURLRewrite>
</models>
<events>
<catalog_product_save_before>
<observers>
<LeathornURLRewrite>
<type>singleton</type>
<class>LeathornURLRewrite/observer</class>
<method>updateurl</method>
</LeathornURLRewrite>
</observers>
</catalog_product_save_before>
</events>
</global>
</config>
app/code/local/My/URLKeyChange/Model/Observer.php
class My_URLKeyChange_Model_Observer {
public function updateurl($observer){
Mage::log('My log entry', null, 'mylogfile.log');
if($observer->getEvent()->getProduct()){
$Product=$observer->getEvent()->getProduct();
$Url='';
if(!is_null($Product->getData('name'))):
$Url=$Url.$Product->getData('name');
endif;
if(!is_null($Product->getData('mpn'))):
$Url=$Url.$Product->getData('mpn').'-';
endif;
//Mage::log('My log entry'.$Url, null, 'mylogfile.log');
$Product->setData('url_key',$Url);
}
}
}
app/etc/modules/My_URLKeyChange.xml
<config>
<modules>
<My_URLKeyChange>
<!-- Whether our module is active: true or false -->
<active>true</active>
<!-- Which code pool to use: core, community or local -->
<codePool>local</codePool>
</My_URLKeyChange>
</modules>
</config>
URL key will always be just dash separated strings. Don't worry, magento will add .html in it dynamically. You can see in magnento backend in any product configuration page, url key is without html.
Also magento will add dash in url key, Replace your "-" by space " ". Otherwise it will take last word of name and SKU as one word. :)

Magento - Custom Module Url

I have had a request to change the url of a module.
For example let's say the url is www.example.com/world
I need the url to be www.example.com/hello/world
I know that within the modules config xml you can change the node to whatever you like (provided it's unique) but does not allow prepend /hello/
<frontend>
<routers>
<anexample>
<use>standard</use>
<args>
<module>An_Example</module>
<frontName>hello/world</frontName>
</args>
</anexample>
</routers>
</frontend>
Does anyone have any idea how this can be done ? Some example code or even a point in the right direction would be appreciated!
Sorry if this isn't detailed enough, i'll be happy to post more of my code if required.
Thanks in advance.
You can add rewrites into Magento's URL Rewrite Manager. Haven't tested following code. But hope it helps.
Mage::getModel('core/url_rewrite')
->setIsSystem(0)
->setStoreId($storeId)
->setOptions('RP')
->setIdPath('index.php?cat=c' . $categoryId . '_' . $this->strip($data['name']) . '.html')
->setTargetPath($categoryModel->getUrlPath() . '.html')// Put the actual path
->setRequestPath('index.php?cat=c' . $categoryId . '_' . $this->strip($data['name']) . '.html') // put the path you want to display
->save();
here i can give you idea to reach your solution it will sure help you
<?xml version="1.0"?>
<config>
<global>
<rewrite>
<some_unique_name_to_identify_this_rewrite>
<from><![CDATA[#^/from_this/#]]></from>
<to>/to_this/</to>
</some_unique_name_to_identify_this_rewrite>
</rewrite>
</global>
</config>
And if you want to do url rewrite with Admin functionality you can go throw this link
Also what you want to achieve could be done with Custom Url Rewrite. For example:
request path: hello/world
target path: 'world'
In this way you're making a destination of system rewrite yours start point.
hope this will sure help you
This was pretty simple in the end. Instead of going to the hassle of creating a rewrite for it i just changed my frontname to 'hello' then created a controller called 'worldController'
Then in my indexController i just put a redirect to */world
Please see the following article by Allan Storm about the Magento rewrite
http://alanstorm.com/magento_dispatch_rewrites_intro
Access custom Module controller path as custom HTML Url Link
Say http://domain.com/index.php/customwallpaper/index/create/
will be rewrited to
http://domain.com/index.php/fotomural-personalizado.html
Insert below data in core_url_rewrite table for url rewrite
Mage::getModel('core/url_rewrite')
->setStoreId(1)
->setIdPath('customwallpaper')
->setRequestPath('fotomural-personalizado.html')
->setTargetPath('customwallpaper/index/create') // Put the actual Controller path
->setIsSystem(1)
->save();
-OR-
Run below sql query
INSERT INTO `core_url_rewrite`
(`url_rewrite_id`, `store_id`, `id_path`, `request_path`, `target_path`, `is_system`, `options`, `description`, `category_id`, `product_id`)
VALUES ('7', '1', 'customwallpaper', 'fotomural-personalizado.html', 'customwallpaper/index/create', '1', NULL, NULL, NULL, NULL);

Magento newsletter_subscriber change_status_at

I've been looking for a while at other threads, several have been close to what I need but not quite.
When a guest or customer sign up for our newsletter the field change_status_at populates with a timestamp.
However, if the guest or customer unsubscribe, we would like the the change_status_at field to pickup the current timestamp.
This is important to use because we do not use the newsletter "feature" of Magento 1.7.0.2 CE. Rather we export the newletter_subscriber list to a company to send an email.
Thank you,
Dan
I just ran into this too. A customer wanted to know the date when a subscribe/unsubscribe action occurred. Looking around the internets I've found a few others who have run into this and some claim that it used to work in very old version of Magento.
I think what happened is that the field definition for change_status_at used to be setup to auto update as a default timestamp (i.e. ON UPDATE CURRENT_TIMESTAMP), but that was lost in some update. So there's no Magento code that writes to the field, because MySql is supposed to magically maintain it.
You could try updating the table definition to add the ON UPDATE CURRENT_TIMESTAMP back in (but I'm not in favor changing default model tables) or adding another field to be the default timestamp field.
Or a better solution would be to create a module with an observer to just add the date in when the subscription changes. Here's that might look like (warning - this code is just an example, there may be some syntax errors due to expunging my module's info) -
app/code/local/Myco/MyMod/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Myco_MyMod>
<version>1.0.0</version>
</Myco_MyMod>>
</modules>
<global>
<models>
<myco>
<class>Myco_MyMod_Model</class>
</myco>
</models>
<events>
<newsletter_subscriber_save_before>
<observers>
<mycomymod_observer_subscriber>
<type>singleton</type>
<class>Myco_MyMod_Model_Observer</class>
<method>setUpdateDate</method>
</mycomymod_observer_subscriber>
</observers>
</newsletter_subscriber_save_before>
</events>
</global>
</config>
===========
in app/code/local/Myco/MyMod/Modules/Observer.php
<?php
class Myco_MyMod_Model_Observer
{
public function setUpdateDate(Varien_Event_Observer $observer) {
$subscriber = $observer->getSubscriber();
$subscriber['change_status_at'] = (date("Y-m-d H:i:s", time()));
}
}

Magento Router Url - Need Hyphnated Path Name

Let's say I use a custom controller to have a url path/frontend name of
/customcategory
Well, obviously if I have a controller file named 'TestController.php' and indexAction
the url path would be
/customcategory/test/index
What I am trying to figure out is how I do re-name the Test Controller, or modify the config xml file, so I can have a hyphenated url from a controller file such as
/customcategory/test-section/index
I know that if I want /customcategory to be hyphenated, I can just modify the frontend tag in the config file. But the site I am building would benefit from a hyphenated controller route, the part that comes after /customcategory with keywords and I cannot get it to work nor can I find an example on google - as crazy as that may seem.
Thanks for your time.
What you are trying to do is possible using global rewrite in your custom module. You could pass all incoming request for /customcategory/* to a specific controller action. But you would have to manage your own route (base on the depth of your url path).
e.g www.MageIgniter.com/customcategory/path1/path2
config.xml
<global>
<rewrite>
<fancy_url>
<from><![CDATA[/customcategory\/(.*)/]]></from>
<to><![CDATA[customcategory/index/processroute/tagname/$1/]]></to>
<complete>1</complete>
</fancy_url>
<rewrite>
</global>
<frontend>
<routers>
<tagseo>
<use>standard</use>
<args>
<frontName>customcategory</frontName>
</args>
</tagseo>
</routers>
class MageIgniter_Customcategory_IndexController extends Mage_Core_Controller_Front_Action
{
public function processRoute(){
print_r($requestUri = Mage::app()->getRequest()->getRequestUri()); //path1/path2
print_r($this->getRequest()->getParam('tagname')); // path1
print_r($this->getRequest())
// do you custom logic here base on about request path explode('/', trim($requestUri,'/'))
}
...
For a working example see "Product Tags" section # http://www.contempospace.com/bedroom-furniture/wardrobe-closets/custom-closet-systems/isa-closet-system-shelves-hanging-walk-in-reach-in-closet.html
As far as I am aware you cannot add hypens in the url to match up to a filename. If you are trying to get a folder structure you can just add more paths to it.
For example if you wanted:
Namespace/CustomCategory/controller/test/SectionController.php
you could do:
/customcategory/test_section/index

Magento Email Templates

How can I set different email templates for Customer Order Confirm email and Admin copy of the same.
I need to add some extra content for the Admin email copy.
Thanks
I am assuming that you are currently using the "copy" feature to send the admin email. Let me know if that's not the case. Because the same email is currently being sent to multiple recipients, it would be difficult to change the content for each recipient. You could send multiple emails with a little bit of code, though, which would allow you to use a different email template for each. This could be achieved by creating a new class:
class MyModule_Model_Sales_Order extends Mage_Sales_Model_Order {
/**
* Sending email with order data
*
* #return Mage_Sales_Model_Order
*/
public function sendNewOrderEmail() {
parent::sendNewOrderEmail();
/**
* Your admin email sending code here. Copy it out of the sendNewOrderEmail
* function in Sales_Order.
*/
return $this;
}
}
And then telling Magento to override the core class inside your module config:
<config>
<global>
<models>
<mymodule>
<class>MyModule_Model</class>
</mymodule>
<sales>
<rewrite>
<order>MyModule_Model_Sales_Order</order>
</rewrite>
</sales>
</models>
</global>
</config>
You'll need to create the template you want and be sure that your overridden model uses that template instead.
Change tag catalog to sales and add namespace with class name, Eg: if namespace is Custom then add Custom_MyModule_Model and Custom_MyModule_Model_Sales_Order, so the resulting XML file would be:
<config>
<global>
<models>
<mymodule>
<class>Custom_MyModule_Model</class>
</mymodule>
<sales>
<rewrite>
<order>Custom_MyModule_Model_Sales_Order</order>
</rewrite>
</sales>
</models>
</global>
</config>
you can check and edit the HTML files in app > locale > en_US > templates > email
Please keep in mind that you are overruling the Sales and not the Catalog.
So to prevent searching why the above code is not working use "sales" instead of "catalog" in the config.xml.
This extension does it: http://codecanyon.net/item/send-new-order-email-to-admin/3198802
Totally worth the $10 given all the time I wasted trying to get the other answer here working. I had just finished making my own custom module for the contact form, but the other solutions here didn't work.

Resources