• Resolved regivanx

    (@regivanx)


    Hello!

    I wish create an woocommerce order in command line.

    I tried :

    wp wc shop_order create --user=my-login --customer_id=2 --line_items=[{"product_id":986,"quantity":1}]

    This create an order with the good customer but without the product and without send an email.

    What is the correct syntax?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Mirko P.

    (@rainfallnixfig)

    Hi @regivanx,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal (https://developer.woocommerce.com/) for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group (https://www.facebook.com/groups/advanced.woocommerce/) or the #developers channel of the WooCommerce Community Slack (https://woocommerce.com/community-slack/). We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers!

    Thread Starter regivanx

    (@regivanx)

    Hi! I subscribed to both groups.

    Some documentation to be deepened:

    Plugin Support Dani F. a11n

    (@danielinhou)

    Hi @regivanx,

    Thanks for getting back with that update. Have you managed to achieve what you were trying to do?

    I am not an expert on this subject but I was curious about this and I just noticed that the examples on this page use a format for the arrays that is slightly different to the one you are using:

    `$ wp wc customer create –email=’[email protected]’ –user=1 –billing='{“first_name”:”Justin”,”last_name”:”S”,”company”:”Automattic”}’ –password=’he llo’

    I am not sure it will work but maybe you want to give it a try. I’m leaving the thread open for a bit to see if anyone is able to chime in to help you out.

    Cheers!

    Thread Starter regivanx

    (@regivanx)

    Thank you for the documentation!

    The correct syntax is in the FAQ of your doc:

    Some ‘lists’ are objects, for example, if you want to set categories for a product the REST API expects an array of objects: https://woocommerce.github.io/woocommerce-rest-api-docs/#product-properties

    So to set this you would use JSON like this:

    wp wc product create --name='Product Name' --categories='[ { "id" : 21 } ]' --user=admin

    I do some tests before mark the thread “resolved”.

    • This reply was modified 3 years, 6 months ago by regivanx.
    Thread Starter regivanx

    (@regivanx)

    Some exemples of command lines to create, view, update and delete an order:

    wp wc shop_order create --user=my-login --customer_id=2 --line_items='[{"product_id":1201}]'
    Success: Created shop_order 6428.
    wp wc shop_order get 6428 --user=my-login
    [The table of the order…]
    wp wc shop_order get 6428 --user=my-login --field=line_items | sed '{ s/{/{\n\t/g ; s/}/\n}/g ; s/\(,"\)/,\n\t"/g }'
    [Only the "line_items" field, formated with sed. See the "id" field, not the "product_id" field. Suppose it is: 376.]
    wp wc shop_order update 6428 --user=my-login --customer_id=2 --line_items='[{"id":376,"quantity":2},{"product_id":1202}]'
    [update the quantity of the 376 item and add a new product 1202. You can see the result with:]
    wp wc shop_order get 6428 --user=my-login --field=line_items | sed '{ s/{/{\n\t/g ; s/}/\n}/g ; s/\(,"\)/,\n\t"/g }'
    wp wc shop_order delete 6428 --user=my-login
    [move the order to trash]
    wp help wc shop_order
    [the help]

    NOTA: these commands only manage the orders, but don’t send email. Probably I need a new command to send the purchase order to the customer. If you know the solution, please post it, if not, I will probably open a new topic on this.

    In any case, the main subject of this thread is resolved, I mark it.

    • This reply was modified 3 years, 6 months ago by regivanx.
    Thread Starter regivanx

    (@regivanx)

    ALTERNATIVE SOLUTION

    You can manage orders by accessing the WordPress REST API via curl and https (among others).

    Follow the instructions to create your REST API keys

    Save your keys in a secure space.

    NOTE: if you use your keys in your shell, they will be saved in your shell history.

    Follow the instructions to manage your orders.

    You have examples on the right.

    A very simple example:

    curl -X POST https://exemple.com/wp-json/wc/v3/orders \
    	     -u ck_baeg3ohr…:cs_outh0Oos… \
    	     -H "Content-Type: application/json" \
    	     -d '{
    		     "customer_id": 2,
    		     "line_items": [
    		     {
    			     "product_id": 1202,
    			     "quantity": 1
    		     }
    		     ]
    	     }'

    You can save your command lines in shell scripts to reuse them later.

    However, I still cannot send an order confirmation email to the customer at the moment. The documentation seems silent on this.

    Plugin Support Dani F. a11n

    (@danielinhou)

    Hey @regivanx ????

    Thanks for posting back all your findings.

    Yes, this kind of syntax is what I was referring to:

    categories='[ { "id" : 21 } ]' --user=admin

    However, I still cannot send an order confirmation email to the customer at the moment. The documentation seems silent on this.

    Maybe you can ask about this on the Facebook and Slack channels that my colleague suggested earlier.

    Cheers!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to create an order in command line (wp-cli) ?’ is closed to new replies.