Magento:将查询字符串添加到产品网址的末尾?

Greetings!

I am looking for a better way to do a modification.. I am adding a query string to the end of the product url. For example, the url would look like

http://demo.magentocommerce.com/coalesce-functioning-on-impatience-t-shirt.html?flagged=yes

notice the ?flagged=yes.

I am currently doing this by adding the following code to the list.phtml file:

$query = '?flagged='.$answer;

then I add this query string to the end of the product url by doing this:

$_product->getProductUrl().$query

I really like to try and leave modifications off the template file. I know there is probably a better way of doing this. I have tried everything I can think of with no luck, so if anyone has any ideas this would be great! The ideal situation would be to set up an observer and add the query string to the product url then.

I greatly appreciate any ideas or help!

There is no way done it. Only override product model and extends getProductUrl()

function getProductUrl($params, $useSid = null) {
$url = pagent::getProductUrl($useSid);
// now parse url, add our parameters from array $params
// generate new url
return $url
}

You can create helper with this method:

public function getProductUrl($urlModel, $product, $query = array(), $useSid = null)
{
   if ($useSid === null) {
      $useSid = Mage::app()->getUseSessionInUrl();
   }

   if (!$useSid) {
      $params['_nosid'] = true;
   }

   if (!empty($query)) {
      $params['_query'] = $query;
   }

   return $product->getUrlModel()->getUrl($product, $params);
}

Invoke helper method:

getProductUrl($_product, array('flagged' => 'YES'));

And that's should be enough.

$product->getUrlModel()->getUrl($product, array('_query' => array(
    'show_offers' => '1'
)))