Last active
January 3, 2016 16:29
-
-
Save zenith6/8490011 to your computer and use it in GitHub Desktop.
API_ItemSearch の検索結果に商品規格も含めるように拡張したAPIです。
data/downloads/api 以下に配置して下さい。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * This file is part of EC-CUBE | |
| * | |
| * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved. | |
| * | |
| * http://www.lockon.co.jp/ | |
| * | |
| * This program is free software; you can redistribute it and/or | |
| * modify it under the terms of the GNU General Public License | |
| * as published by the Free Software Foundation; either version 2 | |
| * of the License, or (at your option) any later version. | |
| * | |
| * This program is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| * GNU General Public License for more details. | |
| * | |
| * You should have received a copy of the GNU General Public License | |
| * along with this program; if not, write to the Free Software | |
| * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| */ | |
| /** | |
| * API_ItemSearch の検索結果に商品規格も含めるように拡張したAPIです。 | |
| * | |
| * @package Api | |
| * @author Seiji Nitta | |
| * @see API_ItemSearch | |
| */ | |
| require_once CLASS_EX_REALDIR . 'api_extends/operations/ItemSearch.php'; | |
| class API_ItemSearchWithClass extends API_ItemSearch_Ex { | |
| public function doAction($arrParam) { | |
| $successful = parent::doAction($arrParam); | |
| if (!$successful) { | |
| return false; | |
| } | |
| $query = SC_Query_Ex::getSingletonInstance(); | |
| // 検索結果に含まれる商品IDを取得 | |
| $product_ids = array(); | |
| $response = $this->getResponseArray(); | |
| $response_key = 'Item'; | |
| foreach ($response[$response_key] as $key => $product) { | |
| $product_ids[$key] = $product['product_id']; | |
| } | |
| // 検索結果が空なら何もしない | |
| if (!$product_ids) { | |
| return true; | |
| } | |
| // 各商品の商品規格を取得 | |
| $from = 'dtb_products_class AS pc' | |
| . ' LEFT JOIN mtb_product_type AS pt ON pc.product_type_id = pt.id' | |
| . ' LEFT JOIN dtb_classcategory AS cc1 ON cc1.classcategory_id = pc.classcategory_id1' | |
| . ' LEFT JOIN dtb_class AS c1 ON c1.class_id = cc1.class_id' | |
| . ' LEFT JOIN dtb_classcategory AS cc2 ON cc2.classcategory_id = pc.classcategory_id2' | |
| . ' LEFT JOIN dtb_class AS c2 ON c2.class_id = cc2.class_id'; | |
| $placeholders = implode(',', array_fill(0, count($product_ids), '?')); | |
| $where = 'pc.del_flg = ?' | |
| . ' AND cc1.del_flg = ?' | |
| . ' AND cc2.del_flg = ?' | |
| . " AND pc.product_id IN ($placeholders)"; | |
| $where_values = array_merge(array(0, 0, 0), $product_ids); | |
| $where .= ' ORDER BY cc1.rank DESC, cc2.rank DESC'; | |
| $cols = implode(',', array( | |
| 'product_id', | |
| 'product_class_id', | |
| 'product_code', | |
| 'stock', | |
| 'stock_unlimited', | |
| 'sale_limit', | |
| 'price01', | |
| 'price02', | |
| 'deliv_fee', | |
| 'point_rate', | |
| 'down_filename', | |
| 'down_realfilename', | |
| 'pt.name AS product_type', | |
| 'c1.name AS class1_name', | |
| 'cc1.name AS classcategory1_name', | |
| 'c2.name AS class2_name', | |
| 'cc2.name AS classcategory2_name', | |
| )); | |
| $rows = $query->select($cols, $from, $where, $where_values); | |
| $products_classes = array(); | |
| foreach ($rows as $row) { | |
| $product_class = $row; | |
| unset($product_class['product_id']); | |
| $products_classes[$row['product_id']]['product_class'][] = $product_class; | |
| } | |
| // 検索結果に商品規格をマージ | |
| foreach ($product_ids as $key => $product_id) { | |
| $response[$response_key][$key]['product_classes'] = array_key_exists($product_id, $products_classes) | |
| ? $products_classes[$product_id] | |
| : array(); | |
| } | |
| // レスポンスを上書き | |
| $this->setResponse($response_key, $response[$response_key]); | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment