Magento在捆绑项目中添加另一个新字段
我想在bundle项的默认标题下添加新的文本列。 当我创建一个新的捆绑产品时,我需要添加一个额外的字段 我希望在默认标题下添加新的文本字段以添加说明。 请帮忙。任何帮助都将不胜感激。 为了在每个捆绑选项中添加新的描述字段,您需要创建一个模块。简而言之,您的模块将实现以下功能 创建一个新表以保存捆绑选项的描述字段 当产品保存时,它将用必要的更改更新我们的表 考虑到这一点,让我们创建我们的模块。让我们的模块成为Programmerrkt_Customfieldinbundle 位置:app/code/local/Programmerrkt/Customfieldinbundle/etc/config.xml 现在我们需要在管理端显示我们的描述字段。为此,我们需要编辑一个块,以呈现捆绑选项。块是app/code/core/Mage/Bundle/block/Adminhtml/Catalog/Product/Edit/Tab/Bundle/Option.php。我们需要更改此文件中的两个方法**只显示主要部分Magento在捆绑项目中添加另一个新字段,magento,magento-1.7,magento-1.4,Magento,Magento 1.7,Magento 1.4,我想在bundle项的默认标题下添加新的文本列。 当我创建一个新的捆绑产品时,我需要添加一个额外的字段 我希望在默认标题下添加新的文本字段以添加说明。 请帮忙。任何帮助都将不胜感激。 为了在每个捆绑选项中添加新的描述字段,您需要创建一个模块。简而言之,您的模块将实现以下功能 创建一个新表以保存捆绑选项的描述字段 当产品保存时,它将用必要的更改更新我们的表 考虑到这一点,让我们创建我们的模块。让我们的模块成为Programmerrkt_Customfieldinbundle 位置:app/code
<?php
----------
/**
* Bundle option renderer class constructor
*
* Sets block template and necessary data
*/
public function __construct()
{
$this->setTemplate('programmerrkt/customfieldinbundle/bundle/product/edit/bundle/option.phtml');
//$this->setTemplate('bundle/product/edit/bundle/option.phtml');
$this->setCanReadPrice(true);
$this->setCanEditPrice(true);
}
------
/**
* Retrieve list of bundle product options
*
* @return array
*/
public function getOptions()
{
if (!$this->_options) {
$customCollection = Mage::getModel('customfieldinbundle/optiondescription')->getCollection();
$store_id = $this->getProduct()->getData('store_id');
//sets default values of description module
$empty = FALSE;
if(count($customCollection)){
$description_id = (int)(count($customCollection)+1);
}
else{
$empty = TRUE;
$description_id = 1;
}
$description_content = "";
$this->getProduct()->getTypeInstance(true)->setStoreFilter($this->getProduct()->getStoreId(),
$this->getProduct());
$optionCollection = $this->getProduct()->getTypeInstance(true)->getOptionsCollection($this->getProduct());
$selectionCollection = $this->getProduct()->getTypeInstance(true)->getSelectionsCollection(
$this->getProduct()->getTypeInstance(true)->getOptionsIds($this->getProduct()),
$this->getProduct()
);
$this->_options = $optionCollection->appendSelections($selectionCollection);
foreach ($this->_options as $option) {
//gets each option's id
$option_id = $option->getData('option_id');
//loop through module collection
if($empty === FALSE){
foreach($customCollection as $description){
//finds the entry corresponds to option id, if exist
if($description['option_id']==$option_id && $description['store_id']==$store_id){
$description_id = (int)$description['description_id'];
$description_content = $description['description'];
break;
}
}
//adds our new datas to option
$option->addData(array('desc_id'=> $description_id, 'description' => $description_content, 'desc_new'=> 'no'));
}
else {
$description_id = (int)$description['description_id'];
$description_content = $description['description'];
//adds our new datas to option
$option->addData(array('desc_id'=> $description_id, 'description' => $description_content,'desc_new'=> 'yes'));
$description_id++;
}
}
if ($this->getCanReadPrice() === false) {
foreach ($this->_options as $option) {
if ($option->getSelections()) {
foreach ($option->getSelections() as $selection) {
$selection->setCanReadPrice($this->getCanReadPrice());
$selection->setCanEditPrice($this->getCanEditPrice());
}
}
}
}
}
return $this->_options;
}
------
这里我们所做的是,我们在标题字段的正下方添加了一个文本区域
现在是最重要的文件。我们的观察员。这对我们的模块来说就是一个诀窍。它通过检查不同的标准来保存我们的数据
<?php
/**
* Bundle Products Observer
*
* @category Mage
* @package Programmerrkt_Customfieldinbundle
* @author Programmer_RKT
*/
class Programmerrkt_Customfieldinbundle_Model_Observer
{
/**
* Setting Bundle Items Data to product for father processing
*
* @param Varien_Object $observer
* @return Programmerrkt_Customfieldbundle_Model_Observer
*/
public function SaveDescriptionAfterProductSave($observer)
{
$model = Mage::getModel('customfieldinbundle/optiondescription'); // loads module's model
$collection = $model->getCollection();
$product = $observer->getEvent()->getProduct(); //get current product
//$optionCollection use to get ids of new options.
//$bundleOption does not contain option_id of new option
//$optionCollection does not contain module's field values. This we can obtain from $bundleOptions
$optionCollection = $product->getTypeInstance(TRUE)->getOptionsCollection($product);
$bundleOptions = $product->getBundleOptionsData(); //@data : array which conatains array
//get all option data such as title, description, type etc.
if(!empty($bundleOptions)){
$store_id = (int)$product->getData('store_id'); //@data : integer
if($store_id !=0) {
//if not default store
foreach ($bundleOptions as $option) {
$option_id = (int)$option['option_id'];
$description_id = (int)$option['description_id'];
$description_new = $option['description_new'];
//use to set option_id for new options in our module
if($option_id <= 0 || is_null($option_id)){
foreach ($optionCollection as $new) {
if($new['type'] == $option['type'] && ( $new['title'] == $option['title'] || $new['default_title'] == $option['title'] )){
$option_id = $new['option_id'];
}
}
}
$data = array(
'option_id' => $option_id,
'store_id' => $store_id,
'description' => $option['description']
);
//description id exist means already there is an entry
if($description_id >= 0 || $description_new=='no'){
$model->load($description_id);
$model->addData($data);
$model->save();
}
else{
$model->setData($data);
$model->save();
}
}
}
else {
//if default store
foreach ($bundleOptions as $option) {
$option_id = (int)$option['option_id'];
$description_id = (int)$option['description_id'];
$description_new = $option['description_new'];
//use to set option_id for new options in our module
if($option_id <= 0 || is_null($option_id)){
foreach ($optionCollection as $new) {
if($new['type'] == $option['type'] && ( $new['title'] == $option['title'] || $new['default_title'] == $option['title'] )){
$option_id = $new['option_id'];
}
}
}
$data = array(
'option_id' => $option_id,
'store_id' => 0,
'description' => $option['description']
);
//description id exist means already there is an entry
if($description_id >= 0 || $description_new=='no'){
$model->load($description_id);
$model->addData($data);
$model->save();
}
else{
$model->setData($data);
$model->save();
}
}
}
}
return $this;
}
}
注意:我只描述了我们模块的重要文件。我想,你们都知道如何在Magento中设置基本模块
就这样。你准备好出发了。清除缓存并加载捆绑产品。您可以在捆绑选项的标题字段下方看到一个文本区域
如果有任何疑问,请随时询问。为了在每个捆绑选项中添加新的描述字段,您需要创建一个模块。简而言之,您的模块将实现以下功能
创建一个新表以保存捆绑选项的描述字段
当产品保存时,它将用必要的更改更新我们的表
考虑到这一点,让我们创建我们的模块。让我们的模块成为Programmerrkt_Customfieldinbundle
位置:app/code/local/Programmerrkt/Customfieldinbundle/etc/config.xml
现在我们需要在管理端显示我们的描述字段。为此,我们需要编辑一个块,以呈现捆绑选项。块是app/code/core/Mage/Bundle/block/Adminhtml/Catalog/Product/Edit/Tab/Bundle/Option.php。我们需要更改此文件中的两个方法**只显示主要部分
<?php
----------
/**
* Bundle option renderer class constructor
*
* Sets block template and necessary data
*/
public function __construct()
{
$this->setTemplate('programmerrkt/customfieldinbundle/bundle/product/edit/bundle/option.phtml');
//$this->setTemplate('bundle/product/edit/bundle/option.phtml');
$this->setCanReadPrice(true);
$this->setCanEditPrice(true);
}
------
/**
* Retrieve list of bundle product options
*
* @return array
*/
public function getOptions()
{
if (!$this->_options) {
$customCollection = Mage::getModel('customfieldinbundle/optiondescription')->getCollection();
$store_id = $this->getProduct()->getData('store_id');
//sets default values of description module
$empty = FALSE;
if(count($customCollection)){
$description_id = (int)(count($customCollection)+1);
}
else{
$empty = TRUE;
$description_id = 1;
}
$description_content = "";
$this->getProduct()->getTypeInstance(true)->setStoreFilter($this->getProduct()->getStoreId(),
$this->getProduct());
$optionCollection = $this->getProduct()->getTypeInstance(true)->getOptionsCollection($this->getProduct());
$selectionCollection = $this->getProduct()->getTypeInstance(true)->getSelectionsCollection(
$this->getProduct()->getTypeInstance(true)->getOptionsIds($this->getProduct()),
$this->getProduct()
);
$this->_options = $optionCollection->appendSelections($selectionCollection);
foreach ($this->_options as $option) {
//gets each option's id
$option_id = $option->getData('option_id');
//loop through module collection
if($empty === FALSE){
foreach($customCollection as $description){
//finds the entry corresponds to option id, if exist
if($description['option_id']==$option_id && $description['store_id']==$store_id){
$description_id = (int)$description['description_id'];
$description_content = $description['description'];
break;
}
}
//adds our new datas to option
$option->addData(array('desc_id'=> $description_id, 'description' => $description_content, 'desc_new'=> 'no'));
}
else {
$description_id = (int)$description['description_id'];
$description_content = $description['description'];
//adds our new datas to option
$option->addData(array('desc_id'=> $description_id, 'description' => $description_content,'desc_new'=> 'yes'));
$description_id++;
}
}
if ($this->getCanReadPrice() === false) {
foreach ($this->_options as $option) {
if ($option->getSelections()) {
foreach ($option->getSelections() as $selection) {
$selection->setCanReadPrice($this->getCanReadPrice());
$selection->setCanEditPrice($this->getCanEditPrice());
}
}
}
}
}
return $this->_options;
}
------
这里我们所做的是,我们在标题字段的正下方添加了一个文本区域
现在是最重要的文件。我们的观察员。这对我们的模块来说就是一个诀窍。它通过检查不同的标准来保存我们的数据
<?php
/**
* Bundle Products Observer
*
* @category Mage
* @package Programmerrkt_Customfieldinbundle
* @author Programmer_RKT
*/
class Programmerrkt_Customfieldinbundle_Model_Observer
{
/**
* Setting Bundle Items Data to product for father processing
*
* @param Varien_Object $observer
* @return Programmerrkt_Customfieldbundle_Model_Observer
*/
public function SaveDescriptionAfterProductSave($observer)
{
$model = Mage::getModel('customfieldinbundle/optiondescription'); // loads module's model
$collection = $model->getCollection();
$product = $observer->getEvent()->getProduct(); //get current product
//$optionCollection use to get ids of new options.
//$bundleOption does not contain option_id of new option
//$optionCollection does not contain module's field values. This we can obtain from $bundleOptions
$optionCollection = $product->getTypeInstance(TRUE)->getOptionsCollection($product);
$bundleOptions = $product->getBundleOptionsData(); //@data : array which conatains array
//get all option data such as title, description, type etc.
if(!empty($bundleOptions)){
$store_id = (int)$product->getData('store_id'); //@data : integer
if($store_id !=0) {
//if not default store
foreach ($bundleOptions as $option) {
$option_id = (int)$option['option_id'];
$description_id = (int)$option['description_id'];
$description_new = $option['description_new'];
//use to set option_id for new options in our module
if($option_id <= 0 || is_null($option_id)){
foreach ($optionCollection as $new) {
if($new['type'] == $option['type'] && ( $new['title'] == $option['title'] || $new['default_title'] == $option['title'] )){
$option_id = $new['option_id'];
}
}
}
$data = array(
'option_id' => $option_id,
'store_id' => $store_id,
'description' => $option['description']
);
//description id exist means already there is an entry
if($description_id >= 0 || $description_new=='no'){
$model->load($description_id);
$model->addData($data);
$model->save();
}
else{
$model->setData($data);
$model->save();
}
}
}
else {
//if default store
foreach ($bundleOptions as $option) {
$option_id = (int)$option['option_id'];
$description_id = (int)$option['description_id'];
$description_new = $option['description_new'];
//use to set option_id for new options in our module
if($option_id <= 0 || is_null($option_id)){
foreach ($optionCollection as $new) {
if($new['type'] == $option['type'] && ( $new['title'] == $option['title'] || $new['default_title'] == $option['title'] )){
$option_id = $new['option_id'];
}
}
}
$data = array(
'option_id' => $option_id,
'store_id' => 0,
'description' => $option['description']
);
//description id exist means already there is an entry
if($description_id >= 0 || $description_new=='no'){
$model->load($description_id);
$model->addData($data);
$model->save();
}
else{
$model->setData($data);
$model->save();
}
}
}
}
return $this;
}
}
注意:我只描述了我们模块的重要文件。我想,你们都知道如何在Magento中设置基本模块
就这样。你准备好出发了。清除缓存并加载捆绑产品。您可以在捆绑选项的标题字段下方看到一个文本区域
如果有任何疑问,请随时提问。请不要问两次相同的问题,您可以随时编辑您的原始问题。这必须是您的原始问题。对社区:我无法从中使用标志选项mobile@KrishnaSunuwar请帮我解决这个问题请不要问同样的问题两次,你可以随时编辑你的原始问题这必须是你的原始问题。对社区:我无法从中使用标志选项mobile@KrishnaSunuwar请帮我解决这个问题我把你说的都安排好了,但我得到:致命错误:C:\wamp2.5\www\app\code\core\Mage\core\Model\Resource\Setup.php第234行找不到类“Programmerrkt\u Customfieldinbundle\u Model\u Resource\Setup.php”,请给出建议。@Rajeev K Tomy你能给我完整的代码示例吗,因为有一些文件丢失了,如db file等谢谢。@Rajeev你为什么要为它添加新表这个目的?你为什么不在目录中添加一个新的列?产品?捆绑?选项?并重用现有的model triad?我设置了你说的所有内容,但我得到:致命错误:C:\wamp2.5\www\app\code\core\Mage\core\Model\Resource\Setup.php第234行找不到类“Programmerrkt\u Customfieldinbundle\u Model\u Resource\Setup.php”,请给出建议。@Rajeev K Tomy你能给我完整的代码示例吗,因为有一些文件丢失了,如db file等谢谢。@Rajeev你为什么要为它添加新表这个目的?为什么不在catalog\u product\u bundle\u option\u值中添加一个新列并重用现有的模型空间坐标轴?
<?php
/**
* Bundle Products Observer
*
* @category Mage
* @package Programmerrkt_Customfieldinbundle
* @author Programmer_RKT
*/
class Programmerrkt_Customfieldinbundle_Model_Observer
{
/**
* Setting Bundle Items Data to product for father processing
*
* @param Varien_Object $observer
* @return Programmerrkt_Customfieldbundle_Model_Observer
*/
public function SaveDescriptionAfterProductSave($observer)
{
$model = Mage::getModel('customfieldinbundle/optiondescription'); // loads module's model
$collection = $model->getCollection();
$product = $observer->getEvent()->getProduct(); //get current product
//$optionCollection use to get ids of new options.
//$bundleOption does not contain option_id of new option
//$optionCollection does not contain module's field values. This we can obtain from $bundleOptions
$optionCollection = $product->getTypeInstance(TRUE)->getOptionsCollection($product);
$bundleOptions = $product->getBundleOptionsData(); //@data : array which conatains array
//get all option data such as title, description, type etc.
if(!empty($bundleOptions)){
$store_id = (int)$product->getData('store_id'); //@data : integer
if($store_id !=0) {
//if not default store
foreach ($bundleOptions as $option) {
$option_id = (int)$option['option_id'];
$description_id = (int)$option['description_id'];
$description_new = $option['description_new'];
//use to set option_id for new options in our module
if($option_id <= 0 || is_null($option_id)){
foreach ($optionCollection as $new) {
if($new['type'] == $option['type'] && ( $new['title'] == $option['title'] || $new['default_title'] == $option['title'] )){
$option_id = $new['option_id'];
}
}
}
$data = array(
'option_id' => $option_id,
'store_id' => $store_id,
'description' => $option['description']
);
//description id exist means already there is an entry
if($description_id >= 0 || $description_new=='no'){
$model->load($description_id);
$model->addData($data);
$model->save();
}
else{
$model->setData($data);
$model->save();
}
}
}
else {
//if default store
foreach ($bundleOptions as $option) {
$option_id = (int)$option['option_id'];
$description_id = (int)$option['description_id'];
$description_new = $option['description_new'];
//use to set option_id for new options in our module
if($option_id <= 0 || is_null($option_id)){
foreach ($optionCollection as $new) {
if($new['type'] == $option['type'] && ( $new['title'] == $option['title'] || $new['default_title'] == $option['title'] )){
$option_id = $new['option_id'];
}
}
}
$data = array(
'option_id' => $option_id,
'store_id' => 0,
'description' => $option['description']
);
//description id exist means already there is an entry
if($description_id >= 0 || $description_new=='no'){
$model->load($description_id);
$model->addData($data);
$model->save();
}
else{
$model->setData($data);
$model->save();
}
}
}
}
return $this;
}
}