Implement shop search and refactor
This commit is contained in:
@@ -218,7 +218,6 @@ class Wiaas_Cart_API {
|
||||
public static function get_cart_items() {
|
||||
return rest_ensure_response(array(
|
||||
'items' => Wiaas_Cart::get_cart_packages(),
|
||||
'raw' => WC()->cart->get_cart_contents(),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
<?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();
|
||||
@@ -69,9 +69,7 @@ class Wiaas_REST_Customer_API {
|
||||
|
||||
public static function get_customer_shops() {
|
||||
|
||||
$customer_id = wiaas_get_current_user_organization_id();
|
||||
|
||||
$customer_shops = Wiaas_Shop_Data_Store::get_customer_shops($customer_id);
|
||||
$customer_shops = Wiaas_Customer::get_customer_shops();
|
||||
|
||||
$customer_shops = array_map(function($customer_shop) {
|
||||
return array(
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Implements wiaas shop based search on top of woocommerce product api
|
||||
*
|
||||
* Class Wiaas_WC_Package_API_Integration
|
||||
*/
|
||||
class Wiaas_WC_Package_API_Integration {
|
||||
|
||||
/**
|
||||
* Rest base for woocommerce product search
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private static $wc_rest_base = '/wc/v2/products';
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Force wc api request to send shop id when searching packages
|
||||
*
|
||||
* @param $null
|
||||
* @param $request
|
||||
* @param $route
|
||||
* @param $handler
|
||||
*
|
||||
* @return null|WP_Error
|
||||
*/
|
||||
public static function validate_package_search_request($null, $request, $route, $handler) {
|
||||
|
||||
if (strpos($route, self::$wc_rest_base) !== false) {
|
||||
|
||||
if (empty($request['shop_id']) || ! absint($request['shop_id'])) {
|
||||
|
||||
return new WP_Error(
|
||||
'missing_shop',
|
||||
'Shop parameter 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($args['tax_query']) ){
|
||||
$args['tax_query'] = array();
|
||||
}
|
||||
|
||||
// Retrieve only packages with available package status
|
||||
$args['tax_query'][] = array(
|
||||
'taxonomy' => 'package_status',
|
||||
'field' => 'name',
|
||||
'terms' => Wiaas_Package_Status::AVAILABLE
|
||||
);
|
||||
|
||||
/**
|
||||
* Retrieve packages that satisfy one of two:
|
||||
*
|
||||
* 1) Package has at least one visible customer specific price set (for current customer)
|
||||
* 2) Package has at least one visible default price set and not customer specific prices set (for current customer)
|
||||
*
|
||||
* This approach enables us that if package has specific prices set for current customer only those prices
|
||||
* are taken into account and default ones are ignored.
|
||||
* Only if package has no specific prices for current customer default prices are taken into account.
|
||||
*
|
||||
*/
|
||||
|
||||
$shop_id = absint($request['shop_id']);
|
||||
$customer_id = wiaas_get_current_user_organization_id();
|
||||
|
||||
$default_price_search_term = '_' . $shop_id . '_default';
|
||||
$customer_visible_price_search_term = '_' . $shop_id . '_customer_' . $customer_id . '_visible';
|
||||
$customer_hidden_price_search_term = '_' . $shop_id . '_customer_' . $customer_id . '_hidden';
|
||||
|
||||
$args['tax_query'][] = array(
|
||||
'relation' => 'OR',
|
||||
array(
|
||||
'taxonomy' => '_wiaas_shop_prices',
|
||||
'terms' => $customer_visible_price_search_term,
|
||||
'field' => 'slug'
|
||||
),
|
||||
array(
|
||||
array(
|
||||
'taxonomy' => '_wiaas_shop_prices',
|
||||
'terms' => $default_price_search_term,
|
||||
'field' => 'slug'
|
||||
),
|
||||
array(
|
||||
'taxonomy' => '_wiaas_shop_prices',
|
||||
'terms' => $customer_hidden_price_search_term,
|
||||
'field' => 'slug',
|
||||
'operator' => 'NOT IN'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
return $args;
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_WC_Package_API_Integration::init();
|
||||
Reference in New Issue
Block a user