クラス
GroupItem
ソース ソース
ファイル: src/API/GroupItem.php
class GroupItem
{
/**
* Creates a group item and returns it
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \wpdb $wpdb
* @global \usc_e_shop $usces
* @param int $groupId
* @param int $welitemSkuMetaId
* @param float $priceModifier
* @param string $itemLabel
* @param int $itemQuantity
* @param int $position
* @return GenericError|GroupItemType
*/
public static function createGroupItem(
$groupId,
$welitemSkuMetaId,
$priceModifier,
$itemLabel = '',
$itemQuantity = 1,
$position = 0
) {
global $wpdb, $usces;
$groupId = (int)$groupId;
$welitemSkuMetaId = (int)$welitemSkuMetaId;
$priceModifier = (float)$priceModifier;
$itemQuantity = (int)$itemQuantity;
$itemLabel = !empty($itemLabel) ? (string)$itemLabel : '';
$csgit = Schema::getComboSetGroupItemsTable();
$cgroup = API\ComboGroup::getComboGroupById($groupId);
if ($cgroup === null) {
return Master::getErrorStore()->getErrorResponse(
ErrorStore::COMBO_GROUP_NOT_FOUND,
[$groupId],
[],
[$groupId]
);
}
$cgroupcset = API\ComboSet::getComboSetById($cgroup->getComboSetId());
if ($cgroupcset instanceof GenericError) {
return $cgroupcset;
}
$postId = Utils::getSkuPostId($welitemSkuMetaId);
if ($postId instanceof GenericError) {
return $postId;
}
$comboSet = ComboSet::getComboSetBySkuMetaId($welitemSkuMetaId);
if ($comboSet instanceof ComboSetType) {
return Master::getErrorStore()->getErrorResponse(ErrorStore::SKU_IS_COMBO_SET);
}
$welitemctype = (string)$usces->getItemChargingType($postId);
$csetctype = (string)$usces->getItemChargingType($cgroupcset->getPostId());
if (strtolower($csetctype) === 'once' && strtolower($welitemctype) === 'continue') {
$mvars = [__('Normal Charging', 'dlseller'), __('Continuation Charging', 'dlseller')];
$error = Master::getErrorStore()->getErrorResponse(
ErrorStore::INVALID_CHARGE_TYPE_COMBINATION,
$mvars,
[],
$mvars
);
$error->setLogger(new Logger())->setData([
'comboSet' => $cgroupcset->getDataForLog(),
'welitemSkuMetaId' => $welitemSkuMetaId,
]);
return $error;
}
if (empty($position)) {
$items = self::getAllGroupItemsByComboGroupId($groupId);
if (!empty($items)) {
$position = count($items);
}
}
$curtime = Utils::getCurrentUTCDateTimeString();
$res = $wpdb->insert(
$csgit,
[
'group_id' => $groupId,
'welitem_sku_meta_id' => $welitemSkuMetaId,
'item_label' => $itemLabel,
'item_quantity' => $itemQuantity,
'price_modifier' => $priceModifier,
'position' => $position,
'created_at' => $curtime,
'updated_at' => $curtime,
],
['%d', '%d', '%s', '%d', '%f', '%d', '%s', '%s']
);
if (empty($res)) {
return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
}
http_response_code(200);
return self::getGroupItemById($wpdb->insert_id);
}
/**
* Updates a group item and returns it
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \wpdb $wpdb
* @param int $itemId
* @param float $priceModifier
* @param string $itemLabel
* @param int $itemQuantity
* @return GenericError|GroupItemType
*/
public static function updateGroupItem(
$itemId,
$priceModifier,
$itemLabel,
$itemQuantity
) {
global $wpdb;
$itemId = (int)$itemId;
$priceModifier = (float)$priceModifier;
$itemQuantity = (int)$itemQuantity;
$itemLabel = (string)$itemLabel;
$item = self::getGroupItemById($itemId);
if ($item === null) {
return Master::getErrorStore()->getErrorResponse(
ErrorStore::GROUP_ITEM_NOT_FOUND,
[$itemId],
[],
[$itemId]
);
}
$csgit = Schema::getComboSetGroupItemsTable();
$curtime = Utils::getCurrentUTCDateTimeString();
$res = $wpdb->update(
$csgit,
[
'item_label' => $itemLabel,
'item_quantity' => $itemQuantity,
'price_modifier' => $priceModifier,
'updated_at' => $curtime,
],
['ID' => $itemId],
['%s', '%d', '%d', '%s'],
['%d']
);
if (empty($res)) {
return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
}
return self::getGroupItemById($itemId);
}
/**
* Deletes a combo group item
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \wpdb $wpdb
* @param int $itemId
* @return GenericError|true
*/
public static function deleteGroupItem($itemId) {
global $wpdb;
$csgit = Schema::getComboSetGroupItemsTable();
$res = $wpdb->delete($csgit, ['id' => $itemId], ['%d']);
if ($res === false) {
return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
}
return true;
}
/**
* Returns an instance of this class given a group item id
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \wpdb $wpdb
* @param int $id
* @return null|GroupItemType
*/
public static function getGroupItemById($id) {
global $wpdb;
$csgit = Schema::getComboSetGroupItemsTable();
$item = $wpdb->get_row(
$wpdb->prepare("SELECT * FROM {$csgit} WHERE ID = %d", $id),
ARRAY_A
);
if (empty($item)) {
return null;
}
return new GroupItemType(
$item['ID'],
$item['group_id'],
$item['welitem_sku_meta_id'],
$item['item_label'],
$item['item_quantity'],
$item['price_modifier'],
$item['position'],
$item['created_at'],
$item['updated_at']
);
}
/**
* Returns a list of group items for a given group ID
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \wpdb $wpdb
* @param int $id
* @return GenericError|GroupItem[]
*/
public static function getAllGroupItemsByComboGroupId($id) {
global $wpdb;
$csgit = Schema::getComboSetGroupItemsTable();
$itemids = $wpdb->get_results(
$wpdb->prepare("SELECT ID FROM {$csgit} WHERE group_id = %d ORDER BY position ASC", $id),
ARRAY_A
);
if ($itemids === null) {
return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
}
$items = [];
foreach ($itemids as $itemid) {
$item = self::getGroupItemById($itemid['ID']);
if (!empty($item)) {
$items[] = $item;
}
}
return $items;
}
/**
* Checks whether the SKU meta ID exists as a group item in any existing combo-sets
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \wpdb $wpdb
* @param int $skuMetaId
* @return bool|GenericError
*/
public static function skuIsGroupItem($skuMetaId) {
global $wpdb;
$cst = Schema::getComboSetsTable();
$csgt = Schema::getComboSetGroupsTable();
$csgit = Schema::getComboSetGroupItemsTable();
$res = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(cs.ID)
FROM {$cst} cs
JOIN {$csgt} cg ON cg.combo_set_id = cs.ID
JOIN {$csgit} gi ON gi.group_id = cg.ID
WHERE gi.welitem_sku_meta_id = %d",
(int)$skuMetaId
)
);
if ($res === null) {
return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
}
return (bool)(int)$res;
}
/**
* Re-orders item positions for a combo group
*
* @author Evan D Shaw <evandanielshaw@gmail.com>
* @global \wpdb $wpdb
* @param int $groupId
* @param int[] $itemIds
* @return GenericError|bool `true` if anything was reordered, `false` otherwise
*/
public static function reorderGroupItems($groupId, $itemIds) {
global $wpdb;
$groupId = (int)$groupId;
$itemIds = !empty($itemIds) ? (array)$itemIds : [];
$updated = false;
$wpdb->query('START TRANSACTION');
foreach ($itemIds as $position => $itemId) {
$item = self::getGroupItemById($itemId);
if ($item === null || $item->getGroupId() !== $groupId || $item->getPosition() === $position) {
continue;
}
$updated = true;
$res = $wpdb->update(
Schema::getComboSetGroupItemsTable(),
[
'position' => $position,
'updated_at' => Utils::getCurrentUTCDateTimeString(),
],
[
'group_id' => $groupId,
'id' => $itemId,
],
['%d', '%s'],
['%d', '%d']
);
if ($res === false) {
$wpdb->query('ROLLBACK');
return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
}
}
$wpdb->query('COMMIT');
return $updated;
}
}
- createGroupItem — Creates a group item and returns it
- deleteGroupItem — Deletes a combo group item
- getAllGroupItemsByComboGroupId — Returns a list of group items for a given group ID
- getGroupItemById — Returns an instance of this class given a group item id
- reorderGroupItems — Re-orders item positions for a combo group
- skuIsGroupItem — Checks whether the SKU meta ID exists as a group item in any existing combo-sets
- updateGroupItem — Updates a group item and returns it