My patch adds new attribute to products. I want it to be enabled on every product by default. However, it is always disabled on sample data products and works as intended when I try to add a completely new product from the admin panel. How to fix it?
<?php
namespace Alex\AskQuestion\Setup\Patch\Data;
use Magento\Catalog\Model\Product;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Eav\Model\Entity\Attribute\Source\Boolean;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Validator\ValidateException;
class AddAskQuestionAttribute implements DataPatchInterface
{
/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;
/**
* @var EavSetupFactory
*/
private $eavSetupFactory;
/**
* AddAskQuestionAttribute constructor.
*
* @param ModuleDataSetupInterface $moduleDataSetup
* @param EavSetupFactory $eavSetupFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
EavSetupFactory $eavSetupFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->eavSetupFactory = $eavSetupFactory;
}
/**
* Adding new attribute to product
*
* @return void
* @throws LocalizedException
* @throws ValidateException
*/
public function apply()
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute(
Product::ENTITY,
'allow_to_ask_questions',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Allow to ask question',
'input' => 'boolean',
'class' => '',
'source' => Boolean::class,
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => true,
'sort_order' => 90,
'user_defined' => false,
'default' => 1,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
}
/**
* @return array|string[]
*/
public static function getDependencies(): array
{
return [];
}
/**
* @return array|string[]
*/
public function getAliases(): array
{
return [];
}
/**
* @return string
*/
public static function getVersion(): string
{
return '1.0.2';
}
}
3 Answers 3
To ensure that your new attribute is enabled by default for all products, You can do this by creating a separate method in your patch class to update the attribute value for existing products.
private function updateDefaultAttributeValue()
{
$this->moduleDataSetup->startSetup();
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
$productCollection = $this->productCollectionFactory->create();
$productCollection->addAttributeToSelect('*');
foreach ($productCollection as $product) {
$product->setData('allow_to_ask_questions', 1);
$product->getResource()->saveAttribute($product, 'allow_to_ask_questions');
}
$this->moduleDataSetup->endSetup();
}
-
thanks, but it didn't help. attribute is created for sample data products but remains disabled.Dramorian– Dramorian2024年01月16日 12:31:14 +00:00Commented Jan 16, 2024 at 12:31
This solved my issue.
//set default value for the new custom attribute
$productIds = $this->collectionFactory->create()->getAllIds();
$this->action->updateAttributes($productIds, ['allow_to_ask_questions' => 1], 1);
public function apply()
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
$eavSetup->addAttribute(
Product::ENTITY,
'allow_to_ask_questions',
[
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Allow to ask question',
'input' => 'boolean',
'class' => '',
'source' => Boolean::class,
'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => true,
'sort_order' => 90,
'user_defined' => false,
// 'default' => 1,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'visible_on_front' => false,
'used_in_product_listing' => true,
'unique' => false,
'apply_to' => ''
]
);
$defaultValues = [
'allow_to_ask_questions' => 1,
];
foreach ($defaultValues as $attributeCode => $defaultValue) {
$eavSetup->updateAttribute(
Product::ENTITY,
$attributeCode,
'default',
$defaultValue
);
}
}