skip to Main Content

I am using Woocmmerce REST API to make our Vue dashboard. So as a feature, I am trying to list today orders and this month orders.
I knew that WooCommerce has date filters like after and before.

But actually I need to set the date as a default(which means current date), so my client can view today orders at the end of the day(daily).
WooCommerce usual timestamp is like this 2020-09-17T10:56:17. I could not match this one with the current day and time from 00:00:00 to 23:59:59, so I can retrieve today orders.

My code goes like this…
I have just tried to make the time and date modification like below, I knew its a silly one as I am a fresher in Vue.

methods: {
    // Extract refresh logic into a method
    refreshData () {
        const today = new Date();
        const date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
        const setStartTime = "00" + ":" + "00" + ":" + "00";
        const setCloseTime = "23" + ":" + "59" + ":" + "59";
     
        // Set start time
        const startCombineDateAndTime = date + 'T' + setStartTime; 
        //this.currenttime = dateTime;
        console.log("start" + startCombineDateAndTime);
     
        // Set close time
        const closeCombineDateAndTime = date + 'T' + setCloseTime; 
        //this.currenttime = dateTime;
        console.log("close" + closeCombineDateAndTime);

        axios.get('https://testing.com/wp-json/wc/v3/orders?after=startCombineDateAndTime&before=closeCombineDateAndTime&per_page=40&consumer_key=ck_123&consumer_secret=cs_456')
...

2

Answers


  1. Chosen as BEST ANSWER

    Solved it through fixing the axios.get url and convertingJS timestamp to ISO8601 compliant date. If anyone needs...

    var today= new Date();
        today.setHours(0, 0, 0, 0);
        var isoTimeType = today.toISOString();
       
        axios.get('https:/testing.de/wp-json/wc/v3/orders?per_page=40&consumer_key=ck_123&consumer_secret=cs_456', {
            params: {
                after: isoTimeType 
            }
        })
    

  2. Looks like you are not applying your timestamps correctly in your axios call. You can use JS’ template string syntax to expand the values in order to call the correct URL:

    axios.get(`https://testing.com/wp-json/wc/v3/orders?after=${startCombineDateAndTime}&before=${closeCombineDateAndTime}&per_page=40&consumer_key=ck_123&consumer_secret=cs_456`)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search