以编程方式创建订单时无法设置装运方法

时间:2014-08-20 11:20:13

标签: php magento

在我的脚本中,我正在使用此代码

 $order = Mage::getModel('sales/order')
                        ->setIncrementId($reservedOrderId)
                        ->setStoreId($storeId)
                        ->setQuoteId(0)
                        ->setGlobal_currency_code('GBP')
                        ->setBase_currency_code('GBP')
                        ->setStore_currency_code('GBP')
                        ->setOrder_currency_code('GBP');

    $shippingAddress = Mage::getModel('sales/order_address')
                    ->setStoreId($storeId)
                    ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
                    ->setCustomerId($customer->getId())
                    ->setCustomerAddressId($customer->getDefaultShipping())
                    //->setCustomer_address_id($shipping->getEntityId())
                    ->setFirstname($shipping->getFirstname())
                    ->setLastname($shipping->getLastname())
                    ->setSuffix($shipping->getSuffix())
                    ->setCompany($shipping->getCompany())
                    ->setStreet($shipping->getStreet())
                    ->setCity($shipping->getCity())
                    ->setCountryId($shipping->getCountryId())
                    ->setRegion($shipping->getRegion())
                    ->setRegionId($shipping->getRegionId())
                    ->setPostcode($shipping->getPostcode())
                    ->setTelephone($shipping->getTelephone())
                    ->setFax($shipping->getFax());
$order->setShippingAddress($shippingAddress)->setShippingMethod('freeshipping_freeshipping');

但是当我在管理员中检查创建的订单时,它显示运送和处理中没有可用的送货信息。 从管理员启用freeshipping 请有人帮我设置送货方式

2 个答案:

答案 0 :(得分:0)

我正在使用类似的东西为订单设置货件:

            if($order->canShip())
            {
                $shipment = $order->prepareShipment();
                $shipment->setCreatedAt($dateCreatedAt->getTimeStamp());
                $shipment->setUpdatedAt($dateUpdatedAt->getTimeStamp());
                $shipment->setTotalWeight($order->getWeight());
                $shipment->setEmailSent(0);
                $shipment->setTotalQty($total_qty);
                $shipment->setShipmentStatus(0);
                $shipment->register();
                $shipment->save();
            } 

它与Magento 1.8合作,我不知道1.9那方面是否真的发生了变化。我想不会。

答案 1 :(得分:0)

请参阅以下代码段以创建订单

require_once 'app/Mage.php';
umask(0);
Mage::app('default');

$id=12; // get Customer Id
$customer = Mage::getModel('customer/customer')->load($id);
// print_r($customer->getData());exit;
$transaction = Mage::getModel('core/resource_transaction');
$storeId = $customer->getStoreId();
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId(1);//exit();
$order = Mage::getModel('sales/order')
->setIncrementId($reservedOrderId)
->setStoreId($storeId)
->setQuoteId(0)
->setGlobal_currency_code('USD')
->setBase_currency_code('USD')
->setStore_currency_code('USD')
->setOrder_currency_code('USD');
//Set your store currency USD or any other

// set Customer data
$order->setCustomer_email($customer->getEmail())
->setCustomerFirstname($customer->getFirstname())
->setCustomerLastname($customer->getLastname())
->setCustomerGroupId($customer->getGroupId())
->setCustomer_is_guest(0)
->setCustomer($customer);

// set Billing Address
$billing = $customer->getDefaultBillingAddress();
$billingAddress = Mage::getModel('sales/order_address')
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
->setCustomerId($customer->getId())
->setCustomerAddressId($customer->getDefaultBilling())
->setCustomer_address_id($billing->getEntityId())
->setPrefix($billing->getPrefix())
->setFirstname($billing->getFirstname())
->setMiddlename($billing->getMiddlename())
->setLastname($billing->getLastname())
->setSuffix($billing->getSuffix())
->setCompany($billing->getCompany())
->setStreet($billing->getStreet())
->setCity($billing->getCity())
->setCountry_id($billing->getCountryId())
->setRegion($billing->getRegion())
->setRegion_id($billing->getRegionId())
->setPostcode($billing->getPostcode())
->setTelephone($billing->getTelephone())
->setFax($billing->getFax());
$order->setBillingAddress($billingAddress);

$shipping = $customer->getDefaultShippingAddress();
$shippingAddress = Mage::getModel('sales/order_address')
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
->setCustomerId($customer->getId())
->setCustomerAddressId($customer->getDefaultShipping())
->setCustomer_address_id($shipping->getEntityId())
->setPrefix($shipping->getPrefix())
->setFirstname($shipping->getFirstname())
->setMiddlename($shipping->getMiddlename())
->setLastname($shipping->getLastname())
->setSuffix($shipping->getSuffix())
->setCompany($shipping->getCompany())
->setStreet($shipping->getStreet())
->setCity($shipping->getCity())
->setCountry_id($shipping->getCountryId())
->setRegion($shipping->getRegion())
->setRegion_id($shipping->getRegionId())
->setPostcode($shipping->getPostcode())
->setTelephone($shipping->getTelephone())
->setFax($shipping->getFax());

$order->setShippingAddress($shippingAddress)
->setShipping_method('flatrate_flatrate');
/*->setShippingDescription($this->getCarrierName('flatrate'));*/
/*some error i am getting here need to solve further*/

//you can set your payment method name here as per your need
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($storeId)
->setCustomerPaymentId(0)
->setMethod('purchaseorder')
->setPo_number(' – ');
$order->setPayment($orderPayment);

// let say, we have 2 products
//check that your products exists
//need to add code for configurable products if any
$subTotal = 0;
$products = array(
    '7088' => array(
    'qty' => 2
    ),
    '7089' => array(
    'qty' => 1
    )
);

foreach ($products as $productId=>$product) {
$_product = Mage::getModel('catalog/product')->load($productId);
$rowTotal = $_product->getPrice() * $product['qty'];
$orderItem = Mage::getModel('sales/order_item')
->setStoreId($storeId)
->setQuoteItemId(0)
->setQuoteParentItemId(NULL)
->setProductId($productId)
->setProductType($_product->getTypeId())
->setQtyBackordered(NULL)
->setTotalQtyOrdered($product['rqty'])
->setQtyOrdered($product['qty'])
->setName($_product->getName())
->setSku($_product->getSku())
->setPrice($_product->getPrice())
->setBasePrice($_product->getPrice())
->setOriginalPrice($_product->getPrice())
->setRowTotal($rowTotal)
->setBaseRowTotal($rowTotal);

$subTotal += $rowTotal;
$order->addItem($orderItem);
}

$order->setSubtotal($subTotal)
->setBaseSubtotal($subTotal)
->setGrandTotal($subTotal)
->setBaseGrandTotal($subTotal);

$transaction->addObject($order);
$transaction->addCommitCallback(array($order, 'place'));
$transaction->addCommitCallback(array($order, 'save'));
$transaction->save();
相关问题