Skip to content

Incorrect Scope Assigned to Price Attributes at Creation #39992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright 2025 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Catalog\Observer;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Helper\Data;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\Store;

/**
* Observer is responsible for changing scope for new price attributes in system
* depending on 'Catalog Price Scope' configuration parameter
*/
class ChangePriceAttributeScopeOnCreate implements ObserverInterface
{
/**
* @param Data $catalogData
*/
public function __construct(
private Data $catalogData
) {
}

/**
* Change scope for price attribute when create
*
* @param EventObserver $observer
* @return $this
*/
public function execute(EventObserver $observer)
{
$attribute = $observer->getEvent()->getAttribute();
if (empty($attribute->getId) && $attribute->getFrontendInput() == 'price') {
$scope = $this->catalogData->getPriceScope();
$scope = ($scope == Store::PRICE_SCOPE_WEBSITE)
? ProductAttributeInterface::SCOPE_WEBSITE_TEXT
: ProductAttributeInterface::SCOPE_GLOBAL_TEXT;
$attribute->setScope($scope);
}
return $this;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Catalog/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@
<event name="admin_system_config_changed_section_general">
<observer name="move_store_level_catalog_data_to_website_scope_on_single_store_mode" instance="Magento\Catalog\Observer\MoveStoreLevelCatalogDataToWebsiteScopeOnSingleStoreMode" />
</event>
<event name="catalog_entity_attribute_save_before">
<observer name="catalog_attribute_price_scope_change" instance="Magento\Catalog\Observer\ChangePriceAttributeScopeOnCreate" />
</event>
</config>
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
* Copyright 2019 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\Product\Attribute\Save;

use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Eav\Model\Entity\Attribute\Exception;

/**
Expand Down Expand Up @@ -51,6 +52,15 @@ public function testDefaultValue(string $productSku): void
// product price attribute does not support default value
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_decimal_attribute.php
* @magentoConfigFixture current_store catalog/price/scope 1
*/
public function testScopePriceAttribute()
{
$this->assertTrue($this->getAttribute()->isScopeWebsite());
}

/**
* @inheritdoc
*/
Expand Down