1

i know we can use duplicate option. but i don't want to create another new product.

Product 1 -> Product 2

Product 1 and Product 2 are two different products and now i need to copy Product 1 data and its attributes to Product 2 without chaining Product 2 id.

0

sunel,It too complex to do this type of works.I am suggesting to export fields of product A from Dataflow export or Import/export section of Admin>System.Then using import that data to product B and note that on the export sheet put product B sku instead of A's Sku.

  • Hi Amit i managed to achieve it u can check my answer may its correct – sunel Mar 21 '15 at 13:14
0

I was able to do this by the following

<?php

class Copier_Model_Product extends Mage_Core_Model_Abstract {

/**
 * copy the arrtibutes and its value of product  
 * 
 * @param Array $data 
 * 
 * @return void
 * @throws Mage_Core_Exception
 * 
 */ 
public function copy($data) {

        $productModel = Mage::getModel('catalog/product');
        $currentProduct = $productModel->load($data['productId']);

        $productModel = Mage::getModel('catalog/product');
        $copyProduct = $productModel->load($productModel->getIdBySku($data['sku']));

        $currentSku = $currentProduct->getSku();

        $restrictedAttributes = explode(',', Mage::getStoreConfig('copier/restricted/attributes'));

        $copyData = $this->_removeRestrictedAttributes($copyProduct->getData(), $restrictedAttributes);

        $currentProduct
                ->setTypeId($copyProduct->getTypeId())
                ->setAttributeSetId($copyProduct->getAttributeSetId())
                ->setStoreId($copyProduct->getStoreId());

        $productModel = Mage::getModel('catalog/product');
        $productId = $currentProduct->save()->getId();
        $currentProduct = $productModel->load($productId);

        $currentProduct
                ->setData($copyData)
                ->setStoreId($copyProduct->getStoreId())
                ->setSku($currentSku)
                ->setCategoryIds($copyProduct->getCategoryIds())
                ->setWebsiteIDs($copyProduct->getWebsiteIDs());

        //$currentProduct->save();

        /* Prepare Related*/
        $data = array();
        $copyProduct->getLinkInstance()->useRelatedLinks();
        $attributes = array();
        foreach ($copyProduct->getLinkInstance()->getAttributes() as $_attribute) {
            if (isset($_attribute['code'])) {
                $attributes[] = $_attribute['code'];
            }
        }
        foreach ($copyProduct->getRelatedLinkCollection() as $_link) {
            $data[$_link->getLinkedProductId()] = $_link->toArray($attributes);
        }
        $currentProduct->setRelatedLinkData($data);

        /* Prepare UpSell*/
        $data = array();
        $copyProduct->getLinkInstance()->useUpSellLinks();
        $attributes = array();
        foreach ($copyProduct->getLinkInstance()->getAttributes() as $_attribute) {
            if (isset($_attribute['code'])) {
                $attributes[] = $_attribute['code'];
            }
        }
        foreach ($copyProduct->getUpSellLinkCollection() as $_link) {
            $data[$_link->getLinkedProductId()] = $_link->toArray($attributes);
        }
        $currentProduct->setUpSellLinkData($data);

        /* Prepare Cross Sell */
        $data = array();
        $copyProduct->getLinkInstance()->useCrossSellLinks();
        $attributes = array();
        foreach ($copyProduct->getLinkInstance()->getAttributes() as $_attribute) {
            if (isset($_attribute['code'])) {
                $attributes[] = $_attribute['code'];
            }
        }
        foreach ($copyProduct->getCrossSellLinkCollection() as $_link) {
            $data[$_link->getLinkedProductId()] = $_link->toArray($attributes);
        }
        $currentProduct->setCrossSellLinkData($data);

        /* Prepare Grouped */
        $data = array();
        $copyProduct->getLinkInstance()->useGroupedLinks();
        $attributes = array();
        foreach ($copyProduct->getLinkInstance()->getAttributes() as $_attribute) {
            if (isset($_attribute['code'])) {
                $attributes[] = $_attribute['code'];
            }
        }
        foreach ($copyProduct->getGroupedLinkCollection() as $_link) {
            $data[$_link->getLinkedProductId()] = $_link->toArray($attributes);
        }
        $currentProduct->setGroupedLinkData($data);

        $currentProduct->save();

        $fromProduct = $copyProduct->getId();
        $toProduct = $currentProduct->getId();

        $this->_deleteCustomOption($toProduct);

        $this->_changeStock($fromProduct, $toProduct);
        $this->_saveCustomOption($fromProduct, $toProduct);

        $copyProduct->getResource()->duplicate($fromProduct, $toProduct);

}

/**
 * remove rested attributes for the array
 * 
 * @param Array $copyData
 * @param Array $restrictedAttributes
 *  
 * @return Array;
 */
protected function _removeRestrictedAttributes($copyData, $restrictedAttributes){

    foreach ($restrictedAttributes as $_attributes) {
        unset($copyData[$_attributes]);
    }

    return $copyData;   
}

/**
 * copy the stock status 
 * 
 * @param Int $fromProduct - Id of the product used for copying data
 * @param Int $toProduct - Id of the product used to copy data
 * 
 * @return void
 * @throws Mage_Core_Exception
 * 
 */
private function _changeStock($fromProduct, $toProduct) {

    $fromStockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($fromProduct);
    $toStockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($toProduct);
    if (!$toStockItem->getId()) {
        $toStockItem->setData('product_id', $toProduct);
        $toStockItem->setData('stock_id', $fromStockItem->getData('stock_id'));
    }

    $toStockItem->setData('is_in_stock', $fromStockItem->getData('is_in_stock'));
    $toStockItem->setData('manage_stock', $fromStockItem->getData('manage_stock'));

    try {
        $toStockItem->save();
    } catch (Exception $e) {
        throw new Mage_Core_Exception($e->getMessage());
    }
}

/**
 * delete the custom option of the gien product  
 * 
 * @param Int $deleteId - Id of the product used to delete the option 
 * 
 * @return void
 * 
 */ 
private function _deleteCustomOption($deleteId) {
    $catalogModel = Mage::getModel('catalog/product');
    $opt = $catalogModel->load($deleteId);

    foreach ($opt->getOptions() as $o) {
        $o->delete();
    }

    $opt->setHasOptions(0)->save();

}

/**
 * copy the custome option
 * 
 * @param Int $fromProduct - Id of the product used for copying option
 * @param Int $toProduct - Id of the product used to copy option
 * 
 * @return void
 * 
 */
private function _saveCustomOption($fromProduct, $toProduct) {

    $copyProduct = Mage::getModel('catalog/product')->load($fromProduct);
    $currentProduct = Mage::getModel('catalog/product')->load($toProduct);

    if($copyProduct->getHasOptions()){
        $copyProduct->getOptionInstance()->duplicate($fromProduct, $toProduct);
        $currentProduct->setHasOptions(1)->save();
    }
 }
}

i added a new tab in the admin product grid with the id "copier" and the add a input with name "copier"

used this function in the a button onclick

   (function(){new Copyer(document.getElementById('copier'),[productId]);})();

    (function(w){
        var log = function() {
            console.log(arguments);
        };
        Event.observe(w, 'load', function(){
            $('product_edit_form').insert({
              top: $('product_info_tabs_copier_content').writeAttribute('id','copier_content').show()
            }); 
            $('product_info_tabs_copier').up().remove();
        });

        var Copyer = Class.create();
        Copyer.prototype = {
            initialize: function(data,productId) {
                this.field = $(data);
                this.productId = productId;
                this.copy();
            },
            copy: function() {
                this.field.addClassName('required-entry');
                if(Validation.validate(this.field)){
                    var r = confirm("Are you sure to copy data from "+this.field.getValue() 
                        + ".\nThis process is irreversible."
                    );
                    if (r == true) {
                        this.updateAttribute(this.field.getValue(),this.productId);
                    }

                }
                this.field.removeClassName('required-entry');
            },
            updateAttribute: function(sku,productId) {

                 var url = 'helper("adminhtml")->getUrl("copier/process/index"); ?>';
                 var data = {
                    'sku':sku,
                    'productId':productId
                 };
                 new Ajax.Request(url, {
                    method: 'post',
                    parameters: {
                        data: Object.toJSON(data)
                    },
                    onFailure: function() {
                        alert('An error occurred while saving the data.');
                    },
                    onSuccess: function(response) {
                        data = response.responseText.evalJSON();
                        if (data.error) {
                            alert("Error : " + data.message);
                        } else {
                            w.location.reload();
                        }
                    }
                });
            }
        };
        w.Copyer = Copyer;
    })(window);
  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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