Stores/Orders not working

My site offers online courses with Barista Basics and Hospitality 101 as sample courses. I have a collection called “Courses” that onNewOrder saves the buyer’s Id and queries the “Stores/Orders” collection for the line item name of the order. “baristaBasics” and “hospitality101” are boolean fields in the “Courses” collection that are true if the user has purchased that course. With the following code in the events.js file, it was working earlier today, however I must have changed something and now it will no longer save the boolean results in the “Courses” collection. Also when I made sample orders I would receive an email with every purchase, however now it will only randomly send me confirmation emails (maybe 1 in 10 orders will send an email), despite the order still being recorded in the “Stores/Orders” collection. Hopefully this makes sense, and any help would be really appreciated!

import wixData from 'wix-data';
import wixUsersBackend from 'wix-users-backend';
import wixStoresBackend from 'wix-stores-backend';

let userId = wixUsersBackend.currentUser.id;

export function wixStores_onNewOrder(event) {
 let buyerId = event.buyerInfo.id;
 let eventId = event.orderId;
 //let idSave = {
 //    "_id":      event.buyerInfo.id
 //}
 //wixData.save("Courses", idSave)
 //let courseName = event.lineItems.name;
   wixData.query("Stores/Orders")
   .eq("_id", eventId)
   .find()
   .then( (results) => {
   let lineItemName = results.items[0].lineItems[0].name
 
    wixData.query("Courses")
    .eq("_id",buyerId)
    .find()
    .then( (results2) => {
     let existingBaristaBasics = results2.items[0].baristaBasics;
     let existingHospitality = results2.items[0].hospitality101;

     if (lineItemName === "Barista Basics") {
       let toSave1 = {
       "_id":          buyerId,
       "baristaBasics":        true,
       "hospitality101":       existingHospitality
       }
       wixData.save("Courses", toSave1, optionsBaristaSave)
 
     } else if (lineItemName === "Hospitality 101") {
       let toSave2 = {
       "_id":          buyerId,
       "baristaBasics":        existingBaristaBasics,
       "hospitality101":        true
                     }
       wixData.save("Courses", toSave2,optionsHospoSave)
      }
     })
    }) 
   .catch( (err) => {
   let errorMsg = err;
   } );
  }

You wrote, " … it was working earlier today, however I must have changed something…"

I would suggest you go back to the previous working version. You can restore a previous version from the site history .