173 lines
3.7 KiB
PHP
173 lines
3.7 KiB
PHP
<?php
|
|
|
|
class Wiaas_Shop_DB {
|
|
|
|
/**
|
|
* Inserts new customer for shop
|
|
*
|
|
* @param int $owner_id
|
|
* @param array $customer_ids
|
|
*/
|
|
public static function add_shop_customers($owner_id, $customer_ids) {
|
|
global $wpdb;
|
|
|
|
foreach ($customer_ids as $customer_id) {
|
|
$wpdb->insert( $wpdb->prefix . 'wiaas_shop_customer_relationships',
|
|
array(
|
|
'customer_id' => $customer_id,
|
|
'shop_owner_id' => $owner_id,
|
|
'order_type' => Wiaas_Shop::get_default_order_type($owner_id)
|
|
),
|
|
array( '%d', '%d', '%s')
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Updates order type for customer in shop
|
|
*
|
|
* @param int $owner_id
|
|
* @param int $customer_id
|
|
* @param string $order_type
|
|
*
|
|
* @return bool|WP_Error
|
|
*/
|
|
public static function update_shop_customer_order_type($owner_id, $customer_id, $order_type) {
|
|
global $wpdb;
|
|
|
|
$results = $wpdb->update(
|
|
$wpdb->prefix . 'wiaas_shop_customer_relationships',
|
|
array( 'order_type' => $order_type ),
|
|
array(
|
|
'customer_id' => $customer_id,
|
|
'shop_owner_id' => $owner_id,
|
|
),
|
|
array( '%s' ),
|
|
array( '%d', '%d' )
|
|
);
|
|
|
|
if (false === $results) {
|
|
return new WP_Error('cannot_update_order_type', __( 'Could not update order type.', 'wiaas' ), array( 'status' => 400 ));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
/**
|
|
* Removes customer from shop
|
|
*
|
|
* @param int $owner_id
|
|
* @param array $customer_ids
|
|
*/
|
|
public static function remove_shop_customers($owner_id, $customer_ids) {
|
|
|
|
if (empty($customer_ids)) {
|
|
return;
|
|
}
|
|
|
|
global $wpdb;
|
|
|
|
$customer_ids = array_map('absint', $customer_ids);
|
|
$customer_ids = implode(',', $customer_ids);
|
|
|
|
$wpdb->query(
|
|
$wpdb->prepare(
|
|
"DELETE FROM {$wpdb->prefix}wiaas_shop_customer_relationships
|
|
WHERE shop_owner_id = %d AND customer_id IN (%s)",
|
|
$owner_id,
|
|
$customer_ids )
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Retrieves array of customers assigned to shop
|
|
*
|
|
* @param int $owner_id
|
|
*
|
|
* @return array {
|
|
* @type int customer_id
|
|
* @type string order_typr
|
|
* }
|
|
*/
|
|
public static function get_shop_customers($owner_id) {
|
|
|
|
if ($owner_id === 0) {
|
|
return array();
|
|
}
|
|
|
|
global $wpdb;
|
|
|
|
$results = $wpdb->get_results(
|
|
$wpdb->prepare(
|
|
"SELECT customer_id, order_type
|
|
FROM {$wpdb->prefix}wiaas_shop_customer_relationships
|
|
WHERE shop_owner_id = %d",
|
|
$owner_id )
|
|
);
|
|
|
|
if ( empty( $results ) ) {
|
|
return array();
|
|
}
|
|
|
|
$customers = array_map(function($result_row) {
|
|
return array(
|
|
'customer_id' => $result_row->customer_id,
|
|
'order_type' => $result_row->order_type
|
|
);
|
|
}, $results);
|
|
|
|
return $customers;
|
|
}
|
|
|
|
/**
|
|
* Retrieves array of shops that are assigned to customer
|
|
*
|
|
* @param int $customer_id
|
|
*
|
|
* @return array {
|
|
* @type int owner_id
|
|
* @type string order_type
|
|
* }
|
|
*/
|
|
public static function get_customer_shops($customer_id) {
|
|
global $wpdb;
|
|
|
|
$results = $wpdb->get_results(
|
|
$wpdb->prepare(
|
|
"SELECT customer_id, shop_owner_id, order_type
|
|
FROM {$wpdb->prefix}wiaas_shop_customer_relationships
|
|
WHERE customer_id = %d",
|
|
$customer_id )
|
|
);
|
|
|
|
if (empty($results)) {
|
|
return array();
|
|
}
|
|
|
|
$shops = array_map(function($result_row) {
|
|
return array(
|
|
'owner_id' => $result_row->shop_owner_id,
|
|
'order_type' => $result_row->order_type
|
|
);
|
|
}, $results);
|
|
|
|
return $shops;
|
|
}
|
|
|
|
/**
|
|
* Remove shop
|
|
*
|
|
* @param int $owner_id
|
|
*/
|
|
public static function remove_shop($owner_id) {
|
|
global $wpdb;
|
|
|
|
$wpdb->query(
|
|
$wpdb->prepare(
|
|
"DELETE FROM {$wpdb->prefix}wiaas_shop_customer_relationships
|
|
WHERE shop_owner_id = %d",
|
|
$owner_id )
|
|
);
|
|
}
|
|
} |