Implement shop search and refactor

This commit is contained in:
Almira Krdzic
2018-10-17 00:36:19 +02:00
parent afab22a30b
commit 8769606a4b
24 changed files with 379 additions and 182 deletions

View File

@@ -1,5 +1,13 @@
<?php
/**
* Implements logic for multiple shops existing in one marketplace
*
* Every shop has its owner (organization that is commercial lead) and multiple customers (organizations)
* assigned to it.
*
* Class Wiaas_Shop
*/
class Wiaas_Shop {
public static function init() {
@@ -16,6 +24,10 @@ class Wiaas_Shop {
add_action('wiaas_organization_roles_updated', array(__CLASS__, 'maybe_manage_shop_for_commercial_lead'), 10, 2);
}
public static function get_shop_customers($owner_id) {
return Wiaas_Shop_DB::get_shop_customers($owner_id);
}
/**
* Link customers to shop (this will enable them to search and order packages from this shop)
*
@@ -25,16 +37,16 @@ class Wiaas_Shop {
public static function set_shop_customers($owner_id, $customer_ids) {
$current_customer_ids = wp_list_pluck(
Wiaas_Shop_Data_Store::get_shop_customers($owner_id),
Wiaas_Shop_DB::get_shop_customers($owner_id),
'customer_id');
// delete removed customers
$removed_customer_ids = array_diff($current_customer_ids, $customer_ids);
Wiaas_Shop_Data_Store::remove_shop_customers($owner_id, $removed_customer_ids);
Wiaas_Shop_DB::remove_shop_customers($owner_id, $removed_customer_ids);
// save added customers
$added_customer_ids = array_diff($customer_ids, $current_customer_ids);
Wiaas_Shop_Data_Store::add_shop_customers($owner_id, $added_customer_ids);
Wiaas_Shop_DB::add_shop_customers($owner_id, $added_customer_ids);
}
/**
@@ -66,6 +78,13 @@ class Wiaas_Shop {
}
}
public static function update_shop_customer_order_type($owner_id, $customer_id, $order_type) {
Wiaas_Shop_DB::update_shop_customer_order_type(
$owner_id,
$customer_id,
$order_type);
}
/**
* Creates shop for new shop owner (organization with commercial lead role) or
* deletes existing shop if that role has been removed
@@ -124,24 +143,27 @@ class Wiaas_Shop {
// remove pricing terms for previous prices
if (! empty($old_cl_extras)) {
$old_visible_price_types = array_keys(wp_list_filter($old_cl_extras, array( 'visible' => true )));
$old_terms = self::_get_search_terms_from_cl_extras($owner_id, $old_cl_extras);
$old_terms_names = preg_filter('/^/', '_' . $owner_id . '_', $old_visible_price_types);
wp_remove_object_terms($package_id, $old_terms_names, '_wiaas_shop_prices');
wp_remove_object_terms($package_id, $old_terms, '_wiaas_shop_prices');
}
// get visible price types set by shop owner (commercial lead)
$visible_price_types = array_keys(wp_list_filter($cl_extras, array('visible' => true)));
$new_terms_names = preg_filter('/^/', '_' . $owner_id . '_', $visible_price_types);
$new_terms = self::_get_search_terms_from_cl_extras($owner_id, $cl_extras);
// create term for every visible pricing type and link them to package so package can be queried
wp_add_object_terms($package_id, $new_terms_names, '_wiaas_shop_prices');
wp_add_object_terms($package_id, $new_terms, '_wiaas_shop_prices');
}
// PRIVATE
/**
* Each shop will be registered as product attribute.
* This will persist shops information into database.
* Also every attribute has taxonomy associated with it which will enable us to have multiple
* catalogues in one shop
*
* @param int $owner_id
*/
private static function _maybe_create_shop($owner_id) {
$shop_name = 'wiaas_shop_' . $owner_id;
@@ -149,17 +171,24 @@ class Wiaas_Shop {
if ($attribute_id === 0) {
// create shop attribute
wc_create_attribute(array( 'slug' => $shop_name, 'name' => 'Catalogue' ));
wc_create_attribute(array( 'slug' => $shop_name, 'name' => 'Shop' ));
$taxonomy_name = wc_attribute_taxonomy_name($shop_name);
// since attribute taxonomies are registered once on load
// we will register new attribute taxonomy here so default catalogue can be added
register_taxonomy($taxonomy_name, array('product'));
// add default catalogue option to shop attribute
wp_insert_term( 'Default', $taxonomy_name);
// add default catalogue option to shop
wp_insert_term( 'Default Catalogue', $taxonomy_name);
}
}
/**
* Deleted associated attribute for shop. This will effectively remove shop and all of its potential catalogues
*
* @param int $owner_id
*/
private static function _maybe_remove_shop($owner_id) {
// get corresponding attribute for shop
$attribute_id = wc_attribute_taxonomy_id_by_name('wiaas_shop_' . $owner_id);
@@ -169,6 +198,55 @@ class Wiaas_Shop {
wc_delete_attribute($attribute_id);
}
}
/**
* Generate search terms from cl extras
*
* For default prices search term
* `_{owner_id}_default` will be generated if all set prices are visible
*
* For every customer entry search term
* `_{owner_id}_customer_{customer_id}_visible` will be generated if any of the prices is visible or
* `_{owner_id}_customer_{customer_id}_hidden` if all prices are hidden`
*
* @param int $owner_id
* @param array $cl_extras
*
* @return array
*/
private static function _get_search_terms_from_cl_extras($owner_id, $cl_extras) {
// determine if extras are visible grouped by customer and default settings
$cl_extras_per_customer = array();
$cl_extra_default = false;
foreach ($cl_extras as $cl_extra_type => $cl_extra) {
// is default
if (strpos($cl_extra_type, '_default') !== false) {
// determine if default cl extra is visible
$cl_extra_default = $cl_extra_default || $cl_extra['visible'];
}
// is customer specific
if (strpos($cl_extra_type, '_customer_') !== false) {
$customer_id = absint(explode('_customer_', $cl_extra_type)[1]);
// determine if customer cl extra is visible
$cl_extras_per_customer[$customer_id] = $cl_extras_per_customer[$customer_id] || $cl_extra['visible'];
}
}
$terms = array();
if ($cl_extra_default) {
$terms[] = '_' . $owner_id . '_default';
}
foreach ($cl_extras_per_customer as $customer_id => $visible) {
$terms[] = '_' . $owner_id . '_customer_' . $customer_id . '_' .
($visible ? 'visible' : 'hidden');
}
return $terms;
}
}
Wiaas_Shop::init();