透過 SOAP API 建立訪客訂單

edited 十二月 2013 in Magento
因為 Magento 執行時消耗的資源比較多,回應速度也比較慢,在進行一些活動的時候會有困擾。雖然可以透過擴充機器設備來解決問題,但並不是每個客戶都有那麼多的預算,因此希望獨立製作出活動頁面,然後透過系統排程定時將訂單資料送進 Magento 中。因為這個活動預期購買者都是以訪客身份,所以沒有做系統登入的部份,也沒有依據使用者提供的資訊幫他建立客戶資料,純粹是一個訂購表單直接送入 Magento 資料庫。

在實做過程也遇到了客戶信箱無法正確儲存的問題,暫時透過這兒提到的方式修正:
http://www.magentocommerce.com/boards/v/viewthread/262680/

指定產品透過活動頁面購買會有特別的價格計算方式,所以在 Magento 的購物車價格規則中建立了對應的算法,所以 API 建立訂單過程需要將規則套用進去。

這個活動運費一律內含,都採用貨到付款的方式,付款以及運送方式需要確認 Magento 是否有開啟

下面就是實做出來的程式碼

跟其他類似 API 實做的感覺差異是, Magento SOAP API 做的真完整 XD
$proxy = new SoapClient('http://your_magento_url/index.php/api/v2_soap?wsdl=1');
        $sessionId = $proxy->login('id', 'password');
        foreach ($orders AS $order) {
            $cartId = $proxy->shoppingCartCreate($sessionId, 2); //2 = store_id

            $customer = array(
                'mode' => 'guest',
                'email' => $order['Order']['email'],
                'firstname' => $billingNames['firstname'],
                'lastname' => $billingNames['lastname'],
                'website_id' => 2,
                'store_id' => 2,
                'group_id' => 0,
            );
            $proxy->shoppingCartCustomerSet($sessionId, $cartId, $customer);

            $product = array(
                'qty' => intval($order['Order']['qty']),
                'product_id' => $order['Order']['product_id'],
            );
            $proxy->shoppingCartProductAdd($sessionId, $cartId, array($product));

            $address = array(
                array(
                    'mode' => 'shipping',
                    'firstname' => $deliveryNames['firstname'],
                    'lastname' => $deliveryNames['lastname'],
                    'street' => $order['Order']['delivery_address'],
                    'city' => $order['Order']['delivery_city'],
                    'region' => $order['Order']['delivery_region'],
                    'telephone' => $order['Order']['delivery_phone'],
                    'postcode' => $order['Order']['delivery_postcode'],
                    'country_id' => 'TW',
                    'is_default_shipping' => 0,
                    'is_default_billing' => 0
                ),
                array(
                    'mode' => 'billing',
                    'firstname' => $billingNames['firstname'],
                    'lastname' => $billingNames['lastname'],
                    'street' => $order['Order']['billing_address'],
                    'city' => $order['Order']['billing_city'],
                    'region' => $order['Order']['billing_region'],
                    'telephone' => $order['Order']['billing_phone'],
                    'postcode' => $order['Order']['billing_postcode'],
                    'country_id' => 'TW',
                    'is_default_shipping' => 0,
                    'is_default_billing' => 0
                ),
            );
            // add customer address
            $proxy->shoppingCartCustomerAddresses($sessionId, $cartId, $address);
            // add shipping method
            $proxy->shoppingCartShippingMethod($sessionId, $cartId, 'freeshipping_freeshipping');
            if ($order['Order']['product_id'] == 1) {
                $proxy->shoppingCartCouponAdd($sessionId, $cartId, 'coupon code 1');
            } else {
                $proxy->shoppingCartCouponAdd($sessionId, $cartId, 'coupon code 2');
            }


            $paymentMethod = array(
                'po_number' => null,
                'method' => 'cashondelivery',
            );

            // add payment method
            $proxy->shoppingCartPaymentMethod($sessionId, $cartId, $paymentMethod);
            // place the order
            $orderId = $proxy->shoppingCartOrder($sessionId, $cartId, 2, null);
            if (!empty($orderId)) {
                $newOrderComment = '';
                if(!empty($order['Order']['is_paypal'])) {
                    $newOrderComment .= '本訂單使用 Paypal 付款,請留意付款是否完成! / ';
                }
                $newOrderComment .= '希望出貨日期: ' . $order['Order']['delivery_date'];
                if(!empty($order['Order']['comment'])) {
                    $newOrderComment .= ' / ' . $order['Order']['comment'];
                }
                $proxy->salesOrderAddComment($sessionId, $orderId, 'pending', $newOrderComment);
            }
        }
標籤:
Sign In or Register to comment.