実現したいこと
・ECCUBE4から商品情報を取得してWordPressの固定ページに商品一覧を表示させたい(※在庫なし商品のみ)
前提
EC-CUBE4のプラグイン「Web API」とWordPressのプラグイン「Code Snippets」を使用して、
WordPressの固定ページにショートコードで
商品一覧を表示しようとしていますが方法が分からずに躓いております。
ご教授いただけますと幸いです。
下記のページを参考に実装作業をしています。
https://doc.ec-cube.net/eccube-api4/
https://cajiya.co.jp/column/7620
現状できている事
参考にしたページの順序通りに実装したところ
商品ID毎に取得する事には成功しております。
在庫が1以上あるものは表示しないようにすることも成功しております。
試したこと
php
1/** 2* IDから商品を取得&表示するショートコード 3* 4* @param int $atts[id] EC-CUBEの商品ID 5* @return string|false HTML 6*/ 7if( !function_exists('shortcode__eccube_product') ) : 8function shortcode__eccube_product( $atts ) { 9 $id = false; 10 if( isset($atts['id']) ) $id = $atts['id']; 11 if( !$id ) return false; 12 13 $query = <<< EOD 14 query { 15 product(id: {$id}) { 16 id 17 name 18 ProductClasses{ 19 stock 20 price01 21 price02 22 visible 23 } 24 ProductCategories{ 25 Category{ 26 name 27 } 28 } 29 ProductImage{ 30 file_name 31 } 32 } 33 } 34EOD; 35 36 $response = json_decode( request_query_to_eccube($query) ); 37 if( $response == false || isset($response->error) ){ 38 return false; 39 $accesstoken = refresh_eccube_access_token(); 40 if( isset( $accesstoken ) ){ 41 $response = json_decode( request_query_to_eccube($query) ); 42 } 43 } 44 if( $response != false && !isset($response->error) ){ 45 $product = convert_raw_product_data( $response->data->product ); 46 if( !$product ) return false; 47 if( ($product->stock)>=1 ) return false; 48 // return json_encode($product); 49 return <<< EOD 50 <div class="itemlist" id="item-{$product->id}"> 51 <a href="{$product->url}" target="_blank" rel="noopener noreferrer"> 52 <div class="thumb"><img src="https://www.example.com/store/html/upload/save_image/{$product->thumbnail}" alt=""> 53 <div class="text"> 54 <!--p>{$product->category}</p--> 55 <h3>{$product->name}</h3> 56 <!--p class="price">{$product->price}円(税込){$product->stock} visible{$product->visible}</p--> 57 </div> 58 </div> 59 </a> 60 </div> 61EOD; 62 } 63} 64endif; 65add_shortcode( 'eccube_product', 'shortcode__eccube_product' ); 66 67/** 68* 商品一覧を取得&表示するショートコード 69*/ 70if( !function_exists('shortcode__eccube_productlist') ) : 71function shortcode__eccube_productlist() { 72 $query = <<< EOD 73 query { 74 products{ 75 id 76 name 77 ProductClasses{ 78 stock 79 price01 80 price02 81 visible 82 } 83 ProductCategories{ 84 Category{ 85 name 86 } 87 } 88 ProductImage{ 89 file_name 90 } 91 } 92} 93 94EOD; 95 96$response = json_decode( request_query_to_eccube($query) ); 97 if( $response == false || isset($response->error) ){ 98 return false; 99 $accesstoken = refresh_eccube_access_token(); 100 if( isset( $accesstoken ) ){ 101 $response = json_decode( request_query_to_eccube($query) ); 102 } 103 } 104 if( $response != false && !isset($response->error) ){ 105 $product = convert_raw_product_data( $response->data->product ); 106 if( !$product ) return false; 107 if( ($product->stock)>=1 ) return false; 108 // return json_encode($product); 109 return <<< EOD 110 <div class="itemlist" id="item-{$product->id}"> 111 <a href="{$product->url}" target="_blank" rel="noopener noreferrer"> 112 <div class="thumb"><img src="https://www.example.com/store/html/upload/save_image/{$product->thumbnail}" alt=""> 113 <div class="text"> 114 <!--p>{$product->category}</p--> 115 <h3>{$product->name}</h3> 116 <!--p class="price">{$product->price}円(税込){$product->stock} visible{$product->visible}</p--> 117 </div> 118 </a> 119 </div> 120EOD; 121 } 122} 123endif; 124add_shortcode( 'eccube_product_list', 'shortcode__eccube_productlist' );
補足情報(FW/ツールのバージョンなど)
WordPress 6.1.1
ECCUBE 4.1.2
Web API 2.1.3
Code Snippets 3.2.2
あなたの回答
tips
プレビュー