Magento通过php获取功能

时间:2012-04-18 08:47:02

标签: php magento

我试图通过PHP从Magento获取一些数据,它可以工作,但我需要添加多个集合,现在我被卡住了。我只是个初学者所以请原谅我: - )

我使用下面的代码来获取客户详细信息,这样可行。 现在我需要添加客户/地址以获取地址详细信息以填充带有邮政编码的最后一列,任何人都知道如何做到这一点?

<?php
function getcustomers() {
    /* Magento's Mage.php path
     * Mage Enabler users may skip these lines
     */
    require_once ("app/Mage.php");
    umask(0);
    Mage::app("nl");
    /* Magento's Mage.php path */

    /* Get customer model, run a query */
  $collection = Mage::getModel('customer/customer')
    //$collection = Mage::getModel('customer/address')
                  ->getCollection()
                  ->addAttributeToSelect('*');

    $result = array();
    foreach ($collection as $customer) {
        $result[] = $customer->toArray();
    }

    return $result;
}
?>
<html>
<head>
<title>Customers</title>
<style>
table {
    border-collapse: collapse;
}
td {
    padding: 5px;
    border: 1px solid #000000;
}
</style>
</head>
<body>
<table>
<tr>
    <td>ID</td>
    <td>Lastname</td>
    <td>Firstname</td>
    <td>Email</td>
    <td>Is Active?</td>
    <td>Date Created</td>
    <td>Date Updated</td>
    <td>Website ID</td>
    <td>Store ID</td>
    <td>Zip Code</td>
</tr>
<?php
$result = getcustomers();
if(count($result) > 0){
    foreach($result as $key => $value){
        echo "<tr>";
            echo "<td>".$value['entity_id']."</td>";
            echo "<td>".$value['lastname']."</td>";
            echo "<td>".$value['firstname']."</td>";
            echo "<td>".$value['email']."</td>";
            echo "<td>";
            echo $value['is_active'] == 1 ? "Yes" : "No";
            echo "</td>";
            echo "<td>".$value['created_at']."</td>";
            echo "<td>".$value['updated_at']."</td>";
            echo "<td>".$value['website_id']."</td>";
            echo "<td>".$value['store_id']."</td>";
            echo "<td>".$value['zipcode']."</td>";
        echo "</tr>";
    }
}else{
    echo "<tr><td colspan=\"7\">No records found</td></tr>";
}
?>
</table>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

只想说我正在学习Magento,所以我的答案不会完全奏效!我希望它能帮助你朝着正确的方向前进。

只需注意,您只需使用$customer->getData()即可返回数组。

然后您可以使用$customer->getId()来获取ID。然后您可以将其传递到地址模型

foreach($collection as $customer){
  // You have an instance of the Customer already, so we can just use a magic get method
  $cid = $customer->getId();

  // Let's load this customers address, using a chain. Load the model (instantiate the class), then call load with the customer id
  // You might want to check the alias on 'customer' to ensure it has address. You can find this in /app/code/core/Mage/Customer/etc/config.xml ln.251
  $address = Mage::getModel('customer/address')->load($cid);
  // Maybe we should look in here just in case - for debugging
  var_dump($address); // or echo get_class($address);
  // Here I would try one of the magic setter methods, which map to set<Thing> so you can play with this to see if it'll work
  $address->setZipCode('12345');
  // Then we should be able to save it, I think, this bit I'm not sure on.
  $address->save();
}

正如我所说,这就是我在上周从Magento U视频中学到的,希望它有效!