Files
old-new-wiaas/backend/app/plugins/wiaas/includes/api/class-wiaas-package-api.php

78 lines
2.3 KiB
PHP
Raw Normal View History

2018-10-11 07:12:53 +02:00
<?php
class Wiaas_Package_API {
private static $namespace = 'wiaas';
public static function init() {
2018-10-15 05:06:46 +02:00
add_filter('woocommerce_rest_product_object_query', array(__CLASS__, 'filter_packages'), 10, 2);
2018-10-11 07:12:53 +02:00
}
public static function register_routes() {
// TODO: Handle this when assigment of customer to commercial lead is done
register_rest_route( self::$namespace, '/commercial-leads', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array(__CLASS__, 'get_customer_commercial_leads'),
'permission_callback' => 'is_user_logged_in'
) );
}
2018-10-15 05:06:46 +02:00
// TODO: Handle this when assigment of customer to commercial lead is done
public static function get_customer_commercial_leads() {
2018-10-11 07:12:53 +02:00
$commercial_leads = array();
foreach (wiaas_get_commercial_leads() as $id => $name) {
$commercial_leads[] = array(
'id' => $id,
'name' => $name
);
}
return rest_ensure_response($commercial_leads);
}
2018-10-15 05:06:46 +02:00
/**
* 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) {
2018-10-11 07:12:53 +02:00
2018-10-15 05:06:46 +02:00
if ( empty($query['tax_query']) ){
$query['tax_query'] = array();
}
2018-10-11 07:12:53 +02:00
2018-10-15 05:06:46 +02:00
// Moved package status handling here
$query['tax_query'][] = array(
'taxonomy' => 'package_status',
'field' => 'name',
'terms' => Wiaas_Package_Status::AVAILABLE
);
2018-10-11 07:12:53 +02:00
2018-10-15 05:06:46 +02:00
$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_serch_terms = array();
foreach ($pay_types as $pay_type) {
$price_serch_terms[] = '_' . $commercial_lead_id . '_' . $pay_type . '_default';
$price_serch_terms[] = '_' . $commercial_lead_id . '_' . $pay_type . '_customer_' . $customer_id;
}
$args['tax_query'][] = array(
'taxonomy' => '_wiaas_shop_prices',
'terms' => $price_serch_terms,
'field' => 'slug'
2018-10-11 07:12:53 +02:00
);
return $args;
}
}
Wiaas_Package_API::init();