// Yahoo!ショッピング 商品ページ表示
// [my_getItem item_code=商品コード]
function my_getItemFunc( $atts ) {
extract( shortcode_atts( array(
'item_code' => '',
), $atts ) );
if ( $item_code == '' ) {
return '商品コードが未指定です';
}
$appid = 'アプリケーションID'; // アプリケーションID
$storeid = 'ストアID'; // ストアID
$itemcode = $storeid . '_' . $item_code; // 商品コード(ストアID_ストア商品コード)
$url = 'https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemLookup?responsegroup=medium&appid=' . $appid . '&itemcode=' . $itemcode;
$options = [
'http' => [
'method' => 'GET',
'timeout' => 3, // タイムアウト時間
]
];
$json = file_get_contents( $url, false, stream_context_create( $options ) );
// Falseならエラー
if ( $json === false ) {
return 'file_get_contents error';
}
// 200以外のステータスコードはエラー
preg_match( '/HTTP\/1\.[0|1|x] ([0-9]{3})/', $http_response_header[0], $matches );
$statusCode = (int)$matches[1];
if ( $statusCode !== 200 ) {
return 'エラー ステータスコード:' . $statusCode;
}
$jsonarray = json_decode( $json, true );
// totalResultsReturned が1で無いときは非公開とみなす
if ( $jsonarray['ResultSet']['totalResultsReturned'] != 1 ) {
return '販売停止中です';
}
$fprice = number_format( $jsonarray['ResultSet'][0]['Result'][0]['Price']['_value'] );
$html = <<<EOD
<p>商品名:{$jsonarray['ResultSet'][0]['Result'][0]['Name']}</br>
商品コード:{$jsonarray['ResultSet'][0]['Result'][0]['Code']}</p>
<p style="font-size: 18pt; color: #ff0000;">{$jsonarray['ResultSet'][0]['Result'][0]['Headline']}</p>
<p>{$jsonarray['ResultSet'][0]['Result'][0]['Description']}</p>
<p style="color: #ff0000;">販売価格:{$fprice}円(税込)</p>
<form method="post" name="addCart" action="https://order.shopping.yahoo.co.jp/cgi-bin/cart-form" target="_blank" class="cart" accept-charset="EUC-JP">
<input name="vwcatalog" value="$storeid" type="hidden">
<input name="vwitem" value="$item_code" type="hidden">
<label for="vwquantity">数量</label>
<div class="input_area">
<input name="vwquantity" class="counter1" value="1" maxlength="3" onclick="this.select(0, this.value.length);" type="number" data-min="1" data-max="99" pattern="d*" aria-label="数量" aria-describedby="vwquantity" id="vwquantity">
<button type="button" class="btnspinner" data-cal="-1" data-target=".counter1" style="margin-left: auto;">-</button>
<button type="button" class="btnspinner" data-cal="1" data-target=".counter1" style="margin-left: 7px;">+</button>
</div>
<button type="submit" class="btn btn-danger" onclick="YAHOO.JP.sc.addCart(YAHOO.JP.sc.products, YAHOO.JP.sc.prop22);">カートに入れる</button>
</form>
EOD;
return $html;
}
add_shortcode( 'my_getItem', 'my_getItemFunc' );
// カート用 scripts
function my_spinner_scripts() {
wp_enqueue_script( 'spinner', get_stylesheet_directory_uri().'/js/spinner.js', array( 'jquery' ), NULL, true );
}
add_action( 'wp_enqueue_scripts', 'my_spinner_scripts' );