注册后在Shopware订户中的记帐地址

问题描述

早上好

由于许多小时,我尝试在Shopware 6 Subscriber中读取客户帐单地址。 在控制器中,我可以使用:

$SalesChannelContext->getCustomer()->getActiveBillingAddress() 

但在Subscribers-> EntityWrittenEvent中不提供SalesChannelContext,因此该方法无效。通过services.xml进行注入是行不通的(因为SCC不是服务?) 我试图像这样通过客户实体获取客户数据:

$customerRepo = $this->container->get('customer.repository');
$criteria = (new Criteria())
              ->addFilter(new EqualsFilter('id',$id));
       
$customer = $customerRepo->search( $criteria,$context);

找到了客户,但是地址(开票和交货)为空,所以那也是错误的方式。 有没有人想解决这个问题?

我需要什么:获取帐单地址的方法 我得到什么:地址为=> null的客户对象 解决方法:使saleschannelcontext可用是最好的主意?!

感谢您的时间和帮助

解决方法

您订阅哪个活动?许多事件都将SalesChannelContext作为属性,也许您可​​以从那里获取。

SalesChannelContext不是服务,因为它是代表请求当前状态的对象。因此,必须根据每个请求重新创建一些内容。

如果您有客户ID,则已经很容易获得其数据。您所缺少的是与帐单地址的关联。默认情况下,由于性能原因,关联通常不会随主要实体一起加载。但您可以将它们添加到Criteria对象中:

$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('id',$id));
$criteria->addAssociation('defaultBillingAddress');
// $criteria->addAssociation('activeBillingAddress'); if you want the current active address,might differ from the default one
// $criteria->addAssociation('addresses'); if you want all addresses
       
$customer = $this->customerRepo->search( $criteria,$context);

但是在选择另一个查询来选择客户之前,我会尝试从事件中获取SalesChannelContext

,

salesChannelContext不可全局使用,因为仅在请求来自店面时才可用。例如,如果您在Admin-API上下文,甚至在CLI上下文中,则没有可用的SalesChannelContext。

通常,从数据库中获取客户的解决方案看起来像是正确的方法。

由于Shopware DAL(DataAbstractionLayer)默认情况下不加载实体的关联,您遇到了问题,您必须事先定义要与主要实体加载的关联。

在您的情况下,这将通过以下方式完成:

$customerRepo = $this->container->get('customer.repository');
$criteria = (new Criteria())
              ->addFilter(new EqualsFilter('id',$id))
              ->addAssociation('defaultBillingAddress');
       
$customer = $customerRepo->search( $criteria,$context);
$customer->getDefautBillingAddress();

在此处通知addAssociation()呼叫。您可以在here中找到详细说明。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...