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

  • Please share script if result need that csv format like – Rv Singh May 16 '18 at 5:45
3
<?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('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();
    //$data[] = $product->getAssociatedSkus();
   // fputcsv($fp, $data);

    $child_products = $product->getTypeInstance()->getUsedProducts($product, null);
    if(count($child_products) > 0)
    {
        $resultstr  = array();
        foreach($child_products as $child)
        {
            //echo "<pre>";
            //var_dump($child->getData());
            //die;

            //$data = array();
           // $data[] = $child->getTypeId();
            //$data[] = $child->getName();
            //$data[] = $child->getSku();
            $resultstr[] = $child->getSku();

            //$data[] = $child->getAssociatedSkus();
            //$data[] = $child->getAssociatedSkus();
           // fputcsv($fp, $data);
        }
        echo $data[] = implode(',', $resultstr);
    }  
fputcsv($fp, $data);    
}
fclose($fp);
?>
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);
  • You can find export.csv inside root directory. – Bhavin iFlair May 15 '18 at 14:33
  • how can i import configurable product with associated custom script in magento2 – Rv Singh Jun 18 '18 at 9:55
  • How to custom script through export only configurable_variations if you have idea reply me – Rv Singh Jan 16 at 6:24
  • They give me error like. Call to undefined method Magento\Catalog\Model\Product\Type\Simple::getUsedProducts() – Nikul Aug 20 at 6:33

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.