0

In my custom module, I am able to set the description of an existing product successfully using the code below & the product url shows correct output for description:

$product = $this->productRepository->get('q783');
$product->setData('description','hello world');
$this->productRepository->save($product);

However, this programmatically saved description doesn't show up on the admin side rather the edit with page builder form for description shows old values. When you try to update the description using page builder from the browser after having updated it once via php code, the browser edited form does in fact show newer values on the admin side but there is no change on the product url's description despite multiple cache refreshes and reindexing.

Is this expected behavior? Do I need to store description using php code in a different manner so that the saved shows up in edit with page builder section for description on the admin side?

As it stands now, both php code set and manually filled description using edit with page builder are out of sync albeit the actual product url is continuously showing php code set descriptions, no matter how many times I update the product form via browser with newer description values in page builder.

1
  • Are you using a single store setup or a multi-store setup? Feb 20 at 9:44

1 Answer 1

1

We faced the same issue while saving products using REST API. The issue is already raised in Github.

https://github.com/magento/magento2/issues/8897

The issue:

While saving the product with ProductRepository the \Magento\Catalog\Api\ProductRepositoryInterface::save will take the action.

Implementation of \Magento\Catalog\Api\ProductRepositoryInterface::save loads product entity by ID according to the current store, populates product attributes with new values, and saves the entire product in the scope of the current store.

It does not consider whether some attributes had values in this scope before or used default values.

The solution depends on whether you want to set product attribure value at store or default scope.

Solution 1:

Set NULL values for attributes for which you want to preserve inheritance (use default values).

Solution 2:

Save product using the emulation to set your script to default store id 0

use Magento\Store\Model\App\Emulation;
use Magento\Framework\App\Area;

public function __construct(
    FilterFactory $filter,
    Emulation $emulation
){
    $this->filter = $filter;
    $this->emulation = $emulation;
}

.....
    $this->emulation->startEnvironmentEmulation(0,Area::AREA_ADMINHTML);
    ....
    ....
    $productRepository->save($product);
    $this->emulation->stopEnvironmentEmulation();
...

Solution 3:

Use "updateAttributes" method of class \Magento\Catalog\Model\Product\Action to update specific attribute for product instead of updating all of them.

$ids = [$productIds]; //array of product ids
$attrData = [
    'description' => $description;   //specify array of attributes to modify and their values
];
$storeId = //current store id;

/** @var \Magento\Catalog\Model\Product\Action $productAction */
$this->productAction->updateAttributes($ids, $attrData, $storeId);
1
  • This was indeed scope related. When I open the product for editing it opens with "All Store Views" scope but the code is updating "Default Store View". When I switch to the latter in the browser, the page builder is indeed showing the code-updated text.
    – raza19
    Feb 20 at 12:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.