1

I am using Magento 1.9.1 version I have 1000 products in my store.

Is there a script or something that I can run in order to add let's say 6 extra cross-sell products to ALL 1000 products? I don't want to do it 1 by 1 manually.

2 Answers 2

1

I can recommend you an extension that can do that for you. All you have do to is to generate a csv file with all your products ids (or SKUs) on the first column and the 6 crossell ids (or SKUs) on the next 6 columns for each row.
Then you can import that into the extension and it will add the products as cross sells for all the products you specified.

here is a standalone script you can try, but please backup your database before trying it.
Create a file called cross.php on the same level as index.php with this content:

<?php
umask(0);
require_once 'app/Mage.php';
Mage::app('admin');  

$toBeRelated = array(1=>1,2=>2,3=>3,4=>4); //replace the values in here with the ids of the products you want to be cross sels for every other product. The array key is the product id, the value is the position where they should be added.

$allProductIds = Mage::getModel('catalog/product')->getCollection()
      //don't add products as cross sells for themselves.
     ->addAttributeToFilter('entity_id', array('nin' => $toBeRelated))
     ->getAllIds(); //get only the ids of all the products


$product = Mage::getModel('catalog/product');
$relation = Mage_Catalog_Model_Product_Link::LINK_TYPE_CROSSSELL;
$model = Mage::getSingleton('catalog/product_link')->setLinkTypeId($relation);

foreach ($allProductIds as $mainId) {
    $exist = array();
    $existingCollection = $model->getLinkCollection()
        ->addLinkTypeIdFilter($relation)
        ->setProduct($product->setId($mainId))
        ->addProductIdFilter()
        ->joinAttributes();
    //get the existing cross sells so you won't overwrite them
    foreach ($existingCollection as $item){
        $exist[$item->getLinkedProductId()]['position'] = $item->getPosition();
    }
    foreach ($toBeRelated as $id=>$position) {
        if (!isset($exist[$id])){
             $exists[$id]['position'] = $position;
        }
    }
    //save the cross sells.
    Mage::getResourceSingleton('catalog/product_link')->saveProductLinks(
            new Varien_Object(array('id'=>$mainId)),
            $exists,
            $relation
    );
}

Then just call your script in the browser. I haven't tested it, so watch out for syntax errors.

0

Create new file in root and run it:

<?php
umask(0);
require_once '../app/Mage.php';
Mage::app('admin');  

//1000 product id array
$idArray = array(1,2,3,4,5,....);

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

 $collection->addAttributeToSelect('*');  

 $collection->addAttributeToFilter('entity_id', array('in' => $idArray));

 foreach ($collection as $product) {

 //add crossell prouct id here instead of 1,2,3
        $param = array(
               '1'=>array(
                      'position'=>1
                ),
                '2'=>array(
                      'position'=>2
                ),
                '3'=>array(
                      'position'=>3
                )
        );
        $product->setCrossSellLinkData($param);

        $product->save();

    } 
5
  • This might screw up your database. You should never call save on a product that comes from a collection. Also it will take 1.5 eternities to call save for 1k products.
    – Marius
    Commented Nov 25, 2016 at 7:42
  • things in Magento are complicated....Never mind - I'll rather do it manually...
    – Allysin
    Commented Nov 25, 2016 at 7:44
  • @Allysin doing it manually it would be better if you divide it in four part it. 1-250, 251-500, 501-750, 751-1000. It will save your time Commented Nov 25, 2016 at 7:47
  • @marius can you please explain what is best way to achieve this Commented Nov 25, 2016 at 7:47
  • 1
    @PrashantValanda. I'm thinking about it.
    – Marius
    Commented Nov 25, 2016 at 7:48

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.