0

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';
 }
}
asked Jan 15, 2024 at 18:08

3 Answers 3

2

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();
}
answered Jan 15, 2024 at 19:48
1
  • thanks, but it didn't help. attribute is created for sample data products but remains disabled. Commented Jan 16, 2024 at 12:31
0

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);
answered Jan 16, 2024 at 14:37
0
 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
 );
 }
 }

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.