Skip to content

Instantly share code, notes, and snippets.

@peppeocchi
Last active January 21, 2020 20:57
Show Gist options
  • Save peppeocchi/db97f1e1aee1159842a8 to your computer and use it in GitHub Desktop.
Save peppeocchi/db97f1e1aee1159842a8 to your computer and use it in GitHub Desktop.
Magento Export configurable products ready for MAGMI import (columns sku, configurable_attributes, simples_skus)
<?php
/**
* This file should be placed into the `shell` folder
* You should need a table in your database with fields:
* - sku
* - configurable_attributes
* - simples_skus
*
*/
require_once 'abstract.php';
/**
* Magento Category Export Shell Script
*
* @category Mage
* @package Mage_Shell
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Shell_ExportConfigurable extends Mage_Shell_Abstract
{
protected $connection;
/**
* Run script
*
*/
public function run()
{
$mysqli = new mysqli('localhost', 'root', '', 'temp_delete');
$collectionConfigurable = Mage::getResourceModel('catalog/product_collection')->addAttributeToFilter('type_id', array('eq' => 'configurable'));
foreach ($collectionConfigurable as $_configurableproduct) {
$product = Mage::getModel('catalog/product')->load($_configurableproduct->getId());
$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
$sku = $product->getSku();
$attributes = array();
foreach ($productAttributeOptions as $productAttribute) {
$attributes[] = $productAttribute['attribute_code'];
}
$configurable= Mage::getModel('catalog/product_type_configurable')->setProduct($product);
$simpleCollection = $configurable->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
$simpleSkus = array();
foreach($simpleCollection as $simpleProduct){
$simpleSkus[] = $simpleProduct->getSku();
}
$attrsString = implode(',', $attributes);
$skusString = implode(',', $simpleSkus);
$sku = $mysqli->real_escape_string($sku);
$query = "INSERT INTO configurables (sku,configurable_attributes,simples_skus) VALUES ('{$sku}','{$attrsString}','$skusString')";
if (!$mysqli->query($query)) {
echo $mysqli->error;
die;
}
}
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php exportConfigurables.php
help This help
USAGE;
}
}
$shell = new Mage_Shell_ExportConfigurable();
$shell->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment