Snippets

X-com M1 product / attribute export

Created by Mycha de Vrees last modified
<?php

require_once 'abstract.php';

class Mage_Shell_ExportAttributeSets extends Mage_Shell_Abstract
{
    protected $connection;
    protected $header = true;
    protected $attributeSetModel = null;
    protected $images = [];
    /**
     * Run script
     *
     */
    public function run()
    {
        Mage::app()->setCurrentStore($this->getArg('store_id') ? $this->getArg('store_id') : Mage_Core_Model_App::ADMIN_STORE_ID);

        ini_set('memory_limit', '4096M');
        
        // fetch attributes from eav_attribute_set
        $attributeSets = [
            21 => "Accessoires",
            10 => "Braces",
            24 => "Cardio apparatuur",
            15 => "Compressiekleding",
            17 => "Fitness",
            23 => "Homecare",
            19 => "Meetinstrumenten",
            16 => "Oefenmateriaal",
            25 => "Protectie",
            28 => "Sportswear",
            14 => "Verbruiksmateriaal",
            29 => "Voet",
            20 => "Zit- en ligcomfort",
        ];

        $attributeCodes = [];

        $map = $this->getAttributeMap();
        $allowed = $this->getAllowedAttributes();

        foreach ($attributeSets as $attributeSetId => $attributesetLabel) {

            $groups = Mage::getModel('eav/entity_attribute_group')
            ->getResourceCollection()
            ->setAttributeSetFilter($attributeSetId) // Attribute set Id compressiekleding
            ->setSortOrder()
            ->load();

            foreach ($groups as $group) {
                $groupName = $group->getAttributeGroupName();
                $groupId   = $group->getAttributeGroupId();

                $attributes = Mage::getResourceModel('catalog/product_attribute_collection')
                    ->setAttributeGroupFilter($group->getId())
                    ->addVisibleFilter()
                    ->checkConfigurableProducts()
                    ->load();

                foreach ($attributes->getItems() as $attribute) {
                    $attributeCode = $attribute->getAttributeCode();
                    if (isset($map[$attribute->getAttributeCode()])) {
                        $attributeCode = $map[$attribute->getAttributeCode()];
                    }
                    if (in_array($attributeCode, $allowed)) {
                        $attributeCodes['attribute_sets'][$attributesetLabel]['name'] = $attributesetLabel;
                        $attributeCodes['attribute_sets'][$attributesetLabel]['inherit'] = 'Default';
                        $attributeCodes['attribute_sets'][$attributesetLabel]['groups']['name'] = $groupName;
                        $attributeCodes['attribute_sets'][$attributesetLabel]['groups']['attributes'][] = $attributeCode;
                    }
                }

                // if ($groupId == 43) { // Get group 'Application'
                //     if ($attributes->getSize() > 0) {
                //         echo '<strong>'.$groupName.'</strong><br>';
                //         foreach ($attributes->getItems() as $attribute) {
                //             /* @var $child Mage_Eav_Model_Entity_Attribute */
                //             $attributeCodes[] = $attribute->getAttributeCode();
                //             echo $attribute->getName(); echo '<br>';
                //         }
                //     }
                // }

            }
        }

        file_put_contents('attributesets.json', json_encode($attributeCodes));
    }

    private function getAttributeMap(): array
    {
        return [
            "alteanbol" => "alternative_ean_bol",
            "ean_bolcom" => "ean_bol",
            "bol_shipping_time" => "bol_shipping_time",
            "avonddealdagen" => "evening_deal_days",
            "proefperiode" => "trial_period",
            "huisselectie" => "home_branded_selection",
            "home_branded_product" => "home_branded_sku",
            "onze_keuze" => "our_choice",
            "onze_keuze_info" => "our_choice_info",
            "waarom_onze_keuze" => "our_choice_why",
            "grotere_aantallen" => "large_quantities",
            "aantal_afname" => "large_quantities_text",
            "zoekcode_boekje" => "searchcode",
            "eancodealt" => "ean_code_alt",
            "exclude_in_shoppingfeed " => "exclude_in_shoppingfeed",
            "delivery" => "delivery",
            "levertijd_afwijkend" => "delivery_custom",
            "type_assortiment" => "type_assortiment",
            "xcore_item_group" => "xcore_item_group",
            "xcore_suppliers" => "xcore_suppliers",
            "xcore_suppliers_country_origin" => "xcore_suppliers_country_origin",
            "xcore_suppliers_currency" => "xcore_suppliers_currency",
            "xcore_suppliers_lead_time" => "xcore_suppliers_leadtime",
            "xcore_suppliers_min_order" => "xcom_suppliers_min_order",
            "xcore_suppliers_purchase_price" => "xcom_suppliers_purchase_price",
            "xcore_suppliers_sku" => "xcom_suppliers_purchase_sku",
            "xcore_suppliers_vat_code" => "xcom_suppliers_purchase_vat_code",
            "color_toprobag" => "multicolor",
            "color" => "multicolor",
        ];
    }

    private function getAllowedAttributes(): array
    {
        return [
            "depth",
            "seat_depth",
            "total_length",
            "chair_height",
            "total_width",
            "seat_size",
            "seat_size_open",
            "seat_size_closed",
            "comfort_height",
            "width_arm",
            "seat_width",
            "chair_widthdeapth",
            "seat_adjustable_height",
            "contents",
            "max_height_handle",
            "seat_height",
            "height",
            "length",
            "width",
            "braces_size_pelvis",
            "indication",
            "braces_sole_height",
            "braces_indication_thigh",
            "braces_indication_calf",
            "wheels_type",
            "compression_herzog_length",
            "compression_herzog_shoe_size",
            "label_opties",
            "toilet_cover",
            "wheel_diameter",
            "braces_filter_knee",
            "length_thigh",
            "length_arm",
            "protection_level",
            "zit_lig_type",
            "rehasense_rollator",
            "hoogte_nek",
            "compressie_model",
            "compressie_indication",
            "compressie_type",
            "huisselectie",
            "compression_herzog_size",
            "opbouw_rug",
            "cold_warmth_therapy",
            "rehband_productielijn",
            "latex_free",
            "neoprene_free",
            "braces_indicaties_arm",
            "braces_indicaties_bekken",
            "braces_indicaties_duim",
            "braces_indicaties_rib",
            "wristbrace_thumb",
            "application",
            "protection_fit",
            "teenstuk",
            "compressie_denier",
            "compressie_materiaal",
            "compressie_klasse",
            "gender",
            "compression_usage",
            "rollator_type",
            "rollator_weight",
            "braces_indicaties_achillespees",
            "braces_indicaties_enkel",
            "brace_size_neck",
            "braces_indicaties_patella",
            "braces_indicaties_pols",
            "braces_indicaties_schouder",
            "packaging",
            "verbr_mat_type",
            "homecare_type",
            "brace_werking",
            "compressieproduct_type",
            "pasvorm",
            "sportswear_wasinstructies",
            "sportwear_type",
            "braces_support",
            "craft_uitvoering",
            "craft_temperature",
            "braces_indicaties_rug",
            "side",
            "type_assortiment",
            "length_calf",
            "multicolor",
            "color_toprobag",
            "max_weight",
            "training_type",
            "braces_indication_foot",
            "protectie_type",
            "topro_rollator",
            "braces_type",
            "shoe_size",
            "braces_sizing_chart",
            "braces_size_wrist",
            "braces_indicaties_knie",
            "braces_indicaties_nek",
            "compression_size",
            "braces_size_knee",
            "braces_size_achilles",
            "braces_size_kids",
            "braces_size_back",
            "braces_size_ankle",
            "brace_size_thumb",
            "brace_size_shoulder",
            "brace_size_arm",
            "brace_size_thigh",
            "brace_size_rib",
            "brace_size_calf",
            "comfort_edition",
            "rollator_size",
            "protection_size",
            "size_dainese",
            "accessories_size"
        ];
    }


    /**
     * Retrieve Usage Help Message
     *
     */
    public function usageHelp()
    {
        return <<<USAGE
Usage:  php -f export_data.php -- --file <csv_file>

  help                        This help
  file                        Csv file to write to

USAGE;
    }

}

$shell = new Mage_Shell_ExportAttributeSets();
$shell->run();
Example output of running above script with --images flag

[11:12:47]-[production791]-[all4fysio]-[~/public/shell]$ cat images.txt
/p/u/push_care.jpg
/p/u/push_med_kniebrace_1_1.jpg
/m/c/mcdavid_knieband_opening_409.jpg
/m/c/mcdavid-kniebrace-ligament-425-0.jpg
/m/d/md_421_kneesupportstays_2000x_3e1c76ca-b874-4f7d-a6ea-23b4ccd1bae4_1800x1800.jpg
/m/c/mcdavid_kniebrace_ps2_scharnier_429.png
/m/c/mcdavid_kniebrace_met_dual_disk_scharnieren_422.png
/o/n/ontwerp_zonder_titel_3.jpg
/g/e/genu_pro_comf.jpg
/b/a/bauerfeind_genutrain_kniebrace__.jpg
/o/t/otto_bock_genu_sensa_kniebrace_01.jpg
/g/e/genu_carezza_patella_stabilizer_8360.jpg
/g/e/genu_carezza_2_2.jpg
/p/u/push_ortho_aequi_enkelbrace_1.jpg
/p/u/push_care_enkelbrace_1.jpg
/m/u/mueller_stabilizer_enkelbrace.jpg
/t/h/thuasne-ligastrap-malleo-enkelbrace-grijs-218003.jpg
/p/e/pero-med_1.jpg
/t/h/thuasne-enkelbrace-malleo-dynastab.jpg
#!/usr/bin/env bash

mapfile -t IMAGES < images.txt

TARGETPATH="sync/development/magento1/import/product/images/"
ALLIMAGES=""

for IMAGE in "${IMAGES[@]}"; do
  ALLIMAGES+="/home/all4fysio/public/media/catalog/product${IMAGE} :"
done;
rsync -avz all4fysio@production791.hipex.io:${ALLIMAGES::-1} ${TARGETPATH};
<?php

require_once 'abstract.php';

class Mage_Shell_ExportProducts extends Mage_Shell_Abstract
{
    protected $connection;
    protected $header = true;
    protected $attributeSetModel = null;
    protected $images = [];
    /**
     * Run script
     *
     */
    public function run()
    {
        Mage::app()->setCurrentStore($this->getArg('store_id') ? $this->getArg('store_id') : Mage_Core_Model_App::ADMIN_STORE_ID);

        ini_set('memory_limit', '4096M');

        if ($this->getArg('file')) {
            $path = $this->getArg('file');
            echo 'writing data to ' . $path . PHP_EOL;
            $file = fopen($path, 'w');

            $productModel = Mage::getModel("catalog/product");
            $productCollection = $productModel
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter(
                    'status',
                    array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                )
                ->joinField(
                    'qty',
                    'cataloginventory/stock_item',
                    'qty',
                    'product_id=entity_id',
                    '{{table}}.stock_id=1',
                    'left'
                )
                ->joinField(
                    'is_in_stock',
                    'cataloginventory/stock_item',
                    'is_in_stock',
                    'product_id=entity_id',
                    '{{table}}.stock_id=1',
                    'left'
                )
                // ->addFieldToFilter('entity_id', "4375")
                // ->addFieldToFilter(
                //     array('status'),
                //     array(
                //         array('in'=> array(
                //             'complete',
                //             'canceled',
                //             'closed'
                //         )),
                //     )
                // )
                ->setPageSize(100)
            ;

            $pages = $productCollection->getLastPageNumber();
            $i = 1;
            while($i <= $pages) {
                echo("Page: $i/$pages".PHP_EOL);
                $productCollection->setCurPage($i)->load();
                foreach ($productCollection as $product) {
                    try {
                        $this->exportData($product, $file);
                        $product = null;    
                    } catch(\Exception $e) {
                        var_dump($e->getMessage(), $product->getSku());
                    }
                }
                $i++;
                $productCollection->clear();
            }
            fclose($file);
            if ($this->getArg('images')) {
                // get images
                $images = array_unique($this->images);
                file_put_contents('images.txt', implode("\n", $images));
                echo("Images written to images.txt".PHP_EOL);
                // print_r($images);
            }
        } else {
            echo $this->usageHelp();
        }
    }

    protected function exportData(Mage_Catalog_Model_Product $product, $file)
    {
        /*
         * get dropdown attibutes using this query:
           select ea.attribute_code from eav_attribute ea where ea.attribute_id in (
            select distinct eao.attribute_id from eav_attribute_option eao where option_id in (select distinct eaov.option_id from eav_attribute_option_value eaov));
        *
        */
        $attributes = [
            "sku",
            "name",
            "description",
            "status",
            "short_description",
            "manufacturer_sku",
            "weight",
            "url_key",
            "type_id",
            "qty",
            "is_in_stock",
            "tax_class_id",
            "country_of_manufacture",
            "image",
            "featured_text",
            "visibility",
            "costprice",
            "price",
            "special_price",
            "alteanbol",
            "ean_bolcom",
            "proefperiode",
            "home_branded_product"
        ];
        $dropdownAttributes = [
            "color"
        ];

        $keys = array_merge($attributes, $dropdownAttributes);
        $productData = $product->getData();
        $data = [];
        foreach ($keys as $attributeCode) {
            if ($attributeCode == "image" && $productData[$attributeCode] != "no_selection") {
                $this->images[] = $productData[$attributeCode];
            }
            $data[$attributeCode] = $productData[$attributeCode] ?? "";
            if (in_array($attributeCode, $dropdownAttributes)) {
                // check if not "" or not null
                if ($data[$attributeCode]) {
                    $attributeValue = nl2br($product->getResource()->getAttribute($attributeCode)->getFrontend()->getValue($product));
                    $data[$attributeCode] = $attributeValue;
                    unset($attributeValue);
                }
            }
        }

        $data['category_codes'] = implode(';', $product->getCategoryIds());
        $data['simples_skus'] = "";
        $data['configurable_attributes'] = ""; // TODO uncomment when attributes are created in M2
        if ($product->getTypeId() == "configurable") {
            list($simplesSkus, $configurableAttributes) = $this->getSimpleSkus($product);
            $data['simples_skus'] = implode(',', $simplesSkus);
            // TODO uncomment
            //   comma implode because magmi explodes seperated by comma
            $data['configurable_attributes'] = implode(',', $configurableAttributes); // TODO uncomment when attributes are created in M2
        }
        
        $data['attributeset'] = $this->getAttributeSetName($product->getAttributeSetId());
        
        if($this->header) {
            fputcsv($file, array_keys($data), ';');
            $this->header = false;
        }
        fputcsv($file, $data, ';');

        unset($data);
        unset($product);

    }

    /**
     * Retrieve Usage Help Message
     *
     */
    public function usageHelp()
    {
        return <<<USAGE
Usage:  php -f export_data.php -- --file <csv_file>

  help                        This help
  file                        Csv file to write to

USAGE;
    }

    /**
     * @param $configurableProduct
     * return array
     */
    protected function getSimpleSkus($configurableProduct)
    {
        $simpleSkus = [];
        $configurableAttributes = [];
        $children = $configurableProduct->getTypeInstance()->getUsedProducts($configurableProduct);
        $_configurableAttributes = $configurableProduct->getTypeInstance()->getConfigurableAttributesAsArray($configurableProduct);
        foreach ($_configurableAttributes as $configurableAttribute) {
            $configurableAttributes[] = $configurableAttribute['attribute_code'];
        }
        foreach ($children as $childProduct) {
            $simpleSkus[] = $childProduct->getSku();
        }
        return [$simpleSkus, $configurableAttributes];
    }

    private function getAttributeSetName($attributeSetId)
    {
        if (!$this->attributeSetModel) {
            $this->attributeSetModel = Mage::getModel("eav/entity_attribute_set");
        }
        $this->attributeSetModel->load($attributeSetId);
        return $this->attributeSetModel->getAttributeSetName();
    }
}

$shell = new Mage_Shell_ExportProducts();
$shell->run();

// run via php -f product_export.php -- --file products.csv --images
<?php

require_once 'abstract.php';

class Mage_Shell_ExportProducts extends Mage_Shell_Abstract
{
    protected $connection;
    protected $header = true;
    protected $attributeSetModel = null;
    protected $images = [];
    /**
     * Run script
     *
     */
    public function run()
    {
        Mage::app()->setCurrentStore($this->getArg('store_id') ? $this->getArg('store_id') : Mage_Core_Model_App::ADMIN_STORE_ID);

        ini_set('memory_limit', '4096M');

        if ($this->getArg('file')) {
            $path = $this->getArg('file');
            echo 'writing data to ' . $path . PHP_EOL;
            $file = fopen($path, 'w');

            $productModel = Mage::getModel("catalog/product");
            $productCollection = $productModel
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter(
                    'type_assortiment',
                    array('neq' => 4485)
                )
                ->joinField(
                    'qty',
                    'cataloginventory/stock_item',
                    'qty',
                    'product_id=entity_id',
                    '{{table}}.stock_id=1',
                    'left'
                )
                ->joinField(
                    'is_in_stock',
                    'cataloginventory/stock_item',
                    'is_in_stock',
                    'product_id=entity_id',
                    '{{table}}.stock_id=1',
                    'left'
                )
                // ->addFieldToFilter('entity_id', "4375")
                // ->addFieldToFilter(
                //     array('status'),
                //     array(
                //         array('in'=> array(
                //             'complete',
                //             'canceled',
                //             'closed'
                //         )),
                //     )
                // )
                ->setPageSize(100)
            ;

            $pages = $productCollection->getLastPageNumber();
            $i = 1;
            while($i <= $pages) {
                echo("Page: $i/$pages".PHP_EOL);
                $productCollection->setCurPage($i)->load();
                foreach ($productCollection as $product) {
                    try {
                        $this->exportData($product, $file);
                        $product = null;
                    } catch(\Exception $e) {
                        var_dump($e->getMessage(), $product->getSku());
                    }
                }
                $i++;
                $productCollection->clear();
            }
            fclose($file);
            if ($this->getArg('images')) {
                // get images
                $images = array_unique($this->images);
                file_put_contents('images.txt', implode("\n", $images));
                echo("Images written to images.txt".PHP_EOL);
                // print_r($images);
            }
        } else {
            echo $this->usageHelp();
        }
    }

    protected function exportData(Mage_Catalog_Model_Product $product, $file)
    {
        /*
         * get dropdown attibutes using this query:
           select ea.attribute_code from eav_attribute ea where ea.attribute_id in (
            select distinct eao.attribute_id from eav_attribute_option eao where option_id in (select distinct eaov.option_id from eav_attribute_option_value eaov));
        *
        */
        $attributes = [
            "sku",
            "image",
            "gallery"
        ];
        $dropdownAttributes = [

        ];

        $keys = array_merge($attributes, $dropdownAttributes);
        $productData = $product->getData();
        $data = [];
        foreach ($keys as $attributeCode) {
            $data[$attributeCode] = $productData[$attributeCode] ?? "";
            if ($attributeCode == "gallery") {
                $_product = Mage::helper('catalog/product')->getProduct($product->getId(), null, null);
                $gallery = $_product->getData('media_gallery');
                $files = [];
                foreach ($gallery as $key => $image) {
                    foreach ($image as $imageArr) {
                        $files[] = $imageArr['file'];
                        $this->images[] = $imageArr['file'];
                    }
                }
                $data[$attributeCode] = implode(',', $files);
            }
        }

        if($this->header) {
            fputcsv($file, array_keys($data), ';');
            $this->header = false;
        }
        fputcsv($file, $data, ';');

        unset($data);
        unset($product);

    }

    /**
     * Retrieve Usage Help Message
     *
     */
    public function usageHelp()
    {
        return <<<USAGE
Usage:  php -f export_data.php -- --file <csv_file>

  help                        This help
  file                        Csv file to write to

USAGE;
    }
}

$shell = new Mage_Shell_ExportProducts();
$shell->run();

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.