Skip to content

Instantly share code, notes, and snippets.

@mahdiyazdani
Last active March 16, 2024 18:49
Show Gist options
  • Save mahdiyazdani/f351c5c65cbf27b2172478f158dcc9de to your computer and use it in GitHub Desktop.
Save mahdiyazdani/f351c5c65cbf27b2172478f158dcc9de to your computer and use it in GitHub Desktop.
Get WooCommerce vacation schedules via REST-API
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();
<?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);
}
} );
@mahdiyazdani
Copy link
Author

mahdiyazdani commented Jun 13, 2023

Get Store Vacation plugin here.

@mahdiyazdani
Copy link
Author

Learn more here on how to generate WooCommerce API Keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment