0

enter image description hereMagento2 how to developed custom script export for a configurable product with associated sku please if anyone creates the script to share with me as soon as.i attched screenshot for get export csv like this

1
  • Please share script if result need that csv format like Commented May 16, 2018 at 5:45

2 Answers 2

4
<?php
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
$registry = $objectManager->get('Magento\Framework\Registry');
$registry->register('isSecureArea', true);

// Store id of exported products, This is useful when we have multiple stores. 
$store_id = 1;

$fp = fopen("export.csv","w+");
$collection = $objectManager
    ->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')
    ->create()->addStoreFilter($store_id)
    ->addAttributeToFilter('type_id', 'configurable')
    ->addAttributeToSelect(array('name','sku','associated_skus'));

foreach ($collection as $product) {
    $data = array();
    $data[] = $product->getTypeId();
    $data[] = $product->getName();
    $data[] = $product->getSku();

    $child_products = $product->getTypeInstance()->getUsedProducts($product, null);
    if (count($child_products) > 0) {
        $resultstr  = array();
        foreach($child_products as $child) {
            $resultstr[] = $child->getSku();
        }
        echo $data[] = implode(',', $resultstr);
    }  
    fputcsv($fp, $data);    
}

fclose($fp);
0
2

Create one php file inside your magento root directory with below code.

<?php
require './app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$_objectManager = $bootstrap->getObjectManager();
$state = $_objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('admin');
$registry = $_objectManager->get('Magento\Framework\Registry');
$registry->register('isSecureArea', true);

//Store id of exported products, This is useful when we have multiple stores. 
$store_id = 1;

$fp = fopen("export.csv","w+");
$collection = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory')->create()->addStoreFilter($store_id)->addAttributeToFilter('type_id', 'configurable')->addAttributeToSelect(array('name','sku'));
foreach ($collection as $product)
{
    $data = array();
    $data[] = $product->getTypeId();
    $data[] = $product->getName();
    $data[] = $product->getSku();
    fputcsv($fp, $data);

    $child_products = $product->getTypeInstance()->getUsedProducts($product, null);
    if(count($child_products) > 0)
    {
        foreach($child_products as $child)
        {
            $data = array();
            $data[] = $child->getTypeId();
            $data[] = $child->getName();
            $data[] = $child->getSku();
            fputcsv($fp, $data);
        }
    }   
}
fclose($fp);
4
  • You can find export.csv inside root directory. Commented May 15, 2018 at 14:33
  • how can i import configurable product with associated custom script in magento2 Commented Jun 18, 2018 at 9:55
  • How to custom script through export only configurable_variations if you have idea reply me Commented Jan 16, 2019 at 6:24
  • They give me error like. Call to undefined method Magento\Catalog\Model\Product\Type\Simple::getUsedProducts() Commented Aug 20, 2019 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.