1

New to Magento and I am trying to figure out how to create a miscellaneous product that initially has a price of zero. I would like to have a field that the customer can enter any amount into and that become the new price of the product. I have added a text field option but have no idea how to link it to update the price. I have tried to create a event/observer but get lost in the process. Any help or at least pointing me in the right direction would be appreciated. I am using magento 2.2.5

  • 1
    jamil, I have played around with the plugin, but I do have other products that are not user defined price. I will attempt to create my own product, which does seem like the best way to go. Thanks for pointing me in the right direction and from the info your provided I believe that I should be able to manage. Thanks again. – Esseh CC Aug 17 '18 at 13:41
1

Plugin

You could look into using before after and around plugins and write a module to replace the price with $_POST information.

Magento\Catalog\Model\Product\Type\Price has a method called getPrice() and various product types can extend this. If your product is a default type, write an after method that returns the user provided information, but you probably want to check the value first. If your catalog has other items which do not have user-defined prices, or you plan to in the future, then you will need to use custom logic to determine if getPrice is being called on the right item.

Custom Product Type

Depending on your situation I would take it a step further and instead of using a Plugin, consider making a custom product type. With this you can define a price model specifically for your custom product type. To do this you make a file etc/product_types.xml in your module that looks like this:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_types.xsd">
    <type name="user_priced_product" label="Custom Product Type Label" modelInstance="Vendor\Module\Model\Product\Type">
        <priceModel instance="Vendor\Module\Model\Product\Price" />
    </type>
</config>

You then need a basic Product Model

namespace Vendor\Module\Model\Product;

class Type extends \Magento\Catalog\Model\Product\Type\AbstractType
{
    const TYPE_ID = 'user_priced_product';
    public function deleteTypeSpecificData(){
        // method is abstract so must be defined here
    }
}

Then Vendor\Module\Model\Product\Price would be a custom class with a getPrice() that only gets called on your product type.

namespace Vendor\Module\Model\Product;

class Price extends \Magento\Catalog\Model\Product\Type\Price {
    public function getPrice($product) {
        // code to get user input and return price here 
    }
}

The only disadvantage I have come across with custom product types is that many modules that extend functionality for products will only work for Magento's default product types and this can be limiting. I feel the organizational benefit is worth it.

So there's some code to get you started and at least a basic idea. I don't have time to write an entire module, but with this, and some research of your own you should be on the right path. Below are some links I used to write this answer and some that can help you.

Resources to help

Please note I am not affiliated with any of the above and haven't tested any of their code directly.

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.