Magento, passing product name to contact form

2019-04-11 18:31发布

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

2条回答
唯我独甜
2楼-- · 2019-04-11 19:13

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.

查看更多
姐就是有狂的资本
3楼-- · 2019-04-11 19:24

Another way would be to use the Magento registry:

$_product = Mage::registry('current_product');

This would have the efficiency of not having to load another database Model resulting with load of the database. See here and here.

查看更多
登录 后发表回答