0

I have tried to set custom option price when order is placed the price should be stored in sales_order_item table in custom_option column and tried to create plugin for this but the plugin is not working. I have done debug and found that COre magento custom option data is set from this abstract class :

This is magento core function behavior of custom option.

vendor/magento/module-catalog/Model/Product/Type/AbstractType.php

/**
     * Prepare additional options/information for order item which will be
     * created from this product
     *
     * @param \Magento\Catalog\Model\Product $product
     * @return array
     */
    public function getOrderOptions($product)
    {
        $optionArr = [];
        $info = $product->getCustomOption('info_buyRequest');
        if ($info) {
            $optionArr['info_buyRequest'] = $this->serializer->unserialize($info->getValue());
        }

        $optionIds = $product->getCustomOption('option_ids');
        if ($optionIds) {
            foreach (explode(',', $optionIds->getValue()) as $optionId) {
                $option = $product->getOptionById($optionId);
                if ($option) {
                    $confItemOption = $product->getCustomOption(self::OPTION_PREFIX . $option->getId());

                    $group = $option->groupFactory($option->getType())
                        ->setOption($option)
                        ->setProduct($product)
                        ->setConfigurationItemOption($confItemOption);

                    $optionArr['options'][] = [
                        'label' => $option->getTitle(),
                        'value' => $group->getFormattedOptionValue($confItemOption->getValue()),
                        'print_value' => $group->getPrintableOptionValue($confItemOption->getValue()),
                        'option_id' => $option->getId(),
                        'option_type' => $option->getType(),
                        'option_value' => $confItemOption->getValue(),
                        'custom_view' => $group->isCustomizedView(),                        ];
                }
            }
        }

        $productTypeConfig = $product->getCustomOption('product_type');
        if ($productTypeConfig) {
            $optionArr['super_product_config'] = [
                'product_code' => $productTypeConfig->getCode(),
                'product_type' => $productTypeConfig->getValue(),
                'product_id' => $productTypeConfig->getProductId(),
            ];
        }

        return $optionArr;
    }

In this core file if i add this key and value in existing array then i am able to get price in transactional email.

'price' => $group->getOptionPrice($confItemOption->getValue(), 0)

Could any one suggest that how could we save custom option price in order and get it in emails.

2 Answers 2

0

For temporary fix i have used the below code in block and used in email items phtml file.

Please Post answer if there is better way available to achieve it.

app/code/[Vendor_Name]/[Module_Name]/view/frontend/templates/email/items.phtml

/**
     * Returns array of Item options
     *
     * @param object $_item
     * @return array
     */
    public function getItemOptions($_item)
    {
        $result = [[]];
        if ($options = $_item->getProductOptions()) {
            if (isset($options['options'])) {
                $result[] = $options['options'];
            }
            if (isset($options['additional_options'])) {
                $result[] = $options['additional_options'];
            }
            if (isset($options['attributes_info'])) {
                $result[] = $options['attributes_info'];
            }
        }
        return array_merge(...$result);
    }

    /**
     * Return option price
     *
     * @param [] $option
     * @return int | bool
     */
    public function getCustomOptionPrice($option)
    {
        $price = null;
        if (isset($option['option_value'])) {
            $optionValueIds = explode(',', $option['option_value']);
            if (!empty($optionValueIds)) {
                foreach ($optionValueIds as $key => $value) {
                    $select = $this->resourceConnection->select()->from(
                        ['potp' => $this->resourceConnection->getTableName('catalog_product_option_type_price')],
                        [
                            'potp.price'
                        ]
                    )->where('potp.option_type_id = ?', $value);
                    $result = $this->resourceConnection->fetchRow($select);
                    if ($result && isset($result['price'])) {
                        $price += $result['price'];
                    }
                }
            }
        }
        return $price;
    }
0
$price = $option['option_value'];
$quoteItem->setCustomPrice($price);
$quoteItem->setOriginalCustomPrice($price);
$product->setIsSuperMode(true);

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.