Overriding Magento Controller
---o0o---
Note: Controller chứ không phải controllers
Cần phân biệt 2 thư mục controllers và Controller trong Magento Module. Với controllers ta rewrite theo cách thông thường. Còn với Controller ta rewrite theo cách sau
Ví dụ cụ thể: Bỏ bước review khi checkout bằng Paypal
Để bỏ qua bước review cần sửa code ở 2 file sau
File thứ 1:
\app\code\core\Mage\Paypal\Controller\Express\Abstract.php
Tìm tới hàm:
returnAction()
Tìm tới dòng:
$this->_redirect('*/*/review');
Sửa dòng này thành:
$this->_redirect('*/*/placeOrder');
File thứ 2:
\app\code\core\Mage\Paypal\Model\Config.php
Tìm tới hàm: getExpressCheckoutStartUrl()
public function getExpressCheckoutStartUrl($token)
{
return $this->getPaypalUrl(array(
'cmd' => '_express-checkout',
'token' => $token,
));
}
Sửa hàm này thành:
public function getExpressCheckoutStartUrl($token)
{
return $this->getPaypalUrl(array(
'cmd' => '_express-checkout',
'useraction' => 'commit',
'token' => $token,
));
}
Vậy là ta đã biết cần sửa ở đâu, nhưng không thể vào trong core sửa được nên cần có bước rewrite
Bạn chỉ cần copy 2 file ra thư mục local/Mage và sửa như trên
Nghĩa là, ta tạo ra 2 file mới
/app/code/local/Mage/Paypal/Controller/Express/Abstract.php
/app/code/local/Mage/Paypal/Model/Config.php