4

I'm trying to modify the weight of a product depending which custom options are selected. At the moment, I have a Plugin for Magento\Quote\Model\Quote\Weight with this function:

public function beforeSetProduct(\Magento\Quote\Model\Quote\Item $subject, $product) {}

..In which I can get/set the product weight successfully. At this point, though, I need to grab the weight from the selected customizable option and am at loss on how to do that. Any thoughts/ideas would be appreciated.

1
  • I've tried adding in the Product Configuration Helper as discussed in magento.stackexchange.com/a/175883/60595 but I'm not sure how to implement "getSelectedOptionsOfQuoteItem()" as seen in that example-- feeding it $subject within my beforeSetProduct hasn't worked.
    – brackfost
    Commented Mar 12, 2019 at 17:24

2 Answers 2

2

Add the following code in your plugin function and you will get the selected option value (I assume you have a drop down option and you identify the value of your custom weight by the label of the choosen option). You can find some explanation of what's going on in the comments. You only need to change the title of your custom option in the code.

$customOptions = $product->getCustomOptions();

if (isset($customOptions['option_ids'])){
    $optionIds = $customOptions['option_ids'];

    foreach (explode(',', $optionIds->getValue()) as $optionId) {
        $option = $product->getOptionById($optionId);
        if (isset($customOptions['option_'.$optionId])){

            //load the corrsponding quote item option object fo the given id
            $itemOption = $subject->getOptionByCode('option_'.$optionId);

            //create a model of the type of this option and load the data for the selected option value
            $group = $option->groupFactory($option->getType())
            ->setOption($option)
            ->setConfigurationItem($subject)
            ->setConfigurationItemOption($itemOption);

            //change here with the title of your custom option
            if ($option->getTitle() == "your_custom_weight_title"){
                //get the selected value, e.g. the label for drop down options
                $selectedWeightValue = $group->getPrintableOptionValue($itemOption->getValue());
            }
            //get the order value object to access it's data like price or sku
            $optionValue = $group->getOption()->getValueById($itemOption->getValue());
            $sku = $optionValue->getSku();
            $price = $optionValue->getPrice();
        } 
    }
}
3
  • Thanks a bunch @HelgeB! So at this point, the "getPrintableOptionValue" method successfully returns the Title of the option. Any thoughts on how I'd return one of the other fields (Price/Sku/etc) of the selected option? I've added a "Weight" field here with my module that I need to grab the value from.
    – brackfost
    Commented Mar 13, 2019 at 14:11
  • 1
    Since I don't know what you have done in your module, I can't tell you how to get that custom value. I've updated how to get the order value object with example for sku and price. Depending what you have done with your custom field, you may get it from the order value object.
    – HelgeB
    Commented Mar 13, 2019 at 15:15
  • That's exactly what I was looking for, thank you!
    – brackfost
    Commented Mar 13, 2019 at 16:03
0

Much simpler:

$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());

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.