Magento自定义选项:如何获取并保存到订单中?

时间:2014-03-22 02:06:52

标签: magento magento-1.7

我有一个与Magento中的自定义选项不兼容的第三方收银台。在购物车中,所有自定义选项均可用且可见。但是,当我使用收银员处理订单时,他们不会保存并在管理订单视图中可见(或根本不保存)。

但是,如果我使用标准收银台,则会保存自定义选项。

所以我的问题是如何在新收银员中实现对自定义选项的支持。

问题:如何检索自定义选项及其值(客户在添加产品时输入它们),然后将其保存到订单中(以便在管理订单视图中显示它们) )?

注意:我只使用两个文本字段(例如,客户在订购产品时可以写两条短信)。

以下是推送保存订单的代码:

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

    if($customer == 0){
        // set Customer data
        $order->setCustomer_email($shipping['email'])
        ->setCustomerFirstname($shipping['given_name'])
        ->setCustomerLastname($shipping['family_name'])
        //->setCustomerGroupId($customer->getGroupId())
        //->setCustomer_is_guest(0)
        ->setCustomer($customer);
    }else{
        // 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['given_name'])
    //->setMiddlename($billing->getMiddlename())
    ->setLastname($billing['family_name'])
    //->setSuffix($billing->getSuffix())
    //->setCompany($shipping->getCompanyName())
    ->setStreet($billing['street_address'])
    ->setCity($billing['city'])
    ->setCountry_id($billing['country'])
    //->setRegion($billing->getRegion())
    //->setRegion_id($billing->getRegionId())
    ->setPostcode($billing['postal_code'])
    ->setTelephone($billing['phone']);
    //->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->getDefaultBilling())
    //->setCustomer_address_id($shipping->getEntityId())
    //->setPrefix($shipping->getPrefix())
    ->setFirstname($shipping['given_name'])
    //->setMiddlename($shipping->getMiddlename())
    ->setLastname($shipping['family_name'])
    //->setSuffix($shipping->getSuffix())
    //->setCompany($shipping->getCompanyName())
    ->setStreet($shipping['street_address'])
    ->setCity($shipping['city'])
    ->setCountry_id($shipping['country'])
    //->setRegion($shipping->getRegion())
    //->setRegion_id($shipping->getRegionId())
    ->setPostcode($shipping['postal_code'])
    ->setTelephone($shipping['phone']);
    //->setFax($shipping->getFax());

    $itemcount = count($cart);
    $cartItem = $cart[$itemcount-1];
    if($cartItem['reference'] == 'SHIPPING'){
        if($cartItem['unit_price'] == 0){
            $order->setShippingAddress($shippingAddress)
            ->setShipping_method('freeshipping_freeshipping')
            ->setShippingAmount(0)
            ->setShippingDescription('Standardfrakt - Sverige');
        }else{
            $order->setShippingAddress($shippingAddress)
            ->setShipping_method('flatrate_flatrate')
            ->setShippingAmount($ship)
            ->setShippingDescription('Standardfrakt - Sverige');
        }
    }

    // $order->setShippingAddress($shippingAddress)
    // ->setShippingMethod('apport_2') // där X är id i databasen på fraktsättet
    // ->setShippingDescription("DB Schenker"); // Kan läggas in ett eget namn på fraktsättet om ni vill

    $orderPayment = Mage::getModel('sales/order_payment')
    ->setStoreId($storeId)
    ->setCustomerPaymentId(0)
    ->setMethod('purchaseorder')
    ->setPo_number($reservation);
    $order->setPayment($orderPayment);

    $allitems = array();
    $itemcount = count($cart);
    for($i = 0; $i < ($itemcount - 1); $i++){
        $cartItem = $cart[$i];
        if($cartItem['type'] == 'discount'){
            $discount = $cartItem['unit_price'] / 100;
            $discount = abs($discount);
        }else{
            $number += $cartItem['quantity'];
        }
    }

    $discount = $discount / $number;

    for($i = 0; $i < ($itemcount - 1); $i++){
        $cartItem = $cart[$i];
        if($cartItem['reference'] == 'PROMO'){
        }else{
            $product_name = $cartItem['reference'];
            $discounttotal = $discount * $cartItem['quantity'];
            //$discount = ($cartItem['unit_price'] / 100) - ($cartItem['total_price_including_tax'] / 100);
            $taxrate = ($cartItem['tax_rate'] / 100);
            $taxamount = ($cartItem['total_price_including_tax'] / 100) - ($cartItem['total_price_excluding_tax'] / 100);
            $price = ($cartItem['total_price_including_tax'] / 100);
            $model = Mage::getModel('catalog/product'); //getting product model
            $collection = Mage::getModel('catalog/product')->getCollection()->addStoreFilter(); //products collection
            $nr = (int)$cartItem['quantity'];
            $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$product_name);
        //     foreach ($collection as $product) {
        //         $model->load($product->getId());
        //         $pname = $model->getSku();
        //         if(strcmp($pname,$product_name)==0) {
                    $id = $product->getId();
                    $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);
                    $stock->setQty((int)$stock->getQty() - $nr);
                    $stock->save();
        //         }
        //     }
            $allitems[$id] = array('qty' => $nr,
                                    'discount' => $discounttotal,
                                    'taxrate' => $taxrate,
                                    'price' => $price,
                                    'taxamount' => $taxamount);
        }
    }
    $subTotal = 0;
    $exTotal = 0;
    $products = $allitems;

    // Mage::log(print_r($products, 1), null, 'mynewlog.log');


    foreach ($products as $productId=>$product) {
    $_product = Mage::getModel('catalog/product')->load($productId);
    $rawtotal = ($_product->getFinalPrice() - ($product['taxamount'] / $product['qty'])) * $product['qty'];
    $rowTotal = $product['price'];          


    $orderItem = Mage::getModel('sales/order_item')
    ->setStoreId($storeId)
    ->setQuoteItemId(0)
    ->setQuoteParentItemId(NULL)
    ->setProductId($productId)
    ->setProductType($_product->getTypeId())
    ->setQtyBackordered(NULL)
    ->setTotalQtyOrdered($product['qty'])
    ->setQtyOrdered($product['qty'])
    ->setName($_product->getName())
    ->setSku($_product->getSku())
    ->setWeight($_product->getWeight())
    ->setPrice($_product->getFinalPrice())
    ->setBasePrice($product['price'])
    ->setOriginalPrice($_product->getPrice())
    ->setTaxPercent($product['taxrate'])
    ->setTaxAmount($product['taxamount'])
    ->setDiscountAmount($product['discount'])
    ->setRowTotal($rawtotal)
    ->setBaseRowTotal($rowTotal);   


    $stotal = $product['price'] - $product['discount'];
    $etotal = $product['price'] - $product['taxamount'] - $product['discount'];
    $disco += $product['discount'];
    $taxam += $product['taxamount'];
    $subTotal += $stotal;
    $exTotal += $etotal;
    $order->addItem($orderItem);
    }

    $taxam += ($ship / 5);
    $order->setSubtotal($exTotal)
    ->setCoupon_code($couponcode)
    ->setDiscountDescription($couponcode)
    ->setStoreId($store)
    ->setDiscountAmount($disco)
    ->setTaxAmount($taxam)
    ->setSubtotal_incl_tax($subTotal)
    ->setBaseToGlobalRate(1)
    ->setBaseSubtotal($subTotal)
    ->setGrandTotal($subTotal + $ship)
    ->setBaseGrandTotal($subTotal + $ship);

    $transaction->addObject($order);
    $transaction->addCommitCallback(array($order, 'place'));
    $transaction->addCommitCallback(array($order, 'save'));
    $transaction->save();

        // At this point make sure the order is created in your system and send a
        // confirmation email to the customer
        $order->load(Mage::getSingleton('checkout/session')->getLastOrderId());
        $order->sendNewOrderEmail();
        $order->setEmailSent(true);
        $order->save();

1 个答案:

答案 0 :(得分:0)

Magento,请修改代码

->setGlobal_currency_code('SEK')
    ->setBase_currency_code('SEK')
    ->setStore_currency_code('SEK')
    ->setOrder_currency_code('SEK');

下面使用
 ->setGlobalCurrencyCode('SEK')
        ->setBaseCurrencyCode('SEK')
        ->setStoreCurrencyCode('SEK')

请点击链接http://inchoo.net/ecommerce/magento/programmatically-create-order-in-magento/