0

I have a module that was defining a custom product type: MyPT1

I added a second custom product type (MyPT2) using the same module.

But When I create a MyPT2 product in the backend, the price is not present.
I Think that it is due to the fact that InstallData.php is not run during setup:upgrade as the module was already installed.
I then would like to create a UpgradeData.php but I don't know how to create it correctly.

Here is my InstallData.php

<?php
namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    protected $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        $fieldList = [
            'price',
            'special_price',
            'special_from_date',
            'special_to_date',
            'minimal_price',
            'cost',
            'tier_price',
            'weight',
        ];

        foreach ($fieldList as $field) {
            $applyTo = explode(
                ',',
                $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $field, 'apply_to')
            );
            if (!in_array(\Vendor\Module\Model\Product\Type\MyPT1::TYPE_CODE, $applyTo)) {
                $applyTo[] = \Vendor\Module\Model\Product\Type\MyPT1::TYPE_CODE;
                $eavSetup->updateAttribute(
                    \Magento\Catalog\Model\Product::ENTITY,
                    $field,
                    'apply_to',
                    implode(',', $applyTo)
                );
            }

            if (!in_array(\Vendor\Module\Model\Product\Type\MyPT2::TYPE_CODE, $applyTo)) {
                $applyTo[] = \Vendor\Module\Model\Product\Type\MyPT2::TYPE_CODE;
                $eavSetup->updateAttribute(
                        \Magento\Catalog\Model\Product::ENTITY,
                        $field,
                        'apply_to',
                        implode(',', $applyTo)
                        );
            }
        }
    }
}

Thank you for your help,

0

This one should work:

<?php
namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeData implements InstallDataInterface
{
    protected $eavSetupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.1', '<')) {

            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

            $fieldList = [
                'price',
                'special_price',
                'special_from_date',
                'special_to_date',
                'minimal_price',
                'cost',
                'tier_price',
                'weight',
            ];

            foreach ($fieldList as $field) {
                $applyTo = explode(
                    ',',
                    $eavSetup->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $field, 'apply_to')
                );
                if (!in_array(\Vendor\Module\Model\Product\Type\MyPT1::TYPE_CODE, $applyTo)) {
                    $applyTo[] = \Vendor\Module\Model\Product\Type\MyPT1::TYPE_CODE;
                    $eavSetup->updateAttribute(
                        \Magento\Catalog\Model\Product::ENTITY,
                        $field,
                        'apply_to',
                        implode(',', $applyTo)
                    );
                }

                if (!in_array(\Vendor\Module\Model\Product\Type\MyPT2::TYPE_CODE, $applyTo)) {
                    $applyTo[] = \Vendor\Module\Model\Product\Type\MyPT2::TYPE_CODE;
                    $eavSetup->updateAttribute(
                            \Magento\Catalog\Model\Product::ENTITY,
                            $field,
                            'apply_to',
                            implode(',', $applyTo)
                            );
                }
            }
        }

        $setup->endSetup();
    }
}

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.