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) 
    $product = Mage::getModel('catalog/product');

  • How'd your products being inported into website? Is it via CSV or other else?
    – PY Yick
    Jun 22, 2017 at 5:39
  • @PYYick we use magmi [ csv ] and sometimes manually also we upload.... but here we are creating programtically...... 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' );
  • I updated your code as here : pastebin.com/kDHn1M62 , seems i am missing somewhere, can you please check once..... 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
    Jun 29, 2017 at 11:58
  • This is peRFect , Thanks a lot for your valuable time..... 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

  1. Get current product attribute set. On template file:

    $attr_set_id = $this->getProduct()->getAttributeSetId();
  2. Modify create product link to sitename.com/index.php/catalog/product/view/id/195869/s/2-pl‌​us-2-equals-to-5-mag‌​ic-mug/?attr_set_id=<?php echo $attr_set_id; ?>

  3. 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');
        $product = Mage::getModel('catalog/product');
  • 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.... 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_code in your own format (Of course, DON'T hard code it), then you can switch across product attribute sets in ONE time.
    – PY Yick
    Jun 22, 2017 at 6:13
  • Once we click on custom button which is present in product page 1 [ assume Mobile Product page ] . we are calling controller code as i mentioned in question and in that controller we are using attribute set id = 2, in same way once we click on same custom button present in Product page 2 [assume Watch 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 Product is created with attribute set id = 1 & Watch Product page with 3 ] Jun 22, 2017 at 6:23
  • 1
    Let us continue this discussion in chat. Jun 22, 2017 at 7:58
  • 1
    For step one, this should be enough .... $this->getProduct()->getAttributeSetId(). No extra load()needed. I'd also use an event/observer, instead of modifing save() function. Fitting event could be catalog_product_save_before.
    – sv3n
    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

    function createP(attributesetname)
        //ajax Call pass parameter 

I have created magento outer script you need to move it in controllers and update some code

$product = Mage::getModel('catalog/product');
//$productt = Mage::getModel('catalog/product')->getCollection();

//$attributesetname = Mage::app()->getRequest()->getParam(attributesetname) 

    $attributeSetCollection = Mage::getResourceModel('eav/entity_attribute_set_collection')->addFieldToFilter('attribute_set_name' ,$attributesetname )->getFirstItem();

    echo $getattributesetidfromname = $attributeSetCollection->getAttributeSetId();
        ->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
        ->setStatus(1) //product status (1 - enabled, 2 - disabled)
                       '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
        echo 'done';

catch(Exception $e){
    echo "in exception" .$e->getMessage();
//$1product = Mage::getModel('catalog/product');
//echo count($1product);
echo "end";

I have checked in my local server its working

  • Thanks for answer, but this didt worked for me... Jun 23, 2017 at 5:31
  • Error : SQLSTATE[23000] : Integirty constraint violation : 1452 : cant add or update child row ..... 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
    Jun 23, 2017 at 7:30
  • seems i am not getting attribute id...... 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
    Jun 23, 2017 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.