Question

The goal is that we want to be able to change the url under some conditions for all product URLs. It seems that the solution would be to add a function to our module Observer.php. I can't quite get it to run. The original function is

public function getProductUrl($product, $additional = array())
{
    if ($this->hasProductUrl($product)) {
        if (!isset($additional['_escape'])) {
            $additional['_escape'] = true;
        }
        return $product->getUrlModel()->getUrl($product, $additional);
    }

    return '#';
}

What I tried to do is just put in our Observer.php

/**
 * Set the url to be based from it's store not based on the location it's currently showen
 *
 * @return String url
 */
public function getProductUrl($product, $additional = array()) {

    if ($this->hasProductUrl($product)) {
        $pstore_id = array_shift(array_values($_product->getStoreIds()));
        if(Mage::app()->getStore()->getStoreId() == $pstore_id){
            $purl = $product->getUrlModel()->getUrl($product, $additional);//$this->getProductUrl();
        }else{
            $base = Mage::app()->getStore($pstore_id)->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);
            $purl = $base.$product->getUrlPath();
        }
        if (!isset($additional['_escape'])) {
            $additional['_escape'] = true;
        }
        return $purl;
    }
    return '#';
}

But I'm not seeing any change as of yet. What am I missing here. Thank you. Cheers -Jeremy

Was it helpful?

Solution

There is no event that you can hook on for changing the method you want. From the piece of code you posted I conclude you want to override the getProductUrl method from Mage_Catalog_Block_Product_Abstract. You cannot do that unless you copy the file to your local folder. Here are more details about it..
But you can override the product model.
For this you will need a new module (or use one that you already have).
For a new module these are the files you need (Feel free to change the namespace):
app/etc/modules/Easylife_Catalog.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Catalog>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Catalog />
            </depends>
        </Easylife_Catalog>
    </modules>
</config>

app/code/local/Easylife/Catalog/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Catalog>
            <version>0.0.1</version>
        </Easylife_Catalog>
    </modules>
    <global>
        <models>
            <catalog>
                <rewrite>
                    <product>Easylife_Catalog_Model_Product</product>
                </rewrite>
            </catalog>
        </models>
    </global>
</config>

app/code/local/Easylife/Catalog/Model/Product.php - your version of the model

<?php
class Easylife_Catalog_Model_Product extends Mage_Catalog_Model_Product {
    public function getProductUrl($useSid = null){
        //your version of the method here
    }
}

Clear the cache and you are done.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top