简体   繁体   English

如何在Magento的管理端通过客户ID /电子邮件获取购物车商品?

[英]How to get cart items by customer id/email at the admin side in Magento?

I need to display a grid in the admin side with the list of cart products which is not yet ordered for all the customers. 我需要在管理端显示一个网格,其中没有为所有客户订购的购物车产品列表。 ie I want to display quote items of customer. 即我想显示客户的报价项目。 I tried below code, but the results returns something not related to right. 我尝试了下面的代码,但结果返回的内容与权利无关。

$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.

Try this code Below code. 尝试以下代码。 Use getItems() instead of getItemsCollection() function. 使用getItems()而不是getItemsCollection()函数。 and also check Mage_Sales_Model_Quote_Item is extended or not. 并检查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 />";

            }
        }
    }

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM