Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Cnc/SerialNumber/Console/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/SerialNumber/Console/ImportVhs.php
<?php
/**
 * Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved.
 * https://opensource.org/licenses/OSL-3.0  Open Software License (OSL 3.0)
 * Cnc
 * Krzysztof Majkowski <[email protected]>
 */

namespace Cnc\SerialNumber\Console;

use Cnc\Catalog\Model\Attribute\MsiAttributes;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\ResourceConnection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ImportVhs extends Command
{
    /**
     * @var CollectionFactory
     */
    private $collectionFactory;

    /**
     * @var ResourceConnection
     */
    private $resourceConnection;

    /**
     * @var MsiAttributes
     */
    private $msiAttributes;

    /**
     * ImportVhs constructor.
     * @param CollectionFactory $collectionFactory
     * @param ResourceConnection $resourceConnection
     * @param MsiAttributes $msiAttributes
     * @param string|null $name
     */
    public function __construct(
        CollectionFactory $collectionFactory,
        ResourceConnection $resourceConnection,
        MsiAttributes $msiAttributes,
        string $name = null
    ) {
        parent::__construct($name);
        $this->collectionFactory = $collectionFactory;
        $this->resourceConnection = $resourceConnection;
        $this->msiAttributes = $msiAttributes;
    }

    /**
     * @inheritDoc
     */
    protected function configure()
    {
        $this->setName('catalog:serial_number:import_vhs');
        $this->setDescription('Import VHS');
        parent::configure();
    }

    /**
     * CLI command description
     *
     * @param InputInterface $input
     * @param OutputInterface $output
     *
     * @return void
     */
    protected function execute(InputInterface $input, OutputInterface $output): void
    {
        $collection = $this->collectionFactory->create();
        $collection->addAttributeToFilter('cnc_salable_out_of_stock', ['gteq' => 1]);
        $collection->addAttributeToFilter('type_id', 'simple');
        $skus = [];
        foreach ($collection as $product) {
            $skus []= $product->getSku();
        }

        $attributeId = $this->msiAttributes->getAttributeId('cnc_vhs');
        if (!$attributeId) {
            $output->writeln('[ERROR] Attribute cnc_vhs not found.');
            return;
        }

        $connection = $this->resourceConnection->getConnection();
        $connection->delete('msi_custom_attribute_value', 'attribute_id = ' . $attributeId);
        $tableName = $connection->getTableName('msi_custom_attribute_value');

        foreach ($skus as $sku) {
            $connection->insertOnDuplicate(
                $tableName,
                [
                    'source_code' => 'cnc_fr',
                    'sku' => $sku,
                    'attribute_id' => $attributeId,
                    'value' => 1
                ]
            );
        }

        $output->writeln('[OK] cnc_vhs set to '. count($skus) . ' products');
    }
}

Spamworldpro Mini