6

I am working on Magento 2 and having issue with price.

I want to customise the product price. I did it via plugin and it's working fine on product page and list page.

As we are using a plugin, it is not overriding the price in collection with custom price. Due to this price filter is also not working according to updated product price. As we are using price slider-Min - Max price is also not correct.

I have debugged a lot and here is my findings

Min-max price is calculating from product collection by using following methods.

$productCollection->getMinPrice();

$productCollection->getMaxPrice();

Product collections we are getting on product list page is not including the customised price. I think, if we set customised price to product collection then price thing will work fine.

Here is plugin which I am using to customise price.

1.etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Pricing\Price\RegularPrice">
    <plugin name="product_price_change"
            type="Namespace\Modulename\Plugin\Updateprice"
            sortOrder="1"/>
</type>
</config>

2.Plugin/Updateprice.php

<?php
namespace Namespace\Modulename\Plugin;

class Updateprice
{
    public function afterGetValue(\Magento\Catalog\Pricing\Price\RegularPrice $subject, $result) {

        return $result + $myCustomPriceCharges;

    }
}

Any suggestion would be appreciated. Thanks

  • What is reason behind changing price logic? Could you please tell me what is business requirements? – Pramod Kharade Jan 26 '18 at 2:18
  • I want to add extra charges into product price according to attribute value. – Chander Shekhar Jan 26 '18 at 6:13
  • Hello @Pramod , If you have any idea Please share. – Chander Shekhar Jan 29 '18 at 11:55
  • 1
    @PramodKharade : Reason could be anything. Why are you asking for business requirements instead providing solutions ? – zed Blackbeard Jan 29 '18 at 13:39
  • @ChanderShekhar : I am also facing the same issue. Please post the answer once get the solutions. – zed Blackbeard Jan 31 '18 at 5:55
1

You are right the functions getMinPrice() and getMaxPrice in \Magento\Catalog\Model\ResourceModel\Product\Collection class will need to be updated to include the custom charges.

You can write your own implementation of those methods by utilizing di.xml. However, I suspect you will have to implement codes to change the behaviors of more classes to make this work entirely (I don't know for sure but likely that the indexers class).

Also, consider how promotional rules work with your setup. Tax calculation is another area to look into.

Alternatively, I'd suggest that you make your price inclusive of customs charges. In your template, you can display the product price as the + custom charge amount (presumably the customs charge is stored in a product attribute).

So for example if product A price is $80 + $20. It is easier to store A's price as $100 and modify your template to display:

$priceBefore = $product->getPrice() - $product->getCustomCharge();

Hope this helps.

  • Thanks for sharing your Idea. Custom change is adding to product dynamically. – Chander Shekhar Jan 31 '18 at 13:56
  • if you can let us know a bit more about the business application maybe we can suggest something – William Tran Jan 31 '18 at 21:30
  • Everything is already explained in Question. We just need to add some custom charges into product price on frontend every where in collection too. Please Share your idea. – Chander Shekhar Feb 1 '18 at 6:34
0

Add in di.xml

<config>
  <type name="Magento\Catalog\Model\Product">
    <plugin name="change_product" type=" Vendor\Module\Plugin\Product " />
  </type>
</config>

create file in Vendor\Module\Plugin\Product.php

<?php

namespace Vendor\Module\Plugin;

class Product
{
    public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result)
    {
        customPrice=10;  
        return $result*$customPrice;
    }
}
0

If you only need to adjust the layered navigation price filter and all your products have the extra custom charge, you can create a plugin to add your custom charge to the getMinPrice() and getMaxPrice() methods.

You do that the same way you added it to the regular price.

.../etc/di.xml

<type name="Magento\Catalog\Model\ResourceModel\Product\Collection">
    <plugin name="adjust_collection_min_max_price" type="{Vendor}\{Module}\Plugin\Model\ResourceModel\Product\Collection" />
</type>

.../Plugin/Model/ResourceModel/Product/Collection.php

<?php
namespace Vendor\Module\Plugin\Model\ResourceModel\Product;
class Collection
{
    public function afterGetMinPrice(\Magento\Catalog\Model\ResourceModel\Product\Collection $subject, $result) {

        return $result + $myCustomPriceCharges;

    }

    public function afterGetMaxPrice(\Magento\Catalog\Model\ResourceModel\Product\Collection $subject, $result) {

        return $result + $myCustomPriceCharges;

    }
}

If only some of your products have the extra charge, then the above won't work correctly for products that don't have the charge.

In this case you should consider alternative approaches instead of plugins.

For example: Catalog -> Products -> Custom Options -> Add a required option that adds the extra charge only to products that should have your extra charge.

  • It is showing correctly in navigation but when you use to filter, filter collection is not working fine. – zed Blackbeard Feb 6 '18 at 4:34
  • Are you using an extension for layered navigation? Is yes, which one? If you're using Mageplaza's Layered navigation (as I am), you will most probably have to add your custom charge to one of the JS files, for example in my case would be /web/js/price/slider.js or /web/js/view/layer.js. If you have another extension, you would need to check it's JS files and figure it out (I'd search for something like "min", "max", "updatePrice" inside the JS files). – Lez Feb 6 '18 at 8:54

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.