reseller to customer
This commit is contained in:
@@ -10,6 +10,75 @@ class Wiaas_Shop {
|
||||
|
||||
// update prices search terms for package after prices extras have been updated
|
||||
add_action('wiaas_package_prices_extras_set', array(__CLASS__, 'update_package_prices_search_terms'), 10, 4);
|
||||
|
||||
// create new shop if organization was assigned commercial lead role
|
||||
// or remove shop if commercial lead role was removed for organization
|
||||
add_action('wiaas_organization_roles_updated', array(__CLASS__, 'maybe_manage_shop_for_commercial_lead'), 10, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Link customers to shop (this will enable them to search and order packages from this shop)
|
||||
*
|
||||
* @param int $owner_id
|
||||
* @param array $customer_ids
|
||||
*/
|
||||
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),
|
||||
'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);
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve default order type for shop
|
||||
*
|
||||
* @param int $owner_id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_default_order_type($owner_id) {
|
||||
$order_type = get_term_meta(
|
||||
$owner_id,
|
||||
'_wiaas_shop_default_order_type',
|
||||
true);
|
||||
|
||||
return empty($order_type) ? 'commercial_lead' : $order_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update default order type for shop
|
||||
*
|
||||
* @param int $owner_id
|
||||
* @param string $order_type
|
||||
*/
|
||||
public static function update_default_order_type($owner_id, $order_type) {
|
||||
|
||||
if (in_array($order_type, array('commercial_lead', 'reseller'))) {
|
||||
update_term_meta($owner_id, '_wiaas_shop_default_order_type', $order_type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates shop for new shop owner (organization with commercial lead role) or
|
||||
* deletes existing shop if that role has been removed
|
||||
*
|
||||
* @param int $owner_id
|
||||
* @param array $roles
|
||||
*/
|
||||
public static function maybe_manage_shop_for_commercial_lead($owner_id, $roles) {
|
||||
$is_commercial_lead = in_array('commercial_lead', $roles);
|
||||
|
||||
$is_commercial_lead ?
|
||||
self::_maybe_create_shop($owner_id) :
|
||||
self::_maybe_remove_shop($owner_id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -21,12 +90,12 @@ class Wiaas_Shop {
|
||||
'query_var' => true,
|
||||
'rewrite' => false,
|
||||
'public' => true,
|
||||
'capabilities' => array(
|
||||
'manage_terms' => 'manage_wiaas_package_price_terms',
|
||||
'edit_terms' => 'edit_wiaas_package_price_terms',
|
||||
'delete_terms' => 'delete_wiaas_package_price_terms',
|
||||
'assign_terms' => 'assign_wiaas_package_price_terms',
|
||||
),
|
||||
// 'capabilities' => array(
|
||||
// 'manage_terms' => 'manage_wiaas_package_price_terms',
|
||||
// 'edit_terms' => 'edit_wiaas_package_price_terms',
|
||||
// 'delete_terms' => 'delete_wiaas_package_price_terms',
|
||||
// 'assign_terms' => 'assign_wiaas_package_price_terms',
|
||||
// ),
|
||||
);
|
||||
|
||||
register_taxonomy( '_wiaas_shop_prices', array( 'product' ), $args );
|
||||
@@ -68,7 +137,37 @@ class Wiaas_Shop {
|
||||
$new_terms_names = preg_filter('/^/', '_' . $owner_id . '_', $visible_price_types);
|
||||
|
||||
// create term for every visible pricing type and link them to package so package can be queried
|
||||
wp_set_object_terms($package_id, $new_terms_names, '_wiaas_shop_prices');
|
||||
wp_add_object_terms($package_id, $new_terms_names, '_wiaas_shop_prices');
|
||||
}
|
||||
|
||||
// PRIVATE
|
||||
|
||||
private static function _maybe_create_shop($owner_id) {
|
||||
$shop_name = 'wiaas_shop_' . $owner_id;
|
||||
|
||||
$attribute_id = wc_attribute_taxonomy_id_by_name($shop_name);
|
||||
|
||||
if ($attribute_id === 0) {
|
||||
// create shop attribute
|
||||
wc_create_attribute(array( 'slug' => $shop_name, 'name' => 'Catalogue' ));
|
||||
|
||||
$taxonomy_name = wc_attribute_taxonomy_name($shop_name);
|
||||
|
||||
register_taxonomy($taxonomy_name, array('product'));
|
||||
|
||||
// add default catalogue option to shop attribute
|
||||
wp_insert_term( 'Default', $taxonomy_name);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// if shop attribute exists then remove it
|
||||
if ($attribute_id > 0) {
|
||||
wc_delete_attribute($attribute_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user