Guest User

API Request

a guest
Dec 22nd, 2024
1,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.48 KB | Software | 0 0
  1. document.addEventListener('DOMContentLoaded', function() {
  2.     /*
  3.         When the checkout button is clicked:
  4.             1. Collect info from all products in the wishlist.
  5.                 1.1. data-product-id
  6.                 1.2. name
  7.                 1.3. regular price (sale price if the product is on sale)
  8.                 1.4. quantity
  9.                 1.5. thumbnail
  10.             2. Turn the collection into an object.
  11.             3. Convert the object into JSON.
  12.             4. Add JSON object as products to the cart.
  13.     */
  14.     // add-to-cart button
  15.     const chekcout = document.querySelector('.wishlist-footer #wishlist-checkout');
  16.    
  17.     //Listings
  18.     const w_item_ids = document.querySelectorAll(".wishlist-item");
  19.     const w_item_images = document.querySelectorAll(".wishlist-item-image");
  20.     const w_item_names = document.querySelectorAll(".wishlist-item-link .wishlist-item-title");
  21.     const w_item_prices = document.querySelectorAll(".wishlist-item-price-container #wishlist-item-price");
  22.     const w_item_qties = document.querySelectorAll(".wishlist-item-price-container #wishlist-item-quantity");
  23.    
  24.     if (chekcout) {
  25.         chekcout.addEventListener('click', function() {
  26.    
  27.             // ***Get product information***
  28.             for (let i = 0; i < w_item_ids.length; i++) {
  29.                 const w_item_ID = w_item_ids[i].getAttribute("data-product-id");
  30.                 const w_item_name = w_item_names[i].textContent.trim();
  31.                 const w_item_price = w_item_prices[i].textContent.trim();
  32.                 const w_item_qty = w_item_qties[i].textContent.trim();
  33.  
  34.                 const wishlist_product = {
  35.                     _id: w_item_ID,
  36.                     _name: w_item_name,
  37.                     _price: w_item_price,
  38.                     _quantity: w_item_qty
  39.                 }
  40.                
  41.                 let listing =
  42.                     {
  43.                         "path": "/wc/store/v1/cart/add-item",
  44.                         "method": "POST",
  45.                         "cache": "no-store",
  46.                         "body": {
  47.                             "id": wishlist_product._id,
  48.                             "name": wishlist_product._name,
  49.                             "price": wishlist_product._price,
  50.                             "quantity": wishlist_product._quantity
  51.                         },
  52.                         "headers": {
  53.                             "credentials": "same-origin",
  54.                             "Nonce": `${ajaxInfo.security.security_code}`
  55.                         }
  56.                     };
  57.                    
  58.                     const listings =
  59.                         {
  60.                             "requests": []
  61.                         };
  62.                        
  63.                     listings["requests"] = listing;
  64.                    
  65.                     const data = JSON.stringify(listings);
  66.  
  67.                     // ***Send the data to the server using Fetch API***
  68.            
  69.                     // Request Headers
  70.                     const infoHeaders = new Headers();
  71.                         infoHeaders.append("Content-Type", "application/json");
  72.                         infoHeaders.append("X-WP-Nonce", ajaxInfo.security.security_code);
  73.            
  74.                     // Request Body
  75.                     const infoRequest = new Request(
  76.                     ajaxInfo.root_url + "/wp-json/wc/store/batch/v1" , {
  77.                         method: "POST",
  78.                         headers: infoHeaders,
  79.                         credentials: "same-origin",
  80.                         body: JSON.stringify({
  81.                             action: ajaxInfo.action_url,
  82.                             listing: listings,
  83.                             security: ajaxInfo.security.security_code
  84.                         })
  85.                     });
  86.  
  87.                     // Send Request
  88.                     fetch(infoRequest)
  89.                     .then(response => {
  90.                         if (response.ok) {
  91.                             return response.json();
  92.                         } else {
  93.                             throw new Error('Network response was not ok');
  94.                         }
  95.                     })
  96.                     .then(data => {
  97.                         console.log("Raw response text:", decodeURIComponent(data));
  98.                         alert("Data saved and sent successfully:", data);
  99.                     })
  100.                     .catch(error => {
  101.                         alert("Failed to send data: " + error.message);
  102.                     });
  103.             }
  104.         });
  105.     }
  106. });
  107.  
Advertisement
Add Comment
Please, Sign In to add comment