0

I'm adding and removing custom options programmatically like so:

$product = $this->getProductModel()
    ->setStoreId($taskStore)
    ->load($productId);
$optionInstance = $product->getOptionInstance();
$optionInstance->setProduct($product);
$optionInstance->setOptions($customOptions);

If $taskStore is not 0, this adds store-level overrides, which is what I want. However, sometimes I want to only set a store-level override on the titles of dropdown values, not the title of the option itself. If I set the option title to false, it still has a blank store-level override:

foreach ($customOptions as &$customOption) {
    $customOption['title'] = false;
}
unset($customOption);
$product = $this->getProductModel()
    ->setStoreId($taskStore)
    ->load($productId);
$optionInstance = $product->getOptionInstance();
$optionInstance->setProduct($product);
$optionInstance->setOptions($customOptions);

The result:

enter image description here

The goal:

enter image description here

How can I set the title of a custom option to use the default global-scope value while still setting store-level overrides on the values?

1 Answer 1

0

By logging $this->getData() just prior to $this-save() in the saveOptions() method of Mage/Catalog/Model/Product/Option.php and then saving the product from the Magento admin page (with Use Default selected on the option title and first value title), I finally figured it out. The logging gave me this result:

{
    "is_delete": "",
    "previous_type": "drop_down",
    "previous_group": "select",
    "id": "9094",
    "option_id": "9094",
    "scope": {
        "title": "1"
    },
    "type": "drop_down",
    "is_require": "1",
    "sort_order": "1",
    "quantity_options_checkbox": "0",
    "values": {
        "18781": {
            "option_type_id": "18781",
            "is_delete": "",
            "scope": {
                "title": "1"
            },
            "price": "0.00",
            "price_type": "fixed",
            "sku": "",
            "sort_order": "1"
        },
        "18782": {
            "option_type_id": "18782",
            "is_delete": "",
            "title": "Large (12x16\")",
            "price": "0.00",
            "price_type": "fixed",
            "sku": "",
            "sort_order": "2"
        }
    },
    "product_id": "6405",
    "store_id": "1"
}

Which tells me all I need to do is add a new scope key that specifies which fields should be global-scope:

foreach ($customOptions as &$customOption) {
    $customOption['scope'] = ['title' => '1']; // 1 == Global scope
}
unset($customOption);
$product = $this->getProductModel()
    ->setStoreId($taskStore)
    ->load($productId);
$optionInstance = $product->getOptionInstance();
$optionInstance->setProduct($product);
$optionInstance->setOptions($customOptions);

Knowing how weird and complex the custom option tables are in the database, I know why they did it this way, but it was completely unintuitive. I just wish I'd thought of logging the model save earlier.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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