使用固定的订单级折扣

时间:2016-02-04 12:37:50

标签: php magento magento-1.7 magento-1.9 magento-1.8

我正在研究如何以编程方式创建订单,我遇到了这篇很棒的文章:https://blog.amasty.com/creating-magento-order-programmatically/

创建订单的主要代码如下所示:

public function createOrder($products)
{
    if (!($this->_customer instanceof Mage_Customer_Model_Customer)){
        $this->setCustomer(self::CUSTOMER_RANDOM);
    }

    $transaction = Mage::getModel('core/resource_transaction');
    $this->_storeId = $this->_customer->getStoreId();
    $reservedOrderId = Mage::getSingleton('eav/config')
        ->getEntityType('order')
        ->fetchNewIncrementId($this->_storeId);

    $currencyCode  = Mage::app()->getBaseCurrencyCode();
    $this->_order = Mage::getModel('sales/order')
        ->setIncrementId($reservedOrderId)
        ->setStoreId($this->_storeId)
        ->setQuoteId(0)
        ->setGlobalCurrencyCode($currencyCode)
        ->setBaseCurrencyCode($currencyCode)
        ->setStoreCurrencyCode($currencyCode)
        ->setOrderCurrencyCode($currencyCode);


    $this->_order->setCustomerEmail($this->_customer->getEmail())
        ->setCustomerFirstname($this->_customer->getFirstname())
        ->setCustomerLastname($this->_customer->getLastname())
        ->setCustomerGroupId($this->_customer->getGroupId())
        ->setCustomerIsGuest(0)
        ->setCustomer($this->_customer);


    $billing = $this->_customer->getDefaultBillingAddress();
    $billingAddress = Mage::getModel('sales/order_address')
        ->setStoreId($this->_storeId)
        ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
        ->setCustomerId($this->_customer->getId())
        ->setCustomerAddressId($this->_customer->getDefaultBilling())
        ->setCustomerAddress_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())
        ->setVatId($billing->getVatId());
    $this->_order->setBillingAddress($billingAddress);

    $shipping = $this->_customer->getDefaultShippingAddress();
    $shippingAddress = Mage::getModel('sales/order_address')
        ->setStoreId($this->_storeId)
        ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
        ->setCustomerId($this->_customer->getId())
        ->setCustomerAddressId($this->_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())
        ->setVatId($billing->getVatId());

    $this->_order->setShippingAddress($shippingAddress)
        ->setShippingMethod($this->_shippingMethod);

    $orderPayment = Mage::getModel('sales/order_payment')
        ->setStoreId($this->_storeId)
        ->setCustomerPaymentId(0)
        ->setMethod($this->_paymentMethod)
        ->setPoNumber(' – ');

    $this->_order->setPayment($orderPayment);

    $this->_addProducts($products);

    $this->_order->setSubtotal($this->_subTotal)
        ->setBaseSubtotal($this->_subTotal)
        ->setGrandTotal($this->_subTotal)
        ->setBaseGrandTotal($this->_subTotal);

    $transaction->addObject($this->_order);
    $transaction->addCommitCallback(array($this->_order, 'place'));
    $transaction->addCommitCallback(array($this->_order, 'save'));
    $transaction->save();        
}

我这样用过:

$orderGenerator = new OrderGenerator();

$orderGenerator->setCustomer(Mage::getModel('customer/customer')->load(1234));

$orderGenerator->createOrder(array(
    array(
        'product' => 4174,
        'qty' => 2
    ),
    array(
        'product' => 631,
        'qty' => 7
    )
));

这样可以为指定的客户和产品创建订单。

在上面的示例中,订单总数为£25.00(例如)。

如果我想让订单的总体折扣为£5.00,我可以在用于设置值之前调整$this->_subTotal

但是,这只能部分起作用。它正确显示订单的折扣金额(£20.00)。但是当我去调用它时,它就是全额(£25.00)。

如何以编程方式创建具有固定订单级别折扣的订单?或者这不可能吗?我是否需要使用quote机制来解决这个问题?

0 个答案:

没有答案
相关问题