• プラグイン一覧
    - WCEX Item Combo Set
    - WCEX Amazon Pay
    - WCEX Wishlist お気に入りリスト
  • リリース情報
  • お役立ちコラム
  • お問い合わせ
  • サポート
    • よくある質問
      • WCEX Amazon Pay
      • WCEX Wishlist お気に入りリスト
      • wcex-item-combo-set
    • リファレンス
      • WCEX Amazon Pay
      • WCEX Wishlist お気に入りリスト
      • wcex-item-combo-set
新規会員登録
ログイン
新規会員登録
ログイン
カート
  • プラグイン一覧
    • - WCEX Item Combo Set
    • - WCEX Amazon Pay
    • - WCEX Wishlist お気に入りリスト
  • リリース情報
  • お役立ちコラム
  • サポート
    • - よくある質問
      • - WCEX Amazon Pay
      • - WCEX Wishlist お気に入りリスト
      • - wcex-item-combo-set
    • - リファレンス
      • - WCEX Amazon Pay
      • - WCEX Wishlist お気に入りリスト
      • - wcex-item-combo-set
  • お問い合わせ
Aivec APPs > wcex-item-combo-set > クラス > ComboGroup
レファレンス
バージョン
1.0.6
絞り込み:

目次

  • ソース
  • 関数

フック

  • アクション
  • フィルター

ファンクション

    クラス

    ComboGroup

    API methods for a combo group

    ソース #ソース

    ファイル: src/API/ComboGroup.php

    class ComboGroup
    {
        /**
         * Creates a new combo group for a combo-set and returns it
         *
         * @author Evan D Shaw <evandanielshaw@gmail.com>
         * @global \wpdb $wpdb
         * @param int    $comboSetId
         * @param string $label
         * @param bool   $optional
         * @param bool   $enableMultiSelect
         * @return GenericError|ComboGroupType
         */
        public static function createComboGroup($comboSetId, $label = '', $optional = false, $enableMultiSelect = false) {
            global $wpdb;
    
            $position = 0;
            $curtime = Utils::getCurrentUTCDateTimeString();
            $csgt = Schema::getComboSetGroupsTable();
    
            $comboset = ComboSet::getComboSetById($comboSetId);
            if ($comboset instanceof GenericError) {
                return $comboset;
            }
    
            $isgitem = GroupItem::skuIsGroupItem($comboset->getSkuMetaId());
            if ($isgitem instanceof GenericError) {
                return $isgitem;
            }
            if ($isgitem === true) {
                return Master::getErrorStore()->getErrorResponse(ErrorStore::SKU_IS_COMBO_SET_GROUP_ITEM);
            }
    
            $position = (int)$wpdb->get_var(
                $wpdb->prepare("SELECT COUNT(ID) FROM {$csgt} WHERE combo_set_id = %d", $comboSetId)
            );
    
            $res = $wpdb->insert(
                $csgt,
                [
                    'combo_set_id' => $comboSetId,
                    'group_label' => (string)$label,
                    'optional' => (int)$optional,
                    'allow_multiple_selections' => (int)$enableMultiSelect,
                    'position' => $position,
                    'created_at' => $curtime,
                    'updated_at' => $curtime,
                ],
                ['%d', '%s', '%d', '%d', '%d', '%s', '%s']
            );
    
            if ($res === false) {
                return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
            }
    
            return self::getComboGroupById($wpdb->insert_id);
        }
    
        /**
         * Updates a combo group for a combo-set and returns it
         *
         * @author Evan D Shaw <evandanielshaw@gmail.com>
         * @global \wpdb $wpdb
         * @param int    $groupId
         * @param string $label
         * @param bool   $optional
         * @param bool   $enableMultiSelect
         * @return GenericError|ComboGroupType
         */
        public static function updateComboGroup($groupId, $label, $optional, $enableMultiSelect) {
            global $wpdb;
    
            $combogroup = self::getComboGroupById($groupId);
            if (empty($combogroup)) {
                return Master::getErrorStore()->getErrorResponse(
                    ErrorStore::COMBO_GROUP_NOT_FOUND,
                    [$groupId],
                    [],
                    [$groupId]
                );
            }
    
            $curtime = Utils::getCurrentUTCDateTimeString();
            $csgt = Schema::getComboSetGroupsTable();
            $res = $wpdb->update(
                $csgt,
                [
                    'group_label' => (string)$label,
                    'optional' => (int)$optional,
                    'allow_multiple_selections' => (int)$enableMultiSelect,
                    'updated_at' => $curtime,
                ],
                ['ID' => $groupId],
                ['%s', '%d', '%d', '%s'],
                ['%d']
            );
    
            if (empty($res)) {
                return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
            }
    
            return self::getComboGroupById($groupId);
        }
    
        /**
         * Duplicates a combo group
         *
         * @author Evan D Shaw <evandanielshaw@gmail.com>
         * @global \wpdb $wpdb
         * @param int    $groupId
         * @param string $label
         * @return GenericError|ComboGroupType
         */
        public static function duplicateComboGroup($groupId, $label) {
            global $wpdb;
    
            $combogroup = self::getComboGroupById($groupId);
            if (empty($combogroup)) {
                return Master::getErrorStore()->getErrorResponse(
                    ErrorStore::COMBO_GROUP_NOT_FOUND,
                    [$groupId],
                    [],
                    [$groupId]
                );
            }
    
            $wpdb->query('START TRANSACTION');
            $newgroup = self::createComboGroup(
                $combogroup->getComboSetId(),
                !empty($label) ? (string)$label : '',
                $combogroup->getOptional(),
                $combogroup->getEnableMultiSelect()
            );
            if ($newgroup instanceof GenericError) {
                $wpdb->query('ROLLBACK');
                return $newgroup;
            }
    
            foreach ($combogroup->getItems() as $gitem) {
                $newgitem = GroupItem::createGroupItem(
                    $newgroup->getId(),
                    $gitem->getSkuMetaId(),
                    $gitem->getPriceModifier(),
                    $gitem->getItemLabel(),
                    $gitem->getItemQuantity()
                );
                if ($newgitem instanceof GenericError) {
                    $wpdb->query('ROLLBACK');
                    return $newgitem;
                }
            }
    
            $wpdb->query('COMMIT');
    
            $dupgroup = self::getComboGroupById($newgroup->getId());
            if (empty($dupgroup)) {
                return Master::getErrorStore()->getErrorResponse(
                    ErrorStore::COMBO_GROUP_NOT_FOUND,
                    [$newgroup->getId()],
                    [],
                    [$newgroup->getId()]
                );
            }
    
            return $dupgroup;
        }
    
        /**
         * Deletes a combo group
         *
         * @author Evan D Shaw <evandanielshaw@gmail.com>
         * @global \wpdb $wpdb
         * @param int $groupId
         * @return GenericError|true
         */
        public static function deleteComboGroup($groupId) {
            global $wpdb;
    
            $csgt = Schema::getComboSetGroupsTable();
            $res = $wpdb->delete($csgt, ['id' => $groupId], ['%d']);
            if ($res === false) {
                return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
            }
    
            return true;
        }
    
        /**
         * Returns an instance of `\Aivec\Welcart\Extensions\ItemComboSet\Types\ComboGroup`
         * given a group id, or `null` on failure or if not found
         *
         * @author Evan D Shaw <evandanielshaw@gmail.com>
         * @global \wpdb $wpdb
         * @param int $id
         * @return null|ComboGroupType
         */
        public static function getComboGroupById($id) {
            global $wpdb;
    
            $csgt = Schema::getComboSetGroupsTable();
            $group = $wpdb->get_row(
                $wpdb->prepare("SELECT * FROM {$csgt} cg WHERE cg.ID = %d", $id),
                ARRAY_A
            );
    
            if (empty($group)) {
                return null;
            }
    
            $items = GroupItem::getAllGroupItemsByComboGroupId($id);
            if (empty($items)) {
                $items = [];
            }
    
            return new ComboGroupType(
                $group['ID'],
                $group['combo_set_id'],
                $group['group_label'],
                (bool)(int)$group['optional'],
                (bool)(int)$group['allow_multiple_selections'],
                $group['position'],
                $items,
                $group['created_at'],
                $group['updated_at']
            );
        }
    
        /**
         * Returns list of combo groups for a combo-set item
         *
         * @author Evan D Shaw <evandanielshaw@gmail.com>
         * @global \wpdb $wpdb
         * @param int $comboSetId
         * @return ComboGroupType[]|GenericError
         */
        public static function getAllComboGroupsByComboSetId($comboSetId) {
            global $wpdb;
    
            $csgt = Schema::getComboSetGroupsTable();
            $groupids = $wpdb->get_results(
                $wpdb->prepare("SELECT ID FROM {$csgt} WHERE combo_set_id = %d ORDER BY position ASC", (int)$comboSetId),
                ARRAY_A
            );
    
            if ($groupids === null) {
                return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
            }
    
            $groups = [];
            foreach ($groupids as $groupid) {
                $group = self::getComboGroupById($groupid['ID']);
                if (!empty($group)) {
                    $groups[] = $group;
                }
            }
    
            return $groups;
        }
    
        /**
         * Re-orders combo group positions for a combo-set
         *
         * @author Evan D Shaw <evandanielshaw@gmail.com>
         * @global \wpdb $wpdb
         * @param int   $comboSetId
         * @param int[] $groupIds
         * @return GenericError|bool `true` if anything was reordered, `false` otherwise
         */
        public static function reorderComboGroups($comboSetId, $groupIds) {
            global $wpdb;
    
            $comboSetId = (int)$comboSetId;
            $groupIds = !empty($groupIds) ? (array)$groupIds : [];
    
            $updated = false;
            $wpdb->query('START TRANSACTION');
            foreach ($groupIds as $position => $groupId) {
                $group = self::getComboGroupById($groupId);
                if ($group === null || $group->getComboSetId() !== $comboSetId || $group->getPosition() === $position) {
                    continue;
                }
    
                $updated = true;
                $res = $wpdb->update(
                    Schema::getComboSetGroupsTable(),
                    [
                        'position' => $position,
                        'updated_at' => Utils::getCurrentUTCDateTimeString(),
                    ],
                    ['id' => $groupId],
                    ['%d', '%s'],
                    ['%d']
                );
                if ($res === false) {
                    $wpdb->query('ROLLBACK');
                    return Master::getErrorStore()->getErrorResponse(ErrorStore::INTERNAL_SERVER_ERROR);
                }
            }
    
            $wpdb->query('COMMIT');
            return $updated;
        }
    }
    

    ソースを伸ばす ソースを縮める


    関数 #関数

    Top ↑

    • createComboGroup — Creates a new combo group for a combo-set and returns it
    • deleteComboGroup — Deletes a combo group
    • duplicateComboGroup — Duplicates a combo group
    • getAllComboGroupsByComboSetId — Returns list of combo groups for a combo-set item
    • getComboGroupById — Returns an instance of `\Aivec\Welcart\Extensions\ItemComboSet\Types\ComboGroup` given a group id, or `null` on failure or if not found
    • reorderComboGroups — Re-orders combo group positions for a combo-set
    • updateComboGroup — Updates a combo group for a combo-set and returns it

    • 新規会員登録
    • ログイン
      • プラグイン一覧
      • 会社概要
      • リリース情報
      • よくある質問
      • お役立ちコラム
      • お問い合わせ
      • 個人情報保護方針
      • 特定商取引法に基づく表記
      • 情報セキュリティ基本方針
      • 利用規約

    アイベック合同会社は「Welcart」「Amazon Pay」の公式パートナーです。

    ※Amazon、Amazon.co.jp、Amazon Payおよびそれらのロゴは、Amazon.com,inc.またはその関連会社の商標です。

    © 2025 Aivec llc All Rights Reserved.