Bhaveshpp

Professional Magento Developr - Healp eachother to grow

Magento2: How to run script at magento root

27 May 2021 » magento2

Magento 2 Root Script


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', -1);
error_reporting(E_ALL);

use Magento\Framework\App\Bootstrap;

require 'app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
//$state->setAreaCode('adminhtml');

/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $products */
$products = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection')
    ->addAttributeToSelect(['entity_id','sku'])
    ->addFieldToFilter('description', ['like' => '%%']);
echo $products->getSelectSql(true);

Magento 2 Update mass Product description


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', '5G');
error_reporting(E_ALL);

$url='localhost:3306';
$username='magento2_db';
$password='pass';
$db = "mage_v241";

$conn=mysqli_connect($url,$username,$password,$db);

if(!$conn){
    die('Could not Connect My Sql:' .mysql_error());
}

$mysqli = new mysqli($url,$username,$password,$db);

if ($mysqli -> connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
    exit();
}

$result = mysqli_query($conn,"SELECT * FROM `catalog_product_entity_text` WHERE `value` LIKE '%%'");
echo "entity id";
while($row = mysqli_fetch_array($result)) {
    echo "<br>";
    echo $row['entity_id'];
    $description = str_replace('','',$row['value']);
    $description = $mysqli -> real_escape_string($description);
    $sql = "UPDATE `catalog_product_entity_text` SET `value` = '".$description."' WHERE `value_id` = '".$row['value_id']."';";
    mysqli_query($conn,$sql);
}
?>

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('memory_limit', -1);
ini_set( 'max_execution_time', 0 );
error_reporting(E_ALL);

use \Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$products = json_decode(file_get_contents(__DIR__ . "/products_vat.json"));

$bootstrap     = Bootstrap::create( BP, $_SERVER );
$objectManager = $bootstrap->getObjectManager();
$instance      = \Magento\Framework\App\ObjectManager::getInstance();
$state         = $objectManager->get( '\Magento\Framework\App\State' );
$state->setAreaCode( 'frontend' );
$resource = $instance->get( '\Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$table = $resource->getTableName('catalog_product_entity_text');
$sql = "SELECT * FROM `".$table."` WHERE `value` LIKE '%target=\"_blank\"%'";
$result = $connection->fetchAll($sql);

foreach ($result as $key => $value) 
{
    testlog("Entity Id: ".$value['entity_id']);
    testlog("Value Id: ".$value['value_id']);
    testlog("************** old ******************");
    testlog("old Value: ".$value['value']);
    testlog("*************************************");
    testlog("");
    $description = (str_replace('target="_blank"','',$value['value']));
    echo $where = " `value_id` = '".$value['value_id']."'"; 
    $connection->update($table,['value'=>$description],$where);
    // die(__FILE__."::".__LINE__);
}
echo "done";

function testlog($txt)
{
    file_put_contents('20210729-description-update.log', ($txt).PHP_EOL , FILE_APPEND | LOCK_EX);
}
?>