class-api-for-cmbird.php
1 year ago
class-api-for-exact-webhooks.php
1 year ago
class-api-for-product-webhook.php
1 year ago
class-api-for-shipping-status.php
1 year ago
class-api-for-woo-order.php
1 year ago
class-api-for-zoho-inventory.php
1 year ago
class-commercebird-list-items-api-controller.php
1 year ago
class-commercebird-media-api-controller.php
1 year ago
class-commercebird-metadata-controller.php
1 year ago
index.php
1 year ago
trait-api-permission.php
1 year ago
class-api-for-cmbird.php
57 lines
| 1 | <?php |
| 2 | |
| 3 | namespace CommerceBird\API; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | use WP_REST_Response; |
| 10 | use WP_REST_Server; |
| 11 | use WP_REST_Controller; |
| 12 | |
| 13 | class CMBird_APIs extends WP_REST_Controller { |
| 14 | |
| 15 | protected $prefix = 'wc/v3'; |
| 16 | protected $rest_base = 'cmbird'; |
| 17 | |
| 18 | public function __construct() { |
| 19 | register_rest_route( |
| 20 | $this->prefix, |
| 21 | '/' . $this->rest_base . '/products-skus/', |
| 22 | array( |
| 23 | 'methods' => WP_REST_Server::READABLE, |
| 24 | 'callback' => array( $this, 'get_all_product_with_skus' ), |
| 25 | 'permission_callback' => array( $this, 'permission_check' ), |
| 26 | ) |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Check if a given request has access to get items. |
| 32 | */ |
| 33 | public function permission_check() { |
| 34 | return current_user_can( 'manage_woocommerce' ); |
| 35 | } |
| 36 | |
| 37 | public function get_all_product_with_skus(): WP_REST_RESPONSE { |
| 38 | $rest_response = new WP_REST_Response(); |
| 39 | $rest_response->set_data( $this->empty_response ); |
| 40 | $rest_response->set_status( 200 ); |
| 41 | |
| 42 | global $wpdb; |
| 43 | |
| 44 | // Query to fetch product IDs and SKUs directly from the database |
| 45 | $results = $wpdb->get_results( " |
| 46 | SELECT p.ID as product_id, pm.meta_value as sku, p.post_type as product_type |
| 47 | FROM {$wpdb->posts} p |
| 48 | LEFT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = '_sku' |
| 49 | WHERE p.post_type IN ('product', 'product_variation') AND p.post_status = 'publish' |
| 50 | " ); |
| 51 | |
| 52 | $rest_response->set_data( $results ); |
| 53 | |
| 54 | return rest_ensure_response( $rest_response ); |
| 55 | } |
| 56 | } |
| 57 |