質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Symfony2

Symphony2は、簡単で速く堅牢なプログラムの開発サイクルに焦点を当てた、オープンソースのPHPウェブアプリケーション開発フレームワークです。PHP version 5.3.3以上を対象としています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

EC-CUBE

EC-CUBEは、主に日本国内で開発されているECコンテンツ管理システムです。ロックオン社のECKitを元にしてオープンソース化され、商品管理・受注管理・顧客管理・売上集計などECに特化した様々な機能を備えています。

Q&A

解決済

1回答

2055閲覧

ECcubeとSquareの連携。

YUOKAWARA

総合スコア1

Symfony2

Symphony2は、簡単で速く堅牢なプログラムの開発サイクルに焦点を当てた、オープンソースのPHPウェブアプリケーション開発フレームワークです。PHP version 5.3.3以上を対象としています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

EC-CUBE

EC-CUBEは、主に日本国内で開発されているECコンテンツ管理システムです。ロックオン社のECKitを元にしてオープンソース化され、商品管理・受注管理・顧客管理・売上集計などECに特化した様々な機能を備えています。

0グッド

0クリップ

投稿2021/06/10 06:40

●やりたいこと
squareのapiを使用し、ECcubeのフロントページに販売件数順に表示したい。

①squareのAPIを使い、現在のsquareの商品販売件数を取得
②上記dataをECcubeへ取り込む。
③取得したdataを販売件数順に並び替え、フロントページへ表示。

と考えておりますが、squareのどのParameterから取得していいのかがわからない状態です。

前任者の方は、ECcubeとsquareの在庫の連携は作っておりました。
今回は販売件数を取得して、ランキングのような表示をしたいとのことで、こちらの問題が浮上しました。
Squareの連携経験のお有りの方にぜひアドバイスいただきたいいと思っております。
よろしくお願いいたします。

PHP

1<?php 2namespace Customize; 3require 'vendor/autoload.php'; 4use Square\SquareClient; 5use Dotenv\Dotenv; 6class SquareApiConnector 7{ 8 private $client; 9 private $dotenv; 10 public function __construct() 11 { 12 $this->dotenv = new Dotenv(dirname(__FILE__, 3)); 13 $this->dotenv->load(); 14 $this->client = new SquareClient([ 15 'accessToken' => $_ENV['SQUARE_ACCESS_TOKEN'], 16 'environment' => $_ENV['SQUARE_ENVIRONMENT'], 17 ]); 18 } 19 /** 20 * WebhookのPOSTリクエストヘッダのx-square-signatureの妥当性を判定する 21 */ 22 public function isValidSignature($notificationBody, $notificationUrl, $notificationSignature) 23 { 24 // Concatenate your notification URL and 25 // the JSON body of the webhook notification 26 $stringToSign = $notificationUrl . $notificationBody; 27 // Webhook subscription signature key defined in dev portal for app 28 // webhook listener endpoint: http://webhook.site/my-listener-endpoint 29 // Note: Signature key is truncated for illustration 30 $webhookSignatureKey = $_ENV['SQUARE_SIGNATURE_KEY']; 31 // Generate the HMAC-SHA1 signature of the string 32 // signed with your webhook signature key 33 $hash = hash_hmac('sha1', $stringToSign, $webhookSignatureKey, true); 34 $generatedSignature = base64_encode($hash); 35 // Compare HMAC-SHA1 signatures. 36 return (hash_equals($generatedSignature, $notificationSignature)); 37 } 38 /** 39 * Webhookから受信したcategory_object_idから、Squareの商品SKUを取得する 40 */ 41 public function getProductCode($catalogObjectId) 42 { 43 $result = $this->retrieveCatalogObject($catalogObjectId); 44 return $result->getObject()->getItemVariationData()->getSku(); 45 } 46 /** 47 * Square APIのretrieve-catalog-objectを叩く 48 */ 49 public function retrieveCatalogObject($catalogObjectId) 50 { 51 $api_response = $this->client->getCatalogApi()->retrieveCatalogObject($catalogObjectId, false); 52 if ($api_response->isSuccess()) { 53 $result = $api_response->getResult(); 54 return $result; 55 } else { 56 $errors = $api_response->getErrors(); 57 foreach ($errors as $e) { 58 log_error($e->getDetail()); 59 } 60 return null; 61 } 62 } 63 /** 64 * 商品コードを元に、Squareの在庫を上書きする 65 */ 66 public function setSquareStock($productCode, $stock) 67 { 68 // 商品コードでSQUAREの商品を検索する 69 // 分割して取得される場合があるので、カーソルの値がなくなるまでループする 70 $cursor = null; 71 $items = []; 72 do { 73 $searchResult = $this->searchCatalogItems($productCode, $cursor); 74 $items = array_merge($items, $searchResult->getItems()); 75 $cursor = $searchResult->getCursor(); 76 } while (!is_null($cursor) && $cursor != ''); 77 // 取得した商品配列から商品コードに合致する商品IDを取得する 78 $catalogObjectId = ''; 79 foreach ($items as $i) { 80 $variations = $i->getItemData()->getVariations(); 81 foreach ($variations as $v) { 82 if ($v->getItemVariationData()->getSku() == $productCode){ 83 $catalogObjectId = $v->getId(); 84 break; 85 } 86 } 87 } 88 // 商品IDが見つからない場合はエラーログを吐いて処理を中断する 89 if ($catalogObjectId == '') { 90 log_error('product code not found! productCode: ' . $productCode); 91 return; 92 } 93 // SQUAREの商品の在庫数を更新する 94 $this->batchChangeInventory($catalogObjectId, (string) $stock); 95 } 96 /** 97 * Square APIのsearch-catalog-itemsを叩く 98 */ 99 public function searchCatalogItems($productCode, $cursor = null) 100 { 101 $enabled_location_ids = [$_ENV['SQUARE_LOCATION_ID']]; 102 $product_types = ['REGULAR']; 103 $body = new \Square\Models\SearchCatalogItemsRequest(); 104 $body->setTextFilter($productCode); 105 $body->setEnabledLocationIds($enabled_location_ids); 106 $body->setProductTypes($product_types); 107 if (!is_null($cursor) && $cursor != '') { 108 $body->setCursor($cursor); 109 } 110 $api_response = $this->client->getCatalogApi()->searchCatalogItems($body); 111 if ($api_response->isSuccess()) { 112 return $api_response->getResult(); 113 } else { 114 $errors = $api_response->getErrors(); 115 foreach ($errors as $e) { 116 log_error($e->getDetail()); 117 } 118 return null; 119 } 120 } 121 /** 122 * Square APIのbatch-change-inventoryを叩く 123 * Squareの商品IDの在庫数を上書きする 124 */ 125 public function batchChangeInventory($catalogObjectId, $stock) 126 { 127 $physical_count = new \Square\Models\InventoryPhysicalCount(); 128 $physical_count->setCatalogObjectId($catalogObjectId); 129 $physical_count->setState('IN_STOCK'); 130 $physical_count->setLocationId($_ENV['SQUARE_LOCATION_ID']); 131 $physical_count->setQuantity($stock); 132 $physical_count->setOccurredAt(date("Y-m-d\TH:i:s.000+09:00")); 133 $inventory_change = new \Square\Models\InventoryChange(); 134 $inventory_change->setType('PHYSICAL_COUNT'); 135 $inventory_change->setPhysicalCount($physical_count); 136 $changes = [$inventory_change]; 137 $body = new \Square\Models\BatchChangeInventoryRequest(); 138 $body->setIdempotencyKey(uniqid()); 139 $body->setChanges($changes); 140 $body->setIgnoreUnchangedCounts(true); 141 $api_response = $this->client->getInventoryApi()->batchChangeInventory($body); 142 if ($api_response->isSuccess()) { 143 log_info('success inventory updated! catalogObjectId: ' . $catalogObjectId . ' stock: ' . $stock); 144 return $api_response->getResult(); 145 } else { 146 $errors = $api_response->getErrors(); 147 foreach ($errors as $e) { 148 log_error($e->getDetail()); 149 } 150 return null; 151 } 152 } 153 /** 154 * Square APIのlist-catalogを叩く 155 * Squareの商品を全て取得する 156 */ 157 public function listCatalog($cursor = null){ 158 $api_response = $this->client->getCatalogApi()->listCatalog($cursor, 'ITEM'); 159 dump($api_response); 160 if ($api_response->isSuccess()) { 161 log_info('success get list catalogs!'); 162 return $api_response->getResult(); 163 } else { 164 $errors = $api_response->getErrors(); 165 foreach ($errors as $e) { 166 log_error($e->getDetail()); 167 } 168 return null; 169 } 170 } 171 /** 172 * Square APIのBatch-retrieve-inventory-countsを叩く 173 * Squareの在庫を全て取得する 174 * ただし、在庫数はステータスがIN_STOCKのもののみを取得 175 */ 176 public function batchRetrieveInventoryCounts($cursor = null){ 177 $location_ids = [$_ENV['SQUARE_LOCATION_ID']]; 178 $states = ['IN_STOCK']; 179 $body = new \Square\Models\BatchRetrieveInventoryCountsRequest(); 180 $body->setLocationIds($location_ids); 181 $body->setStates($states); 182 if (!is_null($cursor)) { 183 $body->setCursor($cursor); 184 } 185 $api_response = $this->client->getInventoryApi()->batchRetrieveInventoryCounts($body); 186 if ($api_response->isSuccess()) { 187 log_info('success batch retrieve inventory counts!'); 188 return $api_response->getResult(); 189 } else { 190 $errors = $api_response->getErrors(); 191 foreach ($errors as $e) { 192 log_error($e->getDetail()); 193 } 194 return null; 195 } 196 } 197 /** 198 * Squareの全商品の在庫数を含む情報を返す 199 */ 200 public function getItemInventoryData() { 201 // 商品情報を取得 202 // 分割して取得される場合があるので、カーソルの値がなくなるまでループする 203 $cursor = null; 204 $listCatalogs = []; 205 dump($listCatalogs); 206 do { 207 $catalogResult = $this->listCatalog($cursor); 208 $listCatalogs = array_merge($listCatalogs, $catalogResult->getObjects()); 209 $cursor = $catalogResult->getCursor(); 210 } while (!is_null($cursor)); 211 // 商品在庫情報を取得 212 // 分割して取得される場合があるので、カーソルの値がなくなるまでループする 213 $cursor = null; 214 $inventoryCounts = []; 215 do { 216 $inventoryCountsResult = $this->batchRetrieveInventoryCounts($cursor); 217 $inventoryCounts = array_merge($inventoryCounts, $inventoryCountsResult->getCounts()); 218 $cursor = $inventoryCountsResult->getCursor(); 219 } while (!is_null($cursor)); 220 $idHash = []; 221 foreach ($listCatalogs as $catalog) { 222 $itemData = $catalog->getItemData(); 223 $variations = $itemData->getVariations(); 224 foreach($variations as $v) { 225 $item = [ 226 'id' => $v->getId(), 227 'name' => $itemData->getName(), 228 'code' => $v->getItemVariationData()->getSku(), 229 // batchRetrieveInventoryCountsはIN_STOCKが0の在庫データを返さないので0で初期化 230 'stock' => 0 231 ]; 232 $idHash[$item['id']] = $item; 233 } 234 } 235 foreach ($inventoryCounts as $inventory) { 236 $id = $inventory->getCatalogObjectId(); 237 $item = $idHash[$id]; 238 $item['stock'] = $inventory->getQuantity(); 239 $idHash[$item['id']] = $item; 240 } 241 $codeHash = []; 242 foreach ($idHash as $value) { 243 $codeHash[$value['code']] = $value; 244 } 245 return $codeHash; 246 } 247}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tanat

2021/06/10 09:08

現状の質問だと丸投げになっているので、 公式ドキュメント https://developer.squareup.com/docs/ を参照して、関係しそうなAPIを叩いてみて、中身を確認されることをお勧めします。
FKM

2021/06/10 09:26 編集

SquareAPIって在庫管理というより決算システムですけどね。Symfonyが連携している販売実績テーブルからクエリで抽出した方が的確ではないかと思いますが。
YUOKAWARA

2021/06/22 02:42

かしこまりました。 ご意見ありがとうございます。 こちらAPIを再度調べ直して叩いてみます。
YUOKAWARA

2021/07/19 08:04

こちらドキュメントを確認し、Retrieve inventory adjustmentというAPIがそれに近いと思い調べてみました。 こちらのAPIが在庫が減ったときに、SOLDというステータスで販売した数量を確認している様です。 ただ、こちらの中身を確認しましたが、「何個販売している」という値はありませんでした。
tanat

2021/07/19 10:04

このコメント欄はコメントをした人くらいしか見ていないので、質問に追記されることをお勧めします。
guest

回答1

0

ベストアンサー

FKMさんのコメントにある通り、
(作り方次第ですが)EC-CUBE側で注文情報は管理しているはずで、
その情報を取得する方が筋が良いかと思います。


*以下はざっとAPIを探してみただけの情報なので、参考程度と思って頂ければ。

SquareのAPIで情報を取得するのであれば、注文関連のAPIかなと思います。
(商品販売件数=注文のデータのはずなので)

Search orders
で注文一覧を取得して、そのorderIDを使って

Retrieve order
で注文の詳細を取得して集計して表示という感じでいけそうな気がします。

この場合、リアルタイムに販売数を取得するのは厳しいはずなので、
一定時間ごとにデータを取得してランキングを作るようなPHPを作って、cron等で実行するなどの工夫が必要かと思います。

投稿2021/07/19 10:03

tanat

総合スコア18709

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YUOKAWARA

2021/09/07 11:55

ありがとうございます。こちら自己解決いたしました。 クローズさせていただきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問