且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento,将产品名称传递给联系表单

更新时间:2023-11-30 12:24:40

解决方案 >

form.phtml 中将此代码段添加到新的隐藏字段值,它检索当前页面的URL键/值,然后它加载传递值id的模型对象:

 <?php $ productId = $ this-> getRequest() - > getParams(); 
$ model = Mage :: getModel('catalog / product');
$ _product = $ model-> load($ productId [id]);
echo $ _product-> getName(); ?>

向引用该隐藏字段值的电子邮件模板添加了新的var,
也是这部分代码将是很好的候选人被放置在助手类。


Hello from Magento beginer,i'm poor in knowledge of terms and names but i will try hard to explain this as much clear as i can.

I'm currently working on configuration of Magento Contact Form to be able to collect some data from users on Product View Page.
To be more interesting, the form will have also to send some data about the product on which page admin puts it, concretely Product name
Not all products will have this form, only products that are available for supply in different (currently unavailable) colors and sizes.
I copied Magento Contact Form to my Layout->Templates->contact->form.phtml. Embedded it in one of the products page, putting in :
Magento Admin Panel-Catalog-Menage Categories-OneOfTheProducts-edit-Custom layout update

folowing reference xml:

<reference> 
  <block type="core/template" name="customer_request"  
   template="contacts/form.phtml"/>
</reference>

Tested it and it works, still admin has to do this xml placing in many products Custom layout update text box, but that's not part of this question.
Magento form has following fields: name, email, telephone, comment.
However i need to send one more parameter to postAction action of Mage_Contacts_IndexController, and that is Product name.

QUESTION
How can i obtain Product name in Contact form from Product page, can this child block element be aware of Page content in which is being embedded? Is there some global function in Magento i can use and how to pass this parameter to controller, should i use hidden input field or else...?

Any reference or code snippet will mean a world to me

Solution

In form.phtml added this snippet of code to new hidden field value, it retrieves URL key/values of the current page, then it loads model object passing the value of "id":

<?php $productId=$this->getRequest()->getParams();   
$model = Mage::getModel('catalog/product');  
$_product = $model->load($productId["id"]);   
echo $_product->getName(); ?>

Added new var to email template referencing that hidden field value, also this part of code will be good candidate to be placed in helpers class.