get( Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema::class ); } catch (\Throwable $e) { return; } $endpoints = array(); if (class_exists('Automattic\\WooCommerce\\StoreApi\\Schemas\\V1\\CartSchema')) { $endpoints[] = Automattic\WooCommerce\StoreApi\Schemas\V1\CartSchema::IDENTIFIER; } if (class_exists('Automattic\\WooCommerce\\StoreApi\\Schemas\\V1\\CheckoutSchema')) { $endpoints[] = Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema::IDENTIFIER; } foreach ($endpoints as $endpoint) { $extend->register_endpoint_data(array( 'endpoint' => $endpoint, 'namespace' => 'aieo_table_rate', 'data_callback' => array(__CLASS__, 'data_callback'), 'schema_callback' => array(__CLASS__, 'schema_callback'), )); } } public static function data_callback() { $payload = array( 'rates' => array(), 'free_shipping_qual' => false, 'smart_label_prefix' => '', ); if (!function_exists('WC') || !WC()->cart) { return $payload; } $packages = WC()->cart->get_shipping_packages(); if (empty($packages)) { return $payload; } $package = reset($packages); if (!is_array($package)) { return $payload; } if (class_exists('AIEO_DMM_Smart_Shipping_Label') && method_exists('AIEO_DMM_Smart_Shipping_Label', 'guest_prefix_label')) { $payload['smart_label_prefix'] = (string) AIEO_DMM_Smart_Shipping_Label::guest_prefix_label(); } if (class_exists('AIEO_DMM_Free_Shipping') && method_exists('AIEO_DMM_Free_Shipping', 'cart_qualifies_for_free')) { $payload['free_shipping_qual'] = (bool) AIEO_DMM_Free_Shipping::cart_qualifies_for_free($package); } $rates_out = array(); if (!empty($package['rates']) && is_array($package['rates'])) { foreach ($package['rates'] as $rate_id => $rate) { if (!is_object($rate)) { continue; } if ($rate->method_id !== 'aieo_table_rate') { continue; } $rates_out[] = array( 'rate_id' => (string) $rate_id, 'label' => (string) $rate->label, 'cost' => (float) $rate->cost, 'rule_id' => (int) ($rate->get_meta_data()['aieo_rule_id'] ?? 0), ); } } $payload['rates'] = $rates_out; return $payload; } public static function schema_callback() { return array( 'rates' => array( 'description' => __('AIEO-resolved shipping rates with their underlying rule ids.', 'ai-eshop-optimizer'), 'type' => 'array', 'context' => array('view', 'edit'), 'readonly' => true, 'items' => array( 'type' => 'object', 'properties' => array( 'rate_id' => array('type' => 'string', 'context' => array('view'), 'readonly' => true), 'label' => array('type' => 'string', 'context' => array('view'), 'readonly' => true), 'cost' => array('type' => 'number', 'context' => array('view'), 'readonly' => true), 'rule_id' => array('type' => 'integer','context' => array('view'), 'readonly' => true), ), ), ), 'free_shipping_qual' => array( 'description' => __('True if the cart already qualifies for free shipping.', 'ai-eshop-optimizer'), 'type' => 'boolean', 'context' => array('view', 'edit'), 'readonly' => true, ), 'smart_label_prefix' => array( 'description' => __('Smart-label "From X.XX" prefix string for guest carts.', 'ai-eshop-optimizer'), 'type' => 'string', 'context' => array('view', 'edit'), 'readonly' => true, ), ); } } }