77 lines
2.0 KiB
PHP
77 lines
2.0 KiB
PHP
<?php
|
|
|
|
class Wiaas_Package_API {
|
|
|
|
public static function init() {
|
|
|
|
add_filter('woocommerce_rest_product_object_query', array(__CLASS__, 'filter_packages'), 10, 2);
|
|
|
|
add_filter('rest_dispatch_request', array(__CLASS__, 'validate_package_search_request'), 10, 4);
|
|
}
|
|
|
|
public static function register_routes() {
|
|
// do nothing
|
|
}
|
|
|
|
public static function validate_package_search_request($null, $request, $route, $handler) {
|
|
|
|
if (strpos($route, '/wc/v2/products') !== false) {
|
|
|
|
if (empty($request['cl_id']) || ! absint($request['cl_id'])) {
|
|
|
|
return new WP_Error(
|
|
'missing_commercial_lead',
|
|
'Commercial lead is missing',
|
|
array ( 'status' => 400 )
|
|
);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Filter woocommerce REST API query so only valid wiaas packages are returned to the customer
|
|
*
|
|
* @param $args
|
|
* @param $request
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public static function filter_packages($args, $request) {
|
|
|
|
if ( empty($query['tax_query']) ){
|
|
$query['tax_query'] = array();
|
|
}
|
|
|
|
// Retrieve only packages with available package status
|
|
$query['tax_query'][] = array(
|
|
'taxonomy' => 'package_status',
|
|
'field' => 'name',
|
|
'terms' => Wiaas_Package_Status::AVAILABLE
|
|
);
|
|
|
|
|
|
$commercial_lead_id = absint($request['cl_id']);
|
|
|
|
$customer_id = wiaas_get_current_user_organization_id();
|
|
|
|
$pay_types = array_keys(Wiaas_Package_Pricing::get_available_pay_types());
|
|
|
|
$price_search_terms = array();
|
|
foreach ($pay_types as $pay_type) {
|
|
$price_search_terms[] = '_' . $commercial_lead_id . '_' . $pay_type . '_default';
|
|
$price_search_terms[] = '_' . $commercial_lead_id . '_' . $pay_type . '_customer_' . $customer_id;
|
|
}
|
|
|
|
$args['tax_query'][] = array(
|
|
'taxonomy' => '_wiaas_shop_prices',
|
|
'terms' => $price_search_terms,
|
|
'field' => 'slug'
|
|
);
|
|
|
|
return $args;
|
|
}
|
|
}
|
|
|
|
Wiaas_Package_API::init(); |