0

I have a module that programmatically creates new products in Magento 2.3.2 with multiple store views. This Magento version has an issue that causes the product URL to be correct only for the default store view. In order to solve this, I installed the OlegKoval RegenerateUrlRewrites extension that adds a console command to add the missing URLs in the database. The extension works: I run the extension console command and the URLs are fixed.

So what I'm doing right now is:

  • the method is called and generates all the new products
  • I manually run OlegKoval command to fix the URLs

What I'd like to achieve:

  • the method is called and generates all the new products
  • the same method then calls the OlegKoval command to fix the URLs so that I don't have to run the command manually

Is there a way to achieve this?

  • i added solution ): – chandresh chauhan Oct 16 at 13:45
  • Thank you. Unfortunately I got an error. The command I run manually is php bin/magento ok:urlrewrites:regenerate so I tried $output = shell_exec('php bin/magento ok:urlrewrites:regenerate'); $this->log($output); but the log shows Could not open input file: bin/magento – Lounik Oct 17 at 14:22
  • can you check modified answer – chandresh chauhan Oct 18 at 4:01
0

You can try this for run OlegKoval command in observer

$output = shell_exec('php bin/magento cache:clean');

you can change your command in this, you can try below code in observer file

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
    $url = $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);

    $postUrl = $url.'command.php';
    $curl = curl_init();
    curl_setopt_array(
        $curl,
        [
            CURLOPT_URL => $postUrl,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 100,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_SSL_VERIFYPEER => 0,
        ]
    );

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

== create command.php file at the root in magento

<?php
chdir(dirname(__FILE__));

use Magento\Framework\App\Bootstrap;

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

$params = $_SERVER;

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

$obj = $bootstrap->getObjectManager();

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

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$output = shell_exec('php bin/magento cache:clean');
$objectManager->get('Psr\Log\LoggerInterface')->info(print_r($output,true));

also refer this link may help you https://www.google.com/search?q=enable+exec

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.