magento 2 regenerate product url php code

No comments
please follow the step as i show below

1) backup you database 

2) create one controller name Producturl.php

3) copy my code into that controller 


<?php  

namespace Company\Module\Controller\Producturl;


use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\Store\Model\Store;

class Producturl extends \Magento\Framework\App\Action\Action
{
       public function execute()
    { 
           //// for regenerate product url /////////////////
             $objectManager = \Magento\Framework\App\ObjectManager::getInstance();   
             $this->collection = $objectManager->create('\Magento\Catalog\Model\ResourceModel\Product\Collection');

             $this->productUrlRewriteGenerator = $objectManager->create('\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator');
             
             $this->urlPersist = $objectManager->create('\Magento\UrlRewrite\Model\UrlPersistInterface');

             $this->collection->addAttributeToSelect(['url_path', 'url_key']);
              $list = $this->collection->load();
              $store_id=1; // your store id 
              
              foreach($list as $product) {                
                      if($store_id === Store::DEFAULT_STORE_ID)
                          $product->setStoreId($store_id);
                      $this->urlPersist->deleteByData([
                          UrlRewrite::ENTITY_ID => $product->getId(),
                          UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
                          UrlRewrite::REDIRECT_TYPE => 0,
                          UrlRewrite::STORE_ID => $store_id
                      ]);
                      try {
                          $this->urlPersist->replace(
                              $this->productUrlRewriteGenerator->generate($product)
                          );
                      } catch(\Exception $e) {
                          $out->writeln('<error>Duplicated url for '. $product->getId() .'</error>');
                      }
              }
    }


}


?>

No comments :

Post a Comment

make url short php

1 comment
This code is for make you url short through google API


<?php

// Declare the class
class GoogleUrlApi {

public $key=your key
// Constructor
function GoogleURLAPI($apiURL = 'https://www.googleapis.com/urlshortener/v1/url') {
// Keep the API Url

$this->apiURL = $apiURL.'?key='.$this->key;
}

// Shorten a URL
function shorten($url) {
// Send information along
$response = $this->send($url);
// Return the result
return isset($response['id']) ? $response['id'] : false;
}

// Expand a URL
function expand($url) {
// Send information along
$response = $this->send($url,false);
// Return the result
return isset($response['longUrl']) ? $response['longUrl'] : false;
}

// Send information to Google
function send($url,$shorten = true) {
// Create cURL
$ch = curl_init();
// If we're shortening a URL...
if($shorten) {
curl_setopt($ch,CURLOPT_URL,$this->apiURL);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode(array("longUrl"=>$url)));
curl_setopt($ch,CURLOPT_HTTPHEADER,array("Content-Type: application/json"));
}
else {
curl_setopt($ch,CURLOPT_URL,$this->apiURL.'&shortUrl='.$url);
}
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// Execute the post
$result = curl_exec($ch);
// Close the connection
curl_close($ch);
// Return the result
return json_decode($result,true);
}
}



// Create instance with key
$key = 'AIzaSyDUtkDIQ6uK7GCE68Lji_eQE9lPlo3Lk5Q';
$googer = new GoogleURLAPI();

// Test: Shorten a URL
$shortDWName = $googer->shorten("http://americanentrepreneurship.com/nj/entrepreneurs-featured/university-entrepreneurial-activities-draw-alumni-involvement");
echo $shortDWName; // returns http://goo.gl/i002
echo "<br>";
// Test: Expand a URL
$longDWName = $googer->expand($shortDWName);
echo $longDWName; // returns https://davidwalsh.name


?>

1 comment :

Post a Comment