Как добавить еще 6 товаров для перекрестной продажи ко ВСЕМ (1000) товарам одновременно?


Я использую версию Magento 1.9.1 У меня в магазине 1000 товаров.

Есть ли сценарий или что-то, что я могу запустить, чтобы добавить, скажем, 6 дополнительных продуктов для перекрестной продажи ко ВСЕМ 1000 продуктам? Я не хочу делать это 1 на 1 вручную.

Author: Prashant Valanda, 2016-11-25

2 answers

Я могу порекомендовать вам расширение , которое может сделать это за вас. Все, что вам нужно сделать, это создать csv-файл со всеми идентификаторами ваших продуктов (или артикулами) в первом столбце и 6 идентификаторами crossell (или артикулами) в следующих 6 столбцах для каждой строки.
Затем вы можете импортировать это в расширение, и оно добавит продукты в качестве перекрестных продаж для всех указанных вами продуктов.

Вот отдельный скрипт, который вы можете попробовать, но, пожалуйста, сделайте резервную копию своей базы данных, прежде чем попробовать его.
Создавать файл с именем cross.php на том же уровне, что и index.php, с таким содержимым:

<?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
    );
}

Затем просто вызовите свой скрипт в браузере. Я его еще не тестировал, так что следите за синтаксическими ошибками.

 1
Author: Marius, 2016-11-25 08:05:14

Создайте новый файл в корневом каталоге и запустите его:

<?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();

    } 
 0
Author: Prashant Valanda, 2016-11-25 07:40:23