1

从基础开始,我想以编程方式导入客户。这是我的代码。

    <?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(); 

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

4

1 回答 1

1

您几乎完成了只需再次加载客户/地址模型并再次保存,但这次设置 $address->setIsDefaultBilling(false);$address->setIsDefaultShipping('1');This will Work。

在 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!");
于 2017-02-02T15:35:41.607 回答