Thêm file di.xml
File di.xml có nội dung như sau
1 2 3 4 5 | <config> <type name="Magento\Catalog\Model\Product"> <plugin name="change_product" type="Laptrinhinfo\HelloWorld\Plugin\Product" sortOrder="1" disabled="true"/> </type> </config> |
Thay đổi giá sản phẩm
Ta sẽ tạo file app/code/Laptrinhinfo/HelloWorld/Plugin/Product.php, trong class Product, phương thức afterGetPrice sẽ giúp bạn thay đổi giá của sản phẩm. Ở đây tôi sẽ cho phép mỗi sản phẩm có giá ban đầu + 100
1 2 3 4 5 6 7 8 9 10 11 | <?php namespace Laptrinhinfo\HelloWorld\Plugin; class Product { public function afterGetPrice(\Magento\Catalog\Model\Product $subject, $result) { return $result + 100; } } |
Flush cache và test lại
Bước cuối cùng là bạn flush cache của Magento 2 và ra ngoài frontend xem giá sản phẩm đã thay đổi chưa nhé.