BLOGケーススタディ・雑感

2022.06.06 WordPress専用ショッピングカート Welcart。有料プラグインを使用せずに、自動でGoogle Merchant Centerに商品を掲載する。

ショッピング検索結果

Welcartの基本機能に、登録した商品情報とGoogle Merchant Centerの連携機能がありません。
別途、Welcartの運営元のコルネ株式会社より、有料プラグインとして販売されています。(関連リンク参照:本記事投稿時点で、販売停止になっています) 。
WordPressのフィード、feed-rss2.phpを利用してGoogle Merchant CenterへWelcartの商品を連携させ、Google検索のショッピングカテゴリで検索結果として表示させる方法をご紹介します 。
すでに、他の方が、ブログなどでこの方法について説明されている方がいらっしゃいまして、参考にさせていただきました。 (有り難うございました。) 。
しかし、古い情報らしく、紹介されている方法のままでは、きっとうまく行かない場合も多いと思います。Welcartの仕様もアップデートにより日々変化しています。
また、カスタムフィールドなどで商品ページをカスタマイズされているケースも多いため、運営されているサイトに合わせ、コードを最適化する必要があります。
この記事では、Google Merchant Centerと連携できた、一般的な情報としてコードの一部を掲載いたします。
はじめに、使用されているテーマ(子テーマ)内に、「feed-rss2.php」を作成してください。 次に、「functions.php」に下記を追記してください。
参考にはならないと思いますが、有料プラグインを購入しなくても、シンプルなコードで実現可能です。
Google Merchant Centerの仕様に則って、XMLをコーディングしていけば連携できると思います。ご興味のある方は、お問い合わせください。

/* */
/* */
/* */
/* feed-rss2.phpのソースの一部を紹介 */
<?php
header( 'Content-Type: ' . feed_content_type( 'rss2' ) . '; charset=' . get_option( 'blog_charset' ), true ); 
$more = 1;
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?>';
?>
<rss version="2.0"
xmlns:g="http://base.google.com/ns/1.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php do_action( 'rss2_ns' ); ?> >

<?php query_posts("category_name=item&nopaging=1"); ?>
<channel>
<title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
<description><?php bloginfo_rss("description") ?></description>
<link><?php echo get_option('home'); ?></link>
<?php do_action('rss2_head'); ?>
<?php while( have_posts()) : the_post(); usces_the_item();?>

<?php
//if(in_category('item')) {
$skus = get_post_meta($post->ID,'_isku_');
//(省略)
//変数などの出力準備 記述は商品ページのカスタマイズなによります。
?>
<?php $the_itemImageURL=usces_the_itemImageURL(0,'return'); if(!empty($the_itemImageURL)): ?>
<item>
<title><?php usces_the_itemName(); ?></title>
<g:brand>ブランド名</g:brand>
<g:id>post-<?php the_ID(); ?></g:id>
<description><?php if(get_field('main_copy_headline')||get_field('main_copy')):?>
<?php if(get_field('main_copy_headline')) { echo strip_tags(get_field('main_copy_headline')) ;};?>
<?php if(get_field('main_copy')){ echo strip_tags(get_field('main_copy')) ;}?>
<?php else: ?>詳細ページをご覧ください<?php endif; ?>//ACFを使用しています。
</description>
<g:availability><?php if(usces_the_itemZaikoStatus('return')=='在庫有り'){echo 'in_stock';}else if(usces_the_itemZaikoStatus('return')=='近日公開予定'){echo 'backorder';}else{echo 'out_of_stock';}; ?></g:availability>
//在庫表示も構築サイトの内容に合わせて
<g:image_link><?php usces_the_itemImageURL(); ?></g:image_link>
<link><?php the_permalink_rss() ?></link>
<g:mpn><?php usces_the_itemCode(); ?></g:mpn>
<g:gtin>GTINコード</g:gtin> 
//独自変数 GTINを記載した方が結果として反映されやすくなります。※自社アイテムの場合など、コード自体が無い場合は、削除
<g:price><?php echo $price; ?> JPY</g:price>
//独自変数
<?php do_action('rss2_item'); ?>
</item>
<?php endif;?>
<?php endwhile; ?>
</channel>
</rss>

/* */
/* */
/* */
/* functions.php 子テーマフォルダ内にfeed-rss2.phpを利用 */
remove_filter('do_feed_rss2', 'do_feed_rss2', 10);
function custom_feed_rss2(){
$template_file = '/feed-rss2.php';
load_template(get_stylesheet_directory() . $template_file);
}
add_action('do_feed_rss2', 'custom_feed_rss2', 10);