2

I need to override Price.php Model with plugin. I've created PricePlugin inside Plugin folder.

<vendor>/<module>/Plugin/Product/View/Options/PricePlugin.php

PricePlugin.php :

<?php
namespace <vendor>\<module>\Plugin\Product\View\Options;

use \Magento\Catalog\Model\Config\Source\Product\Options\Price;

/**
 * Class AbstractOptions
 * @package <vendor>\<module>\Plugin\Product\View\Options
 */
class PricePlugin
{
    const VALUE_FIXED = 'fixed';
    const VALUE_PERCENT = 'percent';

    /**
     * @param Price $price
     * @return array
     */
    public function toOptionArray(Price $price)
    {
        return [
            ['value' => self::VALUE_FIXED, 'label' => __('Fixed')],
            ['value' => self::VALUE_FIXED, 'label' => __('Per Character')],
            ['value' => self::VALUE_PERCENT, 'label' => __('Percent')],
        ];
    }
}

di.xml

<type name="Magento\Catalog\Model\Config\Source\Product\Options\Price">
    <plugin name="VENDOR_MODULE_Model_Price" type="<Vendor>\<Module>\Plugin\Product\View\Options\PricePlugin" sortOrder="1" />
</type>
0

1 Answer 1

3

You are almost there , plugins can be used on three ways that are after , before and around . You just need to preappend any of this three to the method you want to make changes .

so change

toOptionArray(Price $price)

to

aftertoOptionArray(Price $price)

Also you can manipulate the result from the method you are overriding like you have done it like

public function aftertoOptionArray(Price $price)
    {
        return [
            ['value' => self::VALUE_FIXED, 'label' => __('Fixed')],
            ['value' => self::VALUE_FIXED, 'label' => __('Per Character')],
            ['value' => self::VALUE_PERCENT, 'label' => __('Percent')],
        ];
    }

you can change it to

public function aftertoOptionArray(Price $price, $OptionsArray)
    {
        // $OptionsArray here holds the values from the method toOptionArray() so you can simply add your value here which is ['value' => self::VALUE_FIXED, 'label' => __('Per Character')] into that array .
    }

Hope this helps

0

Your Answer

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

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