0

Hey guys...

I've been working on an export script which later on should fit for MAGMI import. It will/should include category and position (which I already worked out) and associated products of grouped products which is where I got stuck with now...

Code so far looks like this:

<?php
define('SAVE_FEED_LOCATION','export.csv');
set_time_limit(1800);
require_once 'app/Mage.php';
Mage::app('default');
try{
    $handle = fopen(SAVE_FEED_LOCATION, 'w');

$heading = array(
                 '"id"',
                 '"type"',
                 '"sku"',
                 '"grouped_skus"',
                 );
$feed_line=implode(",", $heading)."\r\n";
fwrite($handle, $feed_line);

$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status', 1);
$products->addAttributeToSelect('*');
$products->getSelect()->order(new Zend_Db_Expr('FIELD(e.entity_id, ' . implode(',', $productIds).')'));
$prodIds=$products->getAllIds();

$model = Mage::getModel('catalog/product');

foreach($prodIds as $productId) {


        $product = $model->load($productId);
        $product_id = $product->getId();
        $product_data = array();

        $product_data['id'] = $product_id;
        $product_data['type'] = $product->getTypeId();
        $product_data['sku'] = $product->getSku();

        $associated = "";
        #get associated skus
        if ($product->getTypeId() == 'grouped'){
            $associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);

            foreach ($associatedProducts as $associatedProduct) {
                $associated .= $associatedProduct->getSku().',';
            }

        };
        $product_data['grouped_skus'] = $associated;

        $feed_line = implode(",", $product_data)."\r\n";
        fwrite($handle, $feed_line);
        fflush($handle);


}

fclose($handle);
}
catch(Exception $e){
    die($e->getMessage());
}
?>

I am not an expert in php as you can see.
Problem which appears is that the export of associated product only uses the first product of the product collection and fills the grouped_sku column with all the same entries... I think that is because those are not loaded by product id but by the model something?
But I can't see anything to make it work...
Any ideas in this?

0

declare you model inside foreach loop

foreach($prodIds as $productId) {

 $model = Mage::getModel('catalog/product');

 ----------------------------------

}

  • Thank you very much. I was thinking about that this morning also... but couldn't think of this as the solution ;). Works like a charm! – Thomas Klosinsky Sep 6 '16 at 10:48
  • and how to not get the last "," in $associated? – Thomas Klosinsky Sep 6 '16 at 10:49
  • You can try like this foreach ($associatedProducts as $associatedProduct) { $associated = $associated ? $associated .','.$associatedProduct->getSku():$associatedProduct->getSku(); } – Balaji Setti Sep 6 '16 at 13:35
0

I needed the same thing so I fixed and improved your code You don't need to load each product individually they are already included in the collection

define('SAVE_FEED_LOCATION', 'export.csv');
set_time_limit(1800);
require_once '../app/Mage.php';
ini_set('display_errors',1);
Mage::app('admin');
try {
    $handle = fopen(SAVE_FEED_LOCATION, 'w');
    $heading = array(
        '"id"',
        '"type"',
        '"sku"',
        '"grouped_skus"',
    );
    $feed_line = implode(",", $heading) . "\r\n";
    fwrite($handle, $feed_line);
    $products = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToFilter('type_id', array('eq' => 'grouped'))//select only grouped product
        ->addWebsiteFilter(1)//select only product of website id 2
        ->addAttributeToFilter('status', 1)//select only enabled product
        ->addAttributeToSelect('*');
    //$products->getSelect()->limit(10);//limit product collection for test
    $count = 0;
    foreach ($products as $product) {
        $product_data = array();
        $product_data['id'] = $product->getId();
        $product_data['type'] = $product->getTypeId();
        $product_data['sku'] = $product->getSku();
        $associated = array();
        #get associated skus
        $associatedProducts = $product->getTypeInstance(true)->getAssociatedProducts($product);
        foreach ($associatedProducts as $associatedProduct) {
            $associated[] = $associatedProduct->getSku();
        }
        $product_data['grouped_skus'] = '"'. implode(",", $associated).'"';
        $feed_line = implode(",", $product_data) . "\r\n";
        print_r($feed_line);
        fwrite($handle, $feed_line);
        fflush($handle);
        $count++;
    }
    fclose($handle);
    echo $count ." exported products";
} catch (Exception $e) {
    die($e->getMessage());
}

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.