Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento 1&2:加入收藏以获得客户全名

本文介绍了如何在任何自定义集合上使用联接查询来获取客户的名字,姓氏以及联系人的名字和姓氏以获取客户全名。

我将展示Magento 1.x版和Magento 2.x版的示例代码。

Magento 1:加入收藏以获得客户全名

我想您已经从包含的任何表中获得了一个集合customer_id

$collection = Mage::getModel('yourModule/yourModel')->getCollection();

现在,我们获取客户姓氏和客户姓氏的属性ID。

$firstnameAttributeId = Mage::getModel('eav/entity_attribute')->loadByCode('customer', 'firstname')->getAttributeId();
 
$lastnameAttributeId = Mage::getModel('eav/entity_attribute')->loadByCode('customer', 'lastname')->getAttributeId();

然后,在联接查询中使用这些属性ID。

注意:您的收集表应包含名为“ customer_id”的列/字段。如果客户ID字段的名称不同,则需要使用main_table的字段名称更新以下代码“ main_table.customer_id”。

$collection->getSelect()
            ->join(array('ce1' => 'customer_entity_varchar'), 'ce1.entity_id=main_table.customer_id', array('firstname' => 'value'))
            ->where('ce1.attribute_id='.$firstnameAttributeId) 
            ->join(array('ce2' => 'customer_entity_varchar'), 'ce2.entity_id=main_table.customer_id', array('lastname' => 'value'))
            ->where('ce2.attribute_id='.$lastnameAttributeId) 
            ->columns(new Zend_Db_Expr("CONCAT(`ce1`.`value`, ' ',`ce2`.`value`) AS fullname"))
        ; 

Magento 2:加入收藏以获得客户全名

我在Magento 2中尝试了上面的联接查询代码,但是没有用。

这是Magento 2的工作代码:

我想您已经从包含的任何表中获得了一个集合customer_id

$collection = $this->yourModel->getCollection();

现在,对集合的联接查询:

$collection->getSelect()
            ->join(array('ce1' => 'customer_entity'), 'ce1.entity_id=main_table.customer_id', array('firstname' => 'firstname', 'lastname' => 'lastname'))
            ->columns(new \Zend_Db_Expr("CONCAT(`ce1`.`firstname`, ' ',`ce1`.`lastname`) AS fullname"))
            ;

希望这可以帮助。谢谢。

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论