如何在 Magento 中以编程方式添加个人送货地址?

How to add individual shipping address programmatically in Magento?

基于此,我想以编程方式导入客户。这是我的代码。

    <?php
    error_reporting(E_ALL | E_STRICT);
    ini_set('display_errors', 1);
    require 'app/Mage.php';
    require_once 'Zend/Crypt/Hmac.php';
    Mage::app('default');

    $websiteId = Mage::app()->getWebsite()->getId();
    $store = Mage::app()->getStore();

    $customer = Mage::getModel("customer/customer");
    $customer->setWebsiteId($websiteId);
    $customer->setStore($store);
    $customer->setFirstname('fst');
    $customer->setLastname('lst');
    $customer->setEmail('fst@ex.com');
    $customer->setPassword('somepassword');

    $customer->save();
    echo $customer->getId();

    $address = Mage::getModel("customer/address");
    $address->setCustomerId($customer->getId());
    $address->setFirstname($customer->getFirstname());
    $address->setLastname($customer->getLastname());
    $address->setCountryId('US');
    $address->setPostcode('31000');
    $address->setCity('Osijek');
    $address->setTelephone('0038511223344');
    $address->setFax('0038511223355');
    $address->setCompany('Inchoo');
    $address->setStreet('Kersov');
    $address->setIsDefaultBilling('1');
    $address->setIsDefaultShipping(false);
    $address->setSaveInAddressBook('1');

    $address->save(); 

但它只创建帐单地址。我想再添加一个地址并将其设置为送货。

你几乎完成了只需再次加载 customer/address 模型并再次保存,但这次设置 $address->setIsDefaultBilling(false);$address->setIsDefaultShipping('1'); 这将起作用。

在 magento 1.9 中测试

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

        $websiteId = Mage::app()->getWebsite()->getId();
        $store = Mage::app()->getStore();

        $customer = Mage::getModel("customer/customer");
        $customer->setWebsiteId($websiteId);
        $customer->setStore($store);
        $customer->setFirstname('fst');
        $customer->setLastname('lst');
        $customer->setEmail('fst@ex.com');
        $customer->setPassword('somepassword');

        $customer->save();
        echo $customer->getId();

        $address = Mage::getModel("customer/address");
        $address->setCustomerId($customer->getId());
        $address->setFirstname($customer->getFirstname());
        $address->setLastname($customer->getLastname());
        $address->setCountryId('US');
        $address->setPostcode('31000');
        $address->setCity('Osijek');
        $address->setTelephone('0038511223344');
        $address->setFax('0038511223355');
        $address->setCompany('Inchoo');
        $address->setStreet('Kersov');
        $address->setIsDefaultBilling('1');
        $address->setIsDefaultShipping(false);
        $address->setSaveInAddressBook('1');

        $address->save();

        $address = Mage::getModel("customer/address");
        $address->setCustomerId($customer->getId());
        $address->setFirstname($customer->getFirstname());
        $address->setLastname($customer->getLastname());
        $address->setCountryId('US');
        $address->setPostcode('31000');
        $address->setCity('Osijek');
        $address->setTelephone('0038511223344');
        $address->setFax('0038511223355');
        $address->setCompany('Inchoo');
        $address->setStreet('Kersov');
        $address->setIsDefaultBilling(false);
        $address->setIsDefaultShipping('1');
        $address->setSaveInAddressBook('1');

        $address->save(); 

        die("check it!");