0

我尝试在代码中创建订单,但有时代码有效,但有时会出错,出现异常“请求的付款方式不可用”。(第一个浏览器请求没问题,但第二个出错了,等等)。

我的代码是:

if (Mage::getSingleton('customer/session')->isLoggedIn()) {
    $customer = Mage::getSingleton('customer/session')->getCustomer();
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
} else {
    $customer = Mage::getModel('customer/customer');
    $email = 'test@example.com';
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
    $customer->setStoreId(Mage::app()->getStore()->getId());
    $customer->loadByEmail($email);
    Mage::getSingleton('customer/session')->loginById($customer->getId());
}

$customAddress = Mage::getModel('customer/address')
        ->setCustomerId($customer->getId())
        ->getCustomer();

$customAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
if ($customAddressId) {
    $customAddress = Mage::getModel('customer/address')->load($customAddressId);
}

Mage::getSingleton('checkout/session')->getQuote()
        ->setBillingAddress(Mage::getSingleton('sales/quote_address')->importCustomerAddress($customAddress))
        ->setShippingAddress(Mage::getSingleton('sales/quote_address')->importCustomerAddress($customAddress));

$product = Mage::getModel('catalog/product')
        ->setStoreId(Mage::app()->getStore()->getId())
        ->load(2);

$cart = Mage::getSingleton('checkout/cart');
$cart->truncate();

try {
    $cart->addProduct($product, array('qty' => 2));
    $cart->save();

    $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());
    Mage::getSingleton('checkout/session')->addSuccess($message);
} catch (Exception $ex) {
    echo $ex->getMessage();
}

$storeId = Mage::app()->getStore()->getId();
$checkout = Mage::getSingleton('checkout/type_onepage');
$checkout->initCheckout();
$checkout->saveCheckoutMethod('register');
$checkout->saveShippingMethod('flatrate_flatrate');

$checkout->savePayment(array('method' => 'banktransfer'));

try {
    $checkout->saveOrder();
} catch (Exception $ex) {
    echo $ex->getMessage();
}

$cart->truncate();
$cart->save();
$cart->getItems()->clear()->save();
Mage::getSingleton('customer/session')->logout();
4

1 回答 1

0

我在本地服务器中检查了您的代码,第一次运行正常,但第二次显示错误,The requested Shipping Method is not available不像您上面提到的那样。但是在我启用该运输方式后它工作正常,因此请尝试启用您在管理员代码中使用的运输和付款方式。

于 2013-11-14T12:35:56.937 回答