I want to add another answer and leave my existing answer as feel this one is more what you are after but like both solutions. Below creates a new cli command within Magento for updating image alt tags so you can run on demand or via cron like you have requested.
di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="update_alt" xsi:type="object">Harrigo\ProductUpdateCmd\Console\Command\UpdateAltCommand</item>
</argument>
</arguments>
</type>
</config>
etc/di.xml
This informs Magento about our new command and where to find it.
UpdateAltCommand.php
<?php
namespace Harrigo\ProductUpdateCmd\Console\Command;
use Magento\Catalog\Api\ProductManagementInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Registry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class UpdateAltCommand extends Command
{
protected $productModel;
protected $productRepository;
protected $productManagement;
protected $searchCriteriaBuilder;
protected $registry;
protected $state;
public function __construct(
\Magento\Catalog\Model\Product $productModel,
ProductRepositoryInterface $productRepositoryInterface,
Registry $registry,
SearchCriteriaBuilder $searchCriteriaBuilder,
FilterBuilder $filterBuilder,
State $state
) {
$this->productModel = $productModel;
$this->productRepository = $productRepositoryInterface;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->filterBuilder = $filterBuilder;
$this->registry = $registry;
$this->state = $state;
parent::__construct();
}
protected function configure()
{
$this->setName('harrigo:updatealt')->setDescription('Updates Image Alt.');
$this->addArgument('product_id', InputArgument::OPTIONAL, 'Start from Product ID');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->registry->register('isSecureArea', true, true);
try {
$this->state->getAreaCode();
}
catch (\Exception $e) {
$this->state->setAreaCode(Area::AREA_ADMINHTML);
}
if ($input->getArgument('product_id')) {
$output->writeln('Updating Image Tags from ID: ' . $input->getArgument('product_id'));
} else{
$output->writeln('Updating Image Tags');
}
if ($input->getArgument('product_id')) {
$searchCriteria = $this->searchCriteriaBuilder->addFilter('entity_id', $input->getArgument('product_id'), 'gteq')->create();
$products = $this->productRepository->getList($searchCriteria);
} else{
$products = $this->productRepository->getList($this->searchCriteriaBuilder->create());
}
if (count($products->getItems()) > 0) {
foreach ($products->getItems() as $product) {
$output->writeln('Updating: ' . $product->getName());
$title = $product->getName();
$product = $this->productModel->load($product->getId());
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
if (count($existingMediaGalleryEntries) > 0) {
foreach ($existingMediaGalleryEntries as $key => $entry) {
$entry->setLabel($title);
}
$product->setMediaGalleryEntries($existingMediaGalleryEntries)->setStoreId(0)->save();
}
}
} else {
$output->writeln('No Products Found');
}
}
}
Console/Command/UpdateAltCmd.php
This is the logic for our new command and is added within the Console/Command
directory of the module along with the usual module files. The above tells Magento what command initiates the script and then grabs all products. This then loops through each product adding the product name to each of the image alt tags.
Usage
Once module is installed or remade on your installation run the command by using bin/magento harrigo:updatealt
from Magento root. I have currently only tested this with a handful of products with Magento 2.1.8 however seems to work fine.
Run from Specific Product ID:
This command can also be run with a starting product id by adding the ID to the end of the updatealt
command.
e.g:
bin/magento harrigo:updatealt 100
Download Module
Example of this module here: https://github.com/harrigo/ProductUpdateCmd
Installation
- Download Module
- Place Module Files within
app/code/Harrigo/ProductUpdateCmd
folder from Magento root directory.
- Run
bin/magento setup:upgrade
from Magento root
- Module is now installed, depending on magento mode you may need to compile and deploy static content etc.