我需要从\ Magento \ Checkout \ Model \ Cart覆盖updateItems函数 还需要在__construct arguments中传递我的自定义帮助程序类。这是我的重写类的__construct函数
namespace Vendor\Module\Model;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Checkout\Model\Session;
Class Cart extends \Magento\Checkout\Model\Cart
{
public function __construct(\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Checkout\Model\ResourceModel\Cart $resourceCart, Session $checkoutSession, \Magento\Customer\Model\Session $customerSession, \Magento\Framework\Message\ManagerInterface $messageManager, \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry, \Magento\CatalogInventory\Api\StockStateInterface $stockState, \Magento\Quote\Api\CartRepositoryInterface $quoteRepository, ProductRepositoryInterface $productRepository,
\Vendor\Module\Helper\Data $helper, array $data = []
)
{
$this->helper = $helper;
parent::__construct($eventManager, $scopeConfig, $storeManager, $resourceCart, $checkoutSession, $customerSession, $messageManager, $stockRegistry, $stockState, $quoteRepository, $productRepository, $data);
}
}
此后,我运行setup:upgrade,compile,static内容部署命令。还要删除var中的所有文件夹。但是当我在__construct函数中传递参数时。它不起作用。它显示空白页。当我从__construct函数中删除参数时。然后页面正在加载。
答案 0 :(得分:0)
如果您想从结帐购物车模型类中覆盖@Injectable()
export class SomethingResolver implements Resolve<Action> {
constructor (
private store: Store<State>,
private action$: Actions
) {}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Action> {
this.store.dispatch({ type: Actions.FETCH_SOMETHING });
const responseOK = this.action$.ofType(Actions.RESPONSE_OK);
const responseError = this.action$.ofType(Actions.RESPONSE_ERROR)
.do(() => this.router.navigate(['']);
return Observable.race(responseOk, responseError).take(1);
}
}
,则必须在模块updateItems
文件中添加preference
,如下所示:
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">
<preference for="Magento\Checkout\Model\Cart" type="[Vendor_Name]\[Module_Name]\Model\Cart" />
</config>
应该如下所示:
app/code/[Vendor_Name]/[Module_Name]/Model/Cart.php
然后编译依赖项,仅此而已。阅读本文以了解有关Overriding classes in Magento 2
的更多信息