2015-05-09 28 views
0

我需要在wordpress woocommerce中使用curl下訂單。我已經嘗試過,但它不工作。我的錯誤使用curl的Woocommerce地方訂單?

{"result":"failure","messages":" \n\t\t\t 
    We were unable to process your order, please try again.<\/li>\n\t<\/ul>\n","refresh":"true","reload":"false"} 

我的代碼如下

$datas = "billing_country=IN&billing_first_name=renuka&billing_last_name=fg&billing_company=fsdfgdf&billing_address_1 
    =dsfdsfsd+fdfsdf&billing_address_2=fsdfsdfsdf&billing_city=madurai&billing_state=BR&billing_postcode 
    =6334535&billing_email=renuka%40osiztechnologies.com&billing_phone=2343546&shipping_country=IN&shipping_first_name 
    =renuka&shipping_last_name=fg&shipping_company=fsdfgdf&shipping_address_1=dsfdsfsd+fdfsdf&shipping_address_2 
    =fsdfsdfsdf&shipping_city=madurai&shipping_state=BR&shipping_postcode=6334535&order_comments=&shipping_method 
    %5B0%5D=free_shipping&payment_method=braintree&braintree-card-expiry-month=10&braintree-card-expiry-year 
    =2032&_wpnonce=a5dbf257ca&_wp_http_referer=%2Fprojects%2Ftutor%2Fwp-admin%2Fadmin-ajax.php"; 

    $ch = curl_init('http://localhost/project/wp-admin/admin-ajax.php?action=woocommerce_checkout'); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $datas); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    $result = curl_exec($ch); 

回答

1

給看看創建的WooCommerce REST API version 2訂單。

要使WooCommerce REST API正常工作,必須正確啓用Permalinks,並且必須爲generate an API key提供管理員用戶的寫入權限。

使用他們的例子,我已經在下面列出了,你應該能夠使用curl創建一個訂單。請注意,這依賴於HTTPS。如果您使用HTTP,那麼您需要使用OAuth1。請參閱他們的OAuth1說明here

我強烈建議您使用Gerhard Potgieter的WooCommerce REST API client,但您必須稍微修改才能使用v2而不是v1。而且您必須修改它才能創建訂單而不是更新。

curl -X POST https://example.com/wc-api/v2/orders \ 
    -u consumer_key:consumer_secret \ 
    -H "Content-Type: application/json" \ 
    -d '{ 
    "order": { 
    "payment_details": { 
     "method_id": "bacs", 
     "method_title": "Direct Bank Transfer", 
     "paid": true 
    }, 
    "billing_address": { 
     "first_name": "John", 
     "last_name": "Doe", 
     "address_1": "969 Market", 
     "address_2": "", 
     "city": "San Francisco", 
     "state": "CA", 
     "postcode": "94103", 
     "country": "US", 
     "email": "[email protected]", 
     "phone": "(555) 555-5555" 
    }, 
    "shipping_address": { 
     "first_name": "John", 
     "last_name": "Doe", 
     "address_1": "969 Market", 
     "address_2": "", 
     "city": "San Francisco", 
     "state": "CA", 
     "postcode": "94103", 
     "country": "US" 
    }, 
    "customer_id": 2, 
    "line_items": [ 
     { 
     "product_id": 546, 
     "quantity": 2 
     }, 
     { 
     "product_id": 613, 
     "quantity": 1, 
     "variations": { 
      "pa_color": "Black" 
     } 
     } 
    ], 
    "shipping_lines": [ 
     { 
     "method_id": "flat_rate", 
     "method_title": "Flat Rate", 
     "total": 10 
     } 
    ] 
    } 
}'