0

I'm using a script to programmatically update some products, bootstrapping Magento. I've had a lot of luck saving attributes using the product repository, when it comes to customizable options, I'm confused about the best way to update existing values.

I'm able to load up all of the options I need to update using Magento\Catalog\Model\ResourceModel\Product\Option\CollectionFactory, but not sure how to save them from there.

Do you have to save option data as an array, or is it possible to update an individual field?

asked Aug 30, 2019 at 18:58
1
  • can you provide some code which you have done? Commented Sep 7, 2019 at 12:34

2 Answers 2

0

In this case, it ended up making the most sense for us to update the values with a SQL query.

In my script, I added:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('catalog_product_option_type_value');
$sql = "Update " . $tableName . " Set custom_weight = " . $value . " where option_type_id = " . $valuesArray['option_type_id'] . "\n";
$connection->query($sql);

We have a custom_weight field for our options. We needed to update a bunch of values as pulled from a CSV with some logic thrown in there. The $sql assignment and the $connection->query($sql) line occur, in actuality, within a foreach loop.

answered Sep 12, 2019 at 21:03
-1

At first create an observer with event - catalog_product_save_before In the observer place the below code -

/** Dependency classes **/
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
use Magento\Catalog\Model\Product\OptionFactory;\

public function __construct( OptionFactory $productOptionFactory ) { $this->productOptionFactory = $productOptionFactory; }

$product = $observer->getEvent()->getProduct(); $exist = false; //check if the custom option exists foreach ($product->getOptions() as $option) { if ($option->getGroupByType() == ProductCustomOptionInterface::OPTION_TYPE_FIELD && $option->getTitle() == 'Custom Option') { $exist = true; } } if (!$exist) { try { $optionArray = [ 'title' => 'Custom Option', 'type' => 'field', 'is_require' => false, 'sort_order' => 1, 'price' => 0, 'price_type' => 'fixed', 'sku' => '', 'max_characters' => 0 ]; $option = $this->productOptionFactory->create(); $option->setProductId($product->getId()) ->setStoreId($product->getStoreId()) ->addData($optionArray); $product->addOption($option); } catch (\Exception $e) { //throw new CouldNotSaveException(__('Something went wrong while saving option.')); } }
answered Sep 10, 2019 at 5:36
2
  • Curious, why would an observer be necessary? I'm running a script that bootstraps Magento, so I'm updating the products that way. Commented Sep 10, 2019 at 16:38
  • Also, if I'm reading this correctly, this creates options that don't already exist, whereas I'd like to update the values of existing options. Commented Sep 10, 2019 at 17:05

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.