Current Feature
Once we Click on Custom button "Button1" present in Original Product view page [ This original product is created with Attribute set id 1 ] , We are creating new Product Programatically with attribute set id = 2.
What i need
suppose if that original product is created with Attribute set id 3 , Than I want to create new Product Programatically with attribute set id = 4.
public function saveProduct($type, $doSave = true, $originalProduct)
{
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$product = Mage::getModel('catalog/product');
$product->setTypeId($type)
->setAttributeSetId(2);
$product->save();
}
-
How'd your products being inported into website? Is it via CSV or other else?PY Yick– PY Yick2017年06月22日 05:39:08 +00:00Commented Jun 22, 2017 at 5:39
-
@PYYick we use magmi [ csv ] and sometimes manually also we upload.... but here we are creating programtically......Baby in Magento– Baby in Magento2017年06月22日 05:40:21 +00:00Commented Jun 22, 2017 at 5:40
3 Answers 3
Tested this as standalone script, but it should also work in your controller code.
Note: this a very basic code ... product data is just copied, stock data and websites isn't set here. This has to be added in your code!
// get current product example
$product = Mage::registry('current_product');
$setId = $product->getAttributeSetId();
// attribute set mapping
// current ID => new ID
$attributSets = array(
1 => 2,
3 => 4,
);
if (in_array($setId, array_keys($attributSets))) {
$new = Mage::getModel('catalog/product');
$new->setData($product->getData()); // copy data from original product
$new->setId(null); // reset ID
$new->setSku($product->getSku() . '_NEW' );
$new->setAttributeSetId($attributSets[$setId]);
$new->save();
}
-
I updated your code as here : pastebin.com/kDHn1M62 , seems i am missing somewhere, can you please check once.....Baby in Magento– Baby in Magento2017年06月29日 11:44:50 +00:00Commented Jun 29, 2017 at 11:44
-
Would be helpful to describe whats going wrong ;) This seems wrong to me:
$setId = $product->getAttributeSetId();Replace it witth$setId = $originalProduct->getAttributeSetId();sv3n– sv3n2017年06月29日 11:58:50 +00:00Commented Jun 29, 2017 at 11:58 -
This is peRFect , Thanks a lot for your valuable time.....Baby in Magento– Baby in Magento2017年06月29日 12:08:13 +00:00Commented Jun 29, 2017 at 12:08
It's very simple to set different attribute set for different roducts in ONE CSV file. On the column attribute_set_code of the product CSV, just indicate the attribute set you want to have. Then enter the value of the product attributes, and you're done!
Here is an simplified example to illustrate how it works:
enter image description here enter image description here
======Update on 15:20 22 Jun 2017======
After I clarify with the author of this question, I'd like to deliver another approach
Get current product attribute set. On template file:
$attr_set_id = $this->getProduct()->getAttributeSetId();Modify create product link to
sitename.com/index.php/catalog/product/view/id/195869/s/2-plus-2-equals-to-5-magic-mug/?attr_set_id=<?php echo $attr_set_id; ?>Modify the function
saveProduct(). Read the attribute from URL to determine which attribute set is being used:public function saveProduct($type, $doSave = true, $originalProduct) { $attr_set_id = $this->getRequest()->getParam('attr_set_id'); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $product = Mage::getModel('catalog/product'); $product->setTypeId($type)->setAttributeSetId($attr_set_id); $product->save(); }
-
Thanks for your time , I am extremely sorry , this is not what i needed...... we need to set the attribute set when we are trying to create product programtically once we click on button in product view page....Baby in Magento– Baby in Magento2017年06月22日 06:03:52 +00:00Commented Jun 22, 2017 at 6:03
-
You can apply the same principle from me. No matter how'd you import the products, just set the
attribute_set_codein your own format (Of course, DON'T hard code it), then you can switch across product attribute sets in ONE time.PY Yick– PY Yick2017年06月22日 06:13:32 +00:00Commented Jun 22, 2017 at 6:13 -
Once we click on custom button which is present in
product page 1[ assumeMobile Product page] . we are callingcontroller codeas i mentioned in question and in that controller we are usingattribute set id = 2, in same way once we click on same custom button present inProduct page 2[assumeWatch Product page] we are calling same controlller function , so this time also new product will create with attribute set id = 2 , but here i need to create new product with attribute set id = 4.... [Mobile Productis created withattribute set id = 1& Watch Product page with 3 ]Baby in Magento– Baby in Magento2017年06月22日 06:23:22 +00:00Commented Jun 22, 2017 at 6:23 -
1Let us continue this discussion in chat.Baby in Magento– Baby in Magento2017年06月22日 07:58:07 +00:00Commented Jun 22, 2017 at 7:58
-
1For step one, this should be enough ....
$this->getProduct()->getAttributeSetId(). No extraload()needed. I'd also use an event/observer, instead of modifingsave()function. Fitting event could becatalog_product_save_before.sv3n– sv3n2017年06月22日 08:11:57 +00:00Commented Jun 22, 2017 at 8:11
Pass attribute set name to function argument
<div onclick="createP("original")">Original</div>
<div onclick="createP("new")">New</div>
Create javascript file with ajaxcall
<script>
function createP(attributesetname)
{
//ajax Call pass parameter
}
</script>
I have created magento outer script you need to move it in controllers and update some code
require_once("app/Mage.php");
Mage::app();
Mage_Core_Model_App::ADMIN_STORE_ID;
$product = Mage::getModel('catalog/product');
//$productt = Mage::getModel('catalog/product')->getCollection();
//$attributesetname = Mage::app()->getRequest()->getParam(attributesetname)
try
{
$attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection')->addFieldToFilter('attribute_set_name' ,$attributesetname )->getFirstItem();
echo $getattributesetidfromname = $attributeSetCollection->getAttributeSetId();
$product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
->setAttributeSetId(getattributesetidfromname) //ID of a attribute set named 'default'
->setTypeId('simple') //product type
->setCreatedAt(strtotime('now')) //product cr
->setSku('testsku61') //SKU
->setName('test product21') //product name
->setWeight(4.0000)
->setStatus(1) //product status (1 - enabled, 2 - disabled)
->setTaxClassId(0)
->setStockData(array(
'use_config_manage_stock' => 0, //'Use config settings' checkbox
'manage_stock'=>1, //manage stock
'min_sale_qty'=>1, //Minimum Qty Allowed in Shopping Cart
'max_sale_qty'=>2, //Maximum Qty Allowed in Shopping Cart
'is_in_stock' => 1, //Stock Availability
'qty' => 999 //qty
)
)
->save();
echo 'done';
}
catch(Exception $e){
echo "in exception" .$e->getMessage();
}
//1ドルproduct = Mage::getModel('catalog/product');
//echo count(1ドルproduct);
echo "end";
I have checked in my local server its working
-
Thanks for answer, but this didt worked for me...Baby in Magento– Baby in Magento2017年06月23日 05:31:23 +00:00Commented Jun 23, 2017 at 5:31
-
Error : SQLSTATE[23000] : Integirty constraint violation : 1452 : cant add or update child row .....Baby in Magento– Baby in Magento2017年06月23日 06:34:16 +00:00Commented Jun 23, 2017 at 6:34
-
Are you get attributesetcode name in function echo $getattributesetidfromname = $attributeSetCollection->getAttributeSetId(); die(); and test the attributeid is get or not.S.P– S.P2017年06月23日 07:30:30 +00:00Commented Jun 23, 2017 at 7:30
-
seems i am not getting attribute id......Baby in Magento– Baby in Magento2017年06月23日 07:36:32 +00:00Commented Jun 23, 2017 at 7:36
-
can you trace your code $attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection')->getCollection(); echo '<pre>'; print_r($attributeSetCollection->getData()); die();S.P– S.P2017年06月23日 07:41:34 +00:00Commented Jun 23, 2017 at 7:41