wp plugin
This commit is contained in:
226
igny8-wp-plugin/data/woocommerce.php
Normal file
226
igny8-wp-plugin/data/woocommerce.php
Normal file
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
/**
|
||||
* WooCommerce Integration
|
||||
*
|
||||
* Fetches WooCommerce products, categories, and attributes
|
||||
* Follows WORDPRESS-PLUGIN-INTEGRATION.md guidelines
|
||||
*
|
||||
* @package Igny8Bridge
|
||||
*/
|
||||
|
||||
// Prevent direct access
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if WooCommerce is active
|
||||
*
|
||||
* @return bool True if WooCommerce is active
|
||||
*/
|
||||
function igny8_is_woocommerce_active() {
|
||||
return class_exists('WooCommerce');
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch all WooCommerce products
|
||||
*
|
||||
* @param int $per_page Products per page
|
||||
* @return array|false Formatted products array or false on failure
|
||||
*/
|
||||
function igny8_fetch_woocommerce_products($per_page = 100) {
|
||||
// Check if WooCommerce is active
|
||||
if (!igny8_is_woocommerce_active()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get WooCommerce API credentials
|
||||
$consumer_key = get_option('woocommerce_api_consumer_key', '');
|
||||
$consumer_secret = get_option('woocommerce_api_consumer_secret', '');
|
||||
|
||||
if (empty($consumer_key) || empty($consumer_secret)) {
|
||||
// Try to use basic auth if API keys not set
|
||||
$auth = '';
|
||||
} else {
|
||||
$auth = 'Basic ' . base64_encode($consumer_key . ':' . $consumer_secret);
|
||||
}
|
||||
|
||||
$headers = array();
|
||||
if ($auth) {
|
||||
$headers['Authorization'] = $auth;
|
||||
}
|
||||
|
||||
$wp_response = wp_remote_get(sprintf(
|
||||
'%s/wp-json/wc/v3/products?per_page=%d&status=publish',
|
||||
get_site_url(),
|
||||
$per_page
|
||||
), array(
|
||||
'headers' => $headers
|
||||
));
|
||||
|
||||
if (is_wp_error($wp_response)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$products = json_decode(wp_remote_retrieve_body($wp_response), true);
|
||||
|
||||
if (!is_array($products)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$formatted_products = array();
|
||||
foreach ($products as $product) {
|
||||
$formatted_products[] = array(
|
||||
'id' => $product['id'],
|
||||
'name' => $product['name'],
|
||||
'slug' => $product['slug'],
|
||||
'sku' => $product['sku'],
|
||||
'type' => $product['type'],
|
||||
'status' => $product['status'],
|
||||
'description' => $product['description'],
|
||||
'short_description' => $product['short_description'],
|
||||
'price' => $product['price'],
|
||||
'regular_price' => $product['regular_price'],
|
||||
'sale_price' => $product['sale_price'],
|
||||
'on_sale' => $product['on_sale'],
|
||||
'stock_status' => $product['stock_status'],
|
||||
'stock_quantity' => $product['stock_quantity'],
|
||||
'categories' => $product['categories'] ?? array(),
|
||||
'tags' => $product['tags'] ?? array(),
|
||||
'images' => $product['images'] ?? array(),
|
||||
'attributes' => $product['attributes'] ?? array(),
|
||||
'variations' => $product['variations'] ?? array(),
|
||||
'url' => $product['permalink']
|
||||
);
|
||||
}
|
||||
|
||||
return $formatted_products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch WooCommerce product categories
|
||||
*
|
||||
* @param int $per_page Categories per page
|
||||
* @return array|false Formatted categories array or false on failure
|
||||
*/
|
||||
function igny8_fetch_product_categories($per_page = 100) {
|
||||
if (!igny8_is_woocommerce_active()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$consumer_key = get_option('woocommerce_api_consumer_key', '');
|
||||
$consumer_secret = get_option('woocommerce_api_consumer_secret', '');
|
||||
|
||||
$headers = array();
|
||||
if ($consumer_key && $consumer_secret) {
|
||||
$headers['Authorization'] = 'Basic ' . base64_encode($consumer_key . ':' . $consumer_secret);
|
||||
}
|
||||
|
||||
$wp_response = wp_remote_get(sprintf(
|
||||
'%s/wp-json/wc/v3/products/categories?per_page=%d',
|
||||
get_site_url(),
|
||||
$per_page
|
||||
), array(
|
||||
'headers' => $headers
|
||||
));
|
||||
|
||||
if (is_wp_error($wp_response)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$categories = json_decode(wp_remote_retrieve_body($wp_response), true);
|
||||
|
||||
if (!is_array($categories)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$formatted_categories = array();
|
||||
foreach ($categories as $category) {
|
||||
$formatted_categories[] = array(
|
||||
'id' => $category['id'],
|
||||
'name' => $category['name'],
|
||||
'slug' => $category['slug'],
|
||||
'description' => $category['description'] ?? '',
|
||||
'count' => $category['count'],
|
||||
'parent' => $category['parent'] ?? 0,
|
||||
'image' => $category['image']['src'] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
return $formatted_categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch WooCommerce product attributes
|
||||
*
|
||||
* @return array|false Formatted attributes array or false on failure
|
||||
*/
|
||||
function igny8_fetch_product_attributes() {
|
||||
if (!igny8_is_woocommerce_active()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$consumer_key = get_option('woocommerce_api_consumer_key', '');
|
||||
$consumer_secret = get_option('woocommerce_api_consumer_secret', '');
|
||||
|
||||
$headers = array();
|
||||
if ($consumer_key && $consumer_secret) {
|
||||
$headers['Authorization'] = 'Basic ' . base64_encode($consumer_key . ':' . $consumer_secret);
|
||||
}
|
||||
|
||||
$wp_response = wp_remote_get(
|
||||
get_site_url() . '/wp-json/wc/v3/products/attributes',
|
||||
array(
|
||||
'headers' => $headers
|
||||
)
|
||||
);
|
||||
|
||||
if (is_wp_error($wp_response)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$attributes = json_decode(wp_remote_retrieve_body($wp_response), true);
|
||||
|
||||
if (!is_array($attributes)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$formatted_attributes = array();
|
||||
foreach ($attributes as $attribute) {
|
||||
// Get attribute terms
|
||||
$terms_response = wp_remote_get(sprintf(
|
||||
'%s/wp-json/wc/v3/products/attributes/%d/terms',
|
||||
get_site_url(),
|
||||
$attribute['id']
|
||||
), array(
|
||||
'headers' => $headers
|
||||
));
|
||||
|
||||
$terms = array();
|
||||
if (!is_wp_error($terms_response)) {
|
||||
$terms_data = json_decode(wp_remote_retrieve_body($terms_response), true);
|
||||
if (is_array($terms_data)) {
|
||||
foreach ($terms_data as $term) {
|
||||
$terms[] = array(
|
||||
'id' => $term['id'],
|
||||
'name' => $term['name'],
|
||||
'slug' => $term['slug']
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$formatted_attributes[] = array(
|
||||
'id' => $attribute['id'],
|
||||
'name' => $attribute['name'],
|
||||
'slug' => $attribute['slug'],
|
||||
'type' => $attribute['type'],
|
||||
'order_by' => $attribute['order_by'],
|
||||
'has_archives' => $attribute['has_archives'],
|
||||
'terms' => $terms
|
||||
);
|
||||
}
|
||||
|
||||
return $formatted_attributes;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user