Woocommerce API Payloads

Discuss your code and ask for help here.
Post Reply
wooking
Posts: 1
Joined: Thu Feb 13, 2025 8:29 pm

Woocommerce API Payloads

Post by wooking »

1.
Create your key.

2.
Save the permlink structure to make sure curl fetches properly.

Fetch Orders with status pending
Curl:

Code: Select all

curl https://example.com/wp-json/wc/v3/orders?status=pending \
-u consumer_key:consumer_secret \
--insecure
(force valid ssl by removing --insecure)

Mark an order as paid
Curl
In this one, order id 123 is being marked as paid (processing).

Code: Select all

curl -X PUT https://example.com/wp-json/wc/v3/orders/123 \
    -u consumer_key:consumer_secret \
--insecure \
    -H "Content-Type: application/json" \
    -d '{
  "status": "processing"
}'
Fetch order id based on order_key and update order to processing based on id

Code: Select all

order_key="wc_order_IG6ssj7QYmAUV"
api_url="https://example.com/wp-json/wc/v3/orders"
consumer_key="ck_"
consumer_secret="cs_"


# Fetch orders (use pagination if needed)
order_info=$(curl -s -u "$consumer_key:$consumer_secret" --insecure -X GET "$api_url?status=pending")

# Check if API response is empty
if [[ -z "$order_info" || "$order_info" == "[]" ]]; then
    echo "No orders found. API response is empty."
    exit 1
fi

# Extract Order ID and Total Price for the given order_key
order_id=$(echo "$order_info" | jq -r --arg key "$order_key" '.[] | select(.order_key==$key) | .id')
total_price=$(echo "$order_info" | jq -r --arg key "$order_key" '.[] | select(.order_key==$key) | .total')

# Validate extracted data
if [[ -z "$order_id" || "$order_id" == "null" ]]; then
    echo "Order with key $order_key not found."
    exit 1
fi
echo "Order ID: $order_id"
echo "Total Price: $total_price"
# Update Order Status to "Processing"
update_payload=$(jq -n --arg status "processing" '{status: $status}')
update_response=$(curl -s -u "$consumer_key:$consumer_secret" --insecure -X PUT \
    "$api_url/$order_id" -H "Content-Type: application/json" -d "$update_payload")

# Verify if the status update was successful
updated_status=$(echo "$update_response" | jq -r '.status')

if [[ "$updated_status" == "processing" ]]; then
echo " Order status updated successfully to Processing."
else
    echo " Failed to update order status. Response: $update_response"
fi
Post Reply