我有一个带有 Drupal 8.6 和 Commerce 2.11 的站点
我有几家属于不同所有者的商店(maketplace)。
当一个客户在 2 个商店中,这将创建 2 个购物车。
如何在购物车上方显示店铺名称?
我在自定义模块中创建了以下 4 个文件。
我的问题 :
经过身份验证的用户将产品添加到他的购物车,当他进入购物车页面时出现 403 错误。
我检查了权限,它们设置正确。
如果我卸载我的自定义模块,购物车可以再次访问。
为什么 ?
commerce_marketplace_cart.info.yml
:
name: Commerce Marketplace Cart
description: Implements Commerce Marketplace Cart.
type: module
core: 8.x
package: Commerce (contrib)
dependencies:
- commerce:commerce_cart
- commerce:commerce_product
commerce_marketplace_cart.module
:
<?php
/**
* @file
* Commerce Marketplace Cart.
*/
commerce_marketplace_cart.routing.yml
:
commerce_cart.page:
path: '/cart'
defaults:
_controller: '\Drupal\commerce_marketplace_cart\Controller\MarketplaceCartController::cartPage'
_title: 'Shopping carts'
requirements:
_permission: 'access cart'
在文件夹/src/Controller
中有文件MarketplaceCartController.php
:
<?php
namespace Drupal\commerce_marketplace_cart\Controller;
use Drupal\commerce_cart\Controller\CartController;
/**
* Overrides the cart page controller.
*/
class MarketplaceCartController extends CartController {
/**
* {@inheritdoc}
*/
public function cartPage() {
$build = parent::cartPage();
$carts = $this->cartProvider->getCarts();
$carts = array_filter($carts, function ($cart) {
/** @var \Drupal\commerce_order\Entity\OrderInterface $cart */
return $cart->hasItems();
});
if (!isset($build['empty'])) {
foreach ($build as $key => $value) {
if (isset($value['#prefix'])) {
$store_name = $carts[$key]->getStore()->getName();
$build[$key]['#prefix'] = "<h2 class='cart cart-store-name'>{$store_name}</h2>" . $value['#prefix'];
}
}
}
return $build;
}
}