我创建了一个模块,其中管理名称中有一个选项卡创建样本产品,当我点击该选项卡时,请求转到控制器 它创造了样品 这是我的自定义模块控制器代码
public function createsamplesAction(){
// $rootCategoryId = Mage::app()->getStore()->getRootCategoryId();
$cat = Mage::getModel('catalog/category')->load(2);
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid):
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getName() =='T-SHIRT' || $_category->getName() =='HOODIE' || $_category->getName() =='SWEATER'):
$categoryid[] = $_category->getId();
endif;
endforeach;
$samplepro1 = 'ocodepro1'; //product name
$samplepro2 = 'ocodepro2'; //product name
$samplepro3 = 'ocodepro3';
$model = Mage::getModel('catalog/product'); //getting product model
$collection = $model->getCollection(); //products collection
foreach ($collection as $product) //loop for getting products
{
$model->load($product->getId());
$sku = $model->getSku();
if($sku == $samplepro1 || $sku == $samplepro2 || $sku == $samplepro3)
{
$skuArray[]=$model->getSku();
}
}
if(!empty($skuArray)){
if($skuArray[0]=='ocodepro1' && $skuArray[1]=='ocodepro2' && $skuArray[2]=='ocodepro3'){
$message = $this->__('Error ! You have Already Created Sample Products');
Mage::getSingleton('core/session') ->addError($message);
}
}
else{
for($i=0; $i<3; $i++){
$product = Mage::getModel('catalog/product');
if($i==0){
$product->setSku('ocodepro1');
$product->setName('T-SHIRT');
$product->setPrice(150);
$product->setCategoryIds(array($categoryid[0]));
}
else if($i==1){
$product->setSku('ocodepro2');
$product->setName('HOODIE');
$product->setPrice(300);
$product->setCategoryIds(array($categoryid[1]));
}
else if($i==2){
$product->setSku('ocodepro3');
$product->setName('SWEATER');
$product->setPrice(300);
$product->setCategoryIds(array($categoryid[2]));
}
$product->setDescription('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap');
$product->setShortDescription("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap");
//----- company address and map
$product->setTypeId('simple');
$product->setAttributeSetId(4); // need to look this up
//$product->setCategoryIds(array($categoryid)); // need to look these up
$product->setWeight(1.0);
$product->setTaxClassId(1); // taxable goods
$product->setVisibility(1); // catalog, search
$product->setStatus(1); // enabled
$productQty = '1000';
//$stockData=$product->getStockData();
$stockData['qty']=$productQty;
$stockData['is_in_stock']=1;
$stockData['manage_stock']=1;
$stockData['use_config_manage_stock']=0;
$product->setStockData($stockData);
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
$product->save();
$_proid= $product->save();
$this->_createOptions($_proid->getId());
}
if($product->save()){
$message = $this->__('Success ! Sample Products Plans have been Created.');
Mage::getSingleton('core/session') ->addSuccess($message);
}
else{
$message = $this->__('Error ! There was an Error Please Try Again Later.');
Mage::getSingleton('core/session') ->addError($message);
}
}//
$url= Mage::helper('adminhtml')->getUrl('adminhtml/catalog_product');
$this->_redirectUrl($url);
}
protected function _createOptions($id){
$_loadproduct = Mage::getModel('catalog/product')->load($id);
$options = array(
array(
'title' => 'Front View',
'type' => 'field',
'is_require' => 0,
'sort_order' => 1,
'is_delete' => '',
'previous_type' => '',
'previous_group' => '',
'price' => '0.00',
'price_type' => 'fixed',
'sku' => ''
),
array(
'title' => 'Back View',
'type' => 'field',
'is_require' => 0,
'sort_order' => 2,
'is_delete' => '',
'previous_type' => '',
'previous_group' => '',
'price' => '0.00',
'price_type' => 'fixed',
'sku' => ''
),
array(
'title' => 'Size & Quantity',
'type' => 'area',
'is_require' => 0,
'sort_order' => 3,
'is_delete' => '',
'previous_type' => '',
'previous_group' => '',
'price' => '0.00',
'price_type' => 'fixed',
'sku' => ''
),
array(
'title' => 'Name & Number',
'type' => 'area',
'is_require' => 0,
'sort_order' => 4,
'is_delete' => '',
'previous_type' => '',
'previous_group' => '',
'price' => '0.00',
'price_type' => 'fixed',
'sku' => ''
));
$_loadproduct->setProductOptions($options);
$_loadproduct->setCanSaveCustomOptions(true);
$_loadproduct->save();
}
此代码创建了具有不同sku的产品 但是当我点击产品网格中的第一个产品并检查自定义选项时。它显示了四个选项,我想要的,但当我点击第二个或第三个产品并检查他们的自定义选项时,它会显示重复的自定义选项 产品2(HOODIE)显示两次重复的自定义选项和产品3 SWEATER显示三次重复自定义选项像这样
我希望每个产品的每个自定义属性只能这样一次。
请帮助..
答案 0 :(得分:0)
在您的函数createsamplesAction()
中,您正在为产品分配$options
变量。
$product->setProductOptions($options);
您的$options
变量在哪里。
如果你有。请在for循环之前将其声明为数组。
$options = array();
for($i=0; $i<3; $i++){
//your code here
}
希望这会有所帮助。
答案 1 :(得分:0)
当你说第一个产品没问题,但第二个产品重复两次,第三个产品重复三次时,这意味着$options
正在每个周期建立。我建议您在$options = array();
函数中添加_createOptions($id)
,如下所示:
protected function _createOptions($id){
$options = array();
$_loadproduct = Mage::getModel('catalog/product')->load($id);
...................
...................
}
希望这能解决重复问题。