如何在Magento的管理端通过客户id/电子邮件获取购物车项目



我需要在管理端显示一个网格,其中包含尚未为所有客户订购的购物车产品列表。我想显示客户的报价项目。我尝试了下面的代码,但结果返回了一些和右边不相关的东西。

$customer_email = 'rasd@gmail.com'; 
    $customer_detail = Mage::getModel("customer/customer");
    $customer_detail->setWebsiteId(Mage::app()->getWebsite()->getId());
    $customer_detail->loadByEmail($customer_email);
    $storeIds = Mage::app()->getWebsite(Mage::app()->getWebsite()->getId())->getStoreIds(); 
    $quote = Mage::getModel('sales/quote')->setSharedStoreIds($storeIds)->loadByCustomer($customer_detail);
     if ($quote) { 
        $collection = $quote->getItemsCollection();
        if($customer_email == 'rasd@gmail.com') {   
        echo "<pre>";
        print_r($collection->getData());        
        }
        if ($collection->count() > 0) { echo $customer_email; die('here');
            foreach( $collection as $item ) {
                echo "<pre>";
                print_r($item->getData());
            }
        }
    }

$collection->getData() - it returns the result array but that one seems wrong.
$item->getData() - No result.

试试下面的代码。使用getItems()而不是getItemsCollection()函数。并检查Mage_Sales_Model_ Quote_Item是否扩展。

$customer_email = 'rasd@gmail.com';
$customer_detail = Mage::getModel("customer/customer");
$customer_detail->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer_detail->loadByEmail($customer_email);
$storeIds = Mage::app()->getWebsite(Mage::app()->getWebsite()->getId())->getStoreIds();
$quote = Mage::getModel('sales/quote')->setSharedStoreIds($storeIds)->loadByCustomer($customer_detail);
if ($quote) {
    $productsResult = array();
    foreach ($quote->getAllItems() as $item) { 
        $product = $item->getProduct(); 
        $productsResult[] = array(// Basic product data
            'product_id' => $product->getId(),
            'sku' => $product->getSku(),
            'name' => $product->getName(),
            'set' => $product->getAttributeSetId(),
            'type' => $product->getTypeId(),
            'category_ids' => $product->getCategoryIds(),
            'website_ids' => $product->getWebsiteIds()
        );
    }
    return $productsResult;
}
<?php
    require_once('app/Mage.php');
    umask(0);
    Mage::app();
    $customerId = 1; // your customer ID
    $customer = Mage::getModel('customer/customer')->load($customerId);
    $quote = Mage::getModel('sales/quote')
        ->setSharedStoreIds($storeIds)
        ->loadByCustomer($customer);
    if ($quote) {
        $collection = $quote->getItemsCollection();
        if ($collection->count() > 0) {
            foreach( $collection as $item ) {
                echo 'ID: '.$item->getProductId().'<br />';
                echo 'Name: '.$item->getName().'<br />';
                echo 'Sku: '.$item->getSku().'<br />';
                echo 'Quantity: '.$item->getQty().'<br />';
                echo 'Price: '.$item->getPrice().'<br />';
                echo "<br />";
            }
        }
    }
?>

最新更新