Last active
March 16, 2024 18:49
-
-
Save mahdiyazdani/f351c5c65cbf27b2172478f158dcc9de to your computer and use it in GitHub Desktop.
Get WooCommerce vacation schedules via REST-API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import apiFetch from '@wordpress/api-fetch'; | |
// Define your WooCommerce consumer key and secret | |
const consumerKey = 'your_consumer_key'; | |
const consumerSecret = 'your_consumer_secret'; | |
const storeUrl = 'https://your-domain.com/wp-json/store-vacation/v1'; | |
// Function to fetch WooCommerce vacation schedules. | |
const fetchVacationSchedules = async () => { | |
try { | |
const schedules = await apiFetch({ | |
path: `${storeUrl}/schedule`, | |
method: 'GET', | |
headers: { | |
Authorization: `Basic ${btoa(`${consumerKey}:${consumerSecret}`)}`, | |
}, | |
}); | |
console.log(schedules); | |
} catch (error) { | |
console.error(error); | |
} | |
}; | |
// Call the function to fetch vacation schedules | |
fetchVacationSchedules(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'init', function() { | |
$consumer_key = 'your_consumer_key'; | |
$consumer_secret = 'your_consumer_secret'; | |
$store_url = 'https://your-domain.com/wp-json/store-vacation/v1'; | |
// Set up the request headers | |
$headers = array( | |
'Authorization' => 'Basic ' . base64_encode( $consumer_key . ':' . $consumer_secret ), | |
'Content-Type' => 'application/json', | |
); | |
// Send the GET request | |
$response = wp_remote_get( $store_url . '/schedule', array( 'headers' => $headers ) ); | |
// Check if the request was successful | |
if ( is_wp_error( $response ) ) { | |
$error_message = $response->get_error_message(); | |
echo 'Error: ' . $error_message; | |
} else { | |
// Get the response body | |
$response_body = wp_remote_retrieve_body( $response ); | |
// Convert the response to an array of schedules | |
$schedules = json_decode( $response_body, true ); | |
// Process the schedules as needed | |
// ... | |
// var_dump($schedules); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Get Store Vacation plugin here.