2

Is it possible to have a product attribute like 'custom price' that is global and can be set on a configurable product in a way that the price for the item itself can be pulled from that field and override all the other places price can be set?

Right now I'm basically trying to avoid having to update the price for all of the simple products on a given configurable product. I'd really like to find a way to pull it from just one field.

0

The best way for handling your situation would be to update the prices for the simple products. Consider using Magento's import to update prices that should go fast enough whithout spending to much time on that. Here is the documentation: https://docs.magento.com/m2/ce/user_guide/system/data-import.html

A second option is to hook in at catalog_product_save_after event, check if it is a confugurable product and modify the prices of all children. Some observer code like the following should work:

//observer code
if($product->getTypeId() == \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE){
    $children = $product->getTypeInstance()->getUsedProducts($configProduct);
    foreach ($children as $child){ 
        $child->setPrice($product->getYourPriceAttribute())->save();
     }
}

If you really want to modify Magento's price calculation you can create a plugin after the method getFinalPrice() in the Product model and manipulate the price the way you want (for example taking the price from somewhere else). If there are no other changes to Magento's price calculation in your system yet, you should be able to change your prices with that approach. Depending on your expectations (for example should catalog price rules work on your prices you may place your plugin before the getFinalPrice() method or even after getPrice() method.

Anyway I strongly recommend the usage of Magento's import! If for what ever reason that's not possible, try the observer approach.

I hope that helps. Feedback would be nice :-)

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.