I'm trying to adapt some scripts I created (fully functional but not very Magento2) into modules but after many hours of reading, still don't really understand how it works. For exemple, this scripts called "delete.php" retrieve all configurable/simple associated which are out of stock, convert and download in a csv so I can use the import process (behavior delete) to remove all old products.
<?php
ini_set('memory_limit', -1);
ini_set('max_execution_time', 0);
error_reporting(E_ALL);
ini_set('display_errors', '1');
use Magento\Framework\App\Bootstrap;
include('../../app/bootstrap.php');
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$productCollection = $obj->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection = $productCollection
->addAttributeToSelect('*')
->setPageSize(500) //Selecting 500 products for TEST
->load();
echo "sku" . ';' . "name" . ';' . "mgs_brand" . ';' . "urbmag_season" . "\n";
foreach($collection as $product){
$productType = $product->getTypeId(); // look if CONFIGURABLE
if ($productType == "configurable" AND !$product->isAvailable()) { // CONF + OUT OF STOCK
$productSku = $product->getSku();
$productName = $product->getName();
$productBrand = $product->getAttributeText('mgs_brand'); //brand attribute for filter in csv
$productSeason = $product->getAttributeText('urbmag_season'); // season attribute for filter in csv
echo $productSku.';'.$productName.';'.$productBrand.';'.$productSeason."\n";
$children = $product->getTypeInstance()->getUsedProducts($product); // simple associated
foreach ($children as $child) {
$childSku = $child->getSku();
$childName = $child->getName();
echo $childSku.';'.$childName.';'.$productBrand.';'.$productSeason."\n";
}
}
}
chdir('convert_csv');
if( file_exists ('delete.csv'))
unlink('delete.csv') ;
//***************** DOWNLOAD CSV ***********************
$fp = fopen('delete.csv', 'w+');
fclose($fp);
header("Content-type: text/csv");
header("Content-disposition: attachment; filename = delete.csv");
readfile("delete.csv");
I follow all the tutorials I found, like How to create a basic module in Magento 2 but I can't adapt this for my needs. Idea here is to call an url that execute my function. Would someone have the kindness and patience to explain to me step by step how to make my module ?