簡體   English   中英

Magento按名字搜索客戶

[英]Magento search customer by first name

我正在創建一個magento擴展名,我需要根據他們的名字搜索客戶。 我知道我可以在sql的幫助下做到這一點。 但是我想使用magento的客戶模型來完成這項工作。

我使用了它,它返回所有用戶的數組,但我希望所有符合條件的特定用戶而不是所有用戶。

$collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');

您必須使用addAttributeToFilter方法,該方法的用法類似於簡單的WHERE子句,您也可以在其中使用通配符。

<?php
include 'app/Mage.php';
Mage::app();

$query = "John";

$model = Mage::getSingleton('customer/customer');

$result = $model->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('firstname', array('like' => "%$query%"));

foreach($result as $r) 
{       
        $customer = $model->load($r->getId());
        echo '<b>'.$customer->getFirstname().' '.$customer->getLastname().'</b><br/>';
}

$c = Mage::getModel('customer/customer')->getCollection()->addAttributeToFilter('first_name', array('like' => $the_name_you_want_to_find))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM