Создать продукт из нестандартного типа продукта - PullRequest
0 голосов
/ 17 июня 2020

В magento 2 я создал собственный тип продукта. Эта часть работает

<?php

namespace Pack\TransportGratuit\Model\Product\Type;


use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product as Product;
use Magento\Catalog\Model\Product\Type\AbstractType as AbstractType;

/**
 * Class TransportGratuit
 * @package Pack\TransportGratuit\Model\Product\Type
 */
class TransportGratuit extends AbstractType
{

    public function __construct(\Magento\Catalog\Model\Product\Option $catalogProductOption, \Magento\Eav\Model\Config $eavConfig, \Magento\Catalog\Model\Product\Type $catalogProductType, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\MediaStorage\Helper\File\Storage\Database $fileStorageDb, \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Registry $coreRegistry, \Psr\Log\LoggerInterface $logger, ProductRepositoryInterface $productRepository, \Magento\Framework\Serialize\Serializer\Json $serializer = null)
    {
        $this->setTypeId('transport-gratuit');
        parent::__construct($catalogProductOption, $eavConfig, $catalogProductType, $eventManager, $fileStorageDb, $filesystem, $coreRegistry, $logger, $productRepository, $serializer);
    }

    /**
     * @param Product $product
     */
    public function deleteTypeSpecificData(Product $product)
    {
        // TODO: Implement deleteTypeSpecificData() method.
    }
}

С ее помощью я могу создать продукт с этим типом в бэк-офисе; но я хочу создать его, пока модуль установлен.

Итак, мне удалось это сделать

<?php


namespace Pack\TransportGratuit\Setup;


use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Catalog\Model\Product;

class InstallData implements InstallDataInterface
{

    /** @var \Magento\Framework\App\State **/
    private $state;

    public function __construct(\Magento\Framework\App\State $state) {
        $this->state = $state;
        $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        // TODO: Implement install() method.
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // instance of object manager
        $product = $objectManager->create(Product::class);
        $product->setSku('transport-gratuit');
        $product->setTypeId('transport-gratuit');
        $product->save();
    }
}

Проблема в том, что созданный продукт не имеет типа "TransportGratuit".

РЕДАКТИРОВАТЬ: Исправлено: я должен был использовать 'transport_gratuit'

1 Ответ

0 голосов
/ 18 июня 2020

Если вы хотите добавить новый тип продукта, вы должны выполнить два шага.

  • Создайте модель, которая простирается от Magento\Catalog\Model\Product\Type\AbstractType
  • Создайте etc/product_types.xml, в котором вы определяете свой новый тип продукта и ссылаетесь на модель

Кажется, вы забыли второй шаг, определяя etc/product_types.xml и ссылаясь на созданную вами модель.

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_types.xsd">
    <type label="your-product-type-label" modelInstance="your-product-type-model" name="your-product-type-name" />
</config>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...