Commit 7fc3ee12 by halweg

feat : add command joshine:fix-base-image

parent 983d7735
<?php
namespace Joshine\Script\Console\Command;
use Joshine\Script\Model\ImageQueryResource;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ProductFirstImageAsBaseFixer extends Command
{
protected $imageQueryResource;
protected $productRepository;
protected $searchCriteriaBuilder;
public function __construct(
ImageQueryResource $imageQueryResource,
ProductRepositoryInterface $productRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
string $name = null)
{
$this->imageQueryResource = $imageQueryResource;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->productRepository = $productRepository;
parent::__construct($name);
}
protected function configure()
{
$this->setName('joshine:fix-base-image')->setDescription('设置第一张产品图为base image');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
//$this->fixBaseImage($input, $output, $pid);
$criteria = $this->searchCriteriaBuilder->create();
$products = $this->productRepository->getList($criteria);
foreach ($products as $product) {
$output->writeln('pid' . $product->getId());
}
}
protected function fixBaseImage($input, $output, $productId)
{
$mainImage = $this->imageQueryResource->getProductMainImage($productId);
$firstImage = $this->imageQueryResource->getFirstImages($productId);
$output->writeln('first image' . $firstImage);
if ($mainImage != $firstImage) {
$output->writeln('fix product ' . $mainImage . ' product id: ' . $productId);
$otherImages = $this->imageQueryResource->getAllImages($productId);
foreach ($otherImages as $image) {
$position = 1;
if ($image['value'] == $mainImage) {
$this->imageQueryResource->updatePosition((int)$image['value_id'], $position);
}
}
reset($otherImages);
foreach ($otherImages as $image) {
if ($image['value'] != $mainImage) {
$position++;
$this->imageQueryResource->updatePosition((int)$image['value_id'], $position);
}
}
}
}
}
\ No newline at end of file
<?php
namespace Joshine\Script\Model;
use Magento\Framework\App\ResourceConnection;
class ImageQueryResource
{
/**
* @var ResourceConnection
*/
private $resourceConnection;
/**
* @var \Magento\Eav\Model\Config
*/
private $config;
public function __construct(
ResourceConnection $resourceConnection,
\Magento\Eav\Model\Config $config
) {
$this->resourceConnection = $resourceConnection;
$this->config = $config;
}
public function getProductMainImage(int $productId)
{
$mainImageAttribute = $this->config->getAttribute(\Magento\Catalog\Model\Product::ENTITY, 'image');
$select = $this->resourceConnection->getConnection()->select();
$select->from($this->resourceConnection->getTableName('catalog_product_entity_' . $mainImageAttribute->getBackendType()));
$select->where('entity_id=?', $productId);
$select->where('attribute_id=?', $mainImageAttribute->getId());
$select->reset(\Magento\Framework\DB\Select::COLUMNS);
$select->columns(['value']);
return $this->resourceConnection->getConnection()->fetchOne($select);
}
/**
* @param int $productId
* select * from catalog_product_entity_media_gallery_value as v
inner join catalog_product_entity_media_gallery as g on g.value_id=v.value_id
where entity_id=1802
order by position
*/
public function getAllImages(int $productId)
{
$select = $this->resourceConnection->getConnection()->select();
$select->from(['v' => $this->resourceConnection->getTableName('catalog_product_entity_media_gallery_value')]);
$select->join(
['g' => 'catalog_product_entity_media_gallery'],
'g.value_id=v.value_id',
[]
);
$select->where('entity_id=?', $productId);
$select->reset(\Magento\Framework\DB\Select::COLUMNS);
$select->columns(['v.value_id', 'v.position', 'g.value']);
$select->order('v.position');
return $this->resourceConnection->getConnection()->fetchAll($select);
}
/**
* @param int $productId
* select * from catalog_product_entity_media_gallery_value as v
inner join catalog_product_entity_media_gallery as g on g.value_id=v.value_id
where entity_id=1802
order by position
*/
public function getFirstImages(int $productId)
{
$select = $this->resourceConnection->getConnection()->select();
$select->from(['v' => $this->resourceConnection->getTableName('catalog_product_entity_media_gallery_value')]);
$select->join(
['g' => 'catalog_product_entity_media_gallery'],
'g.value_id=v.value_id',
[]
);
$select->where('entity_id=?', $productId);
$select->reset(\Magento\Framework\DB\Select::COLUMNS);
$select->columns(['g.value', 'v.position']);
$select->order('v.position');
//$select->limit(1);
$result = $this->resourceConnection->getConnection()->fetchAll($select);
$minPosition = 2000;
$position = [];
$minImage = '';
foreach ($result as $row) {
if ($row['position'] < $minPosition) {
$minPosition = $row['position'];
$minImage = $row['value'];
$position[$row['position']] = 1;
}
}
if (count($position) > 1) {
return $minImage;
}
}
public function updatePosition(int $valueId, int $position)
{
$this->resourceConnection->getConnection()->update(
$this->resourceConnection->getTableName('catalog_product_entity_media_gallery_value'),
['position' => $position],
['value_id=?' => $valueId]
);
}
}
\ No newline at end of file
<?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\CommandListInterface">
<arguments>
<argument name="commands" xsi:type="array">
<item name="joshine_fix-base-image" xsi:type="object">Joshine\Script\Console\Command\ProductFirstImageAsBaseFixer</item>
</argument>
</arguments>
</type>
</config>
\ No newline at end of file
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Joshine_Script" setup_version="0.0.1"/>
</config>
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Joshine_Script',
__DIR__
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment