Remove white space from SKU in magento2 using script

Remove White Space From SKU In Magento2 Using Script

In this tutorial, we are going to remove white space from SKU using script.

Step 1: First we have to create a new file on the root path of your Magento 2 site. Like ‘whitespaceSku.php’

Step 2: Then you have to copy and paste the below code into your newly created file.

<!DOCTYPE unspecified PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';
$params =  $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('global');
?>
<html>
<head>
    <title>Trim Product SKU</title>
</head>
<body>
    <h1>Trim Product SKU</h1>
    <div class="main-div">
        <form action="" method="post">
            <table>
                <tr>
                    <td>
                        <label><?php echo "Product from";?></label>
                        <td>
                            <input type="text" name="from" id="from" value="<?php echo (isset($_POST['from']))?$_POST['from']:"" ?>"/>
                            <label class="suggestion-from">Product From</label>
                        </td>
                </tr>
                <tr>
                    <td>
                        <label><?php echo "Product to";?></label>
                        <td>
                            <input type="text" name="to" id="to" value="<?php echo (isset($_POST['to']))?$_POST['to']:"" ?>"/>
                            <label class="suggestion-from">Product To</label>
                        </td>
                    </tr>
                    <tr></tr>
                    <tr>
                        <td colspan="2"><input class="search-button" type="submit" title="Trim" value="Trim"/>
                        </td>
                    </tr>
            </table>
        </form>
    </div>
</body>
</html>   
<?php 
if($_POST){
    $from = $_POST['from'];
    $to = $_POST['to'];
    echo "<table border='1' class='result-table'><thead><tr><td colspan='3'>Import Data</td></tr></thead><tbody><tr><td>Product Id</td><td>Trimmed SKU</td></tr>";
    trimSKU($from, $to);
    echo "</tbody></table>";
}

function trimSKU($from, $to){
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
     $collection = $productCollection->create()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('entity_id',
                    array(
                        'from'=>$from,
                        'to'=>$to
                    ))
                ->load();
        $i = 0;
        foreach($collection as $product){
            $i++;
            if($i > $to || $i < $from) { continue; };
            $arr_string=explode(" ", $product->getSku());
            $string='';
            foreach($arr_string as $str){
                $string.=$str;
        }
            $sku=$string;

            if($product->getId()){
                $product->setSku($sku)->save();
                echo "<tr><td>". $product->getId()."</td><td>".$product->getSKU()."</td></tr>";
            }
        } 
    }

?>

Step 3: Now, you have to run the file by opening it using a URL. Like https://localhost/whitespaceSku.php

Step 4: After you run the URL, you will see a form like the below image.

  • Product form: Enter starting id of your product like 1.
  • Product to: Enter the ending id of your product like 200 or 300.

Step 5: Then click on the Trim button. It will remove white space from SKU I recommend trimming 100 products at a time, not more than 100.

Step 6: At the last check your SKU in the backend. For example: if your product SKU is “remove white space” after trimmed sku will become “removewhitespace”.

Please contact us at manish@bay20.com or call us at +91-8800519180 for any support related to Magento 2. You can also visit the Magento2 development page to check the services we offer.