XML の内容を理解していなかったり、その他、間違いがあるかも。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
<?php /* Welcart に登録されている商品について Google Merchant 向けのデータソース (.xml) を出力する簡易プログラム 本ファイルを wp-load.php と同一のディレクトリーにアップロードして利用します。 サイト毎のカスタマイズが必要です。 Google Merchant Center Next https://merchants.google.com/ 注意:ここに書かれている説明は正しくない場合が有ります。 Google Merchant Center に商品を登録する際に、固有商品 ID (Unique Product Identifier, UPI) の指定が必要です。これには以下の属性が含まれます: GTIN (Global Trade Item Number) 国際的に認識される商品コード。 UPC、EAN、JAN、または ISBN として知られることもあります。 商品に GTIN が存在する場合、正確に入力する必要があります。 MPN (Manufacturer Part Number) メーカーが製品に割り当てた部品番号。 GTIN がない場合、代わりに指定できます。 ブランド (Brand) 商品のブランド名。 固有商品 ID の要件 GTIN が利用可能であれば、それを 必ず 提供する必要があります。 GTIN がない場合は、MPN とブランド の両方を指定します。 自作商品やカスタム製品のように GTIN や MPN が存在しない場合は、商品データに identifier_exists 属性を追加し、その値を FALSE に設定します。 */ // WordPress 環境をロード require_once(__DIR__ . '/wp-load.php'); // ヘッダー設定 header('Content-Type: application/xml; charset=UTF-8'); // XML生成用関数 function generate_google_merchant_xml() { $args = array( 'post_type' => 'post', 'posts_per_page' => -1, // すべての商品を取得 'category_name' => 'item', 'orderby' => 'meta_value', // 商品コードでソート 'order' => 'ASC', // 昇順 'post_status' => 'publish', // 公開中の投稿のみ取得 ); $products = new WP_Query($args); // XMLのヘッダー $xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL; $xml .= '<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">' . PHP_EOL; $xml .= ' <channel>' . PHP_EOL; $xml .= ' <title>' . get_bloginfo('name') . '</title>' . PHP_EOL; $xml .= ' <link>' . home_url() . '</link>' . PHP_EOL; $xml .= ' <description>' . get_bloginfo('description') . '</description>' . PHP_EOL; // 商品データをループ foreach ($products->posts as $product) { global $post; $post = $product; usces_remove_filter(); usces_the_item(); // 最初に呼び出すこと, Welcart オブジェクトの準備 if (!usces_have_skus()) { continue; } // print_r($product); $id = $product->ID; $name = $product->post_title; $description = $product->post_content; $gtin = ''; $mpn = ''; // $id; $brand = ''; // ブランド名 $price = usces_the_itemPrice('return'); $link = get_permalink($product->ID); $number = 0; $image = usces_the_itemImageURL($number, 'return', $post); $stock = usces_the_itemZaikoStatus('return'); if ($stock == '在庫有り') { $stock = 'in stock'; } else { $stock = 'out of stock'; } $xml .= ' <item>' . PHP_EOL; $xml .= ' <g:id>' . htmlspecialchars($id) . '</g:id>' . PHP_EOL; $xml .= ' <g:title>' . htmlspecialchars($name) . '</g:title>' . PHP_EOL; $xml .= ' <g:description>' . htmlspecialchars($description) . '</g:description>' . PHP_EOL; $xml .= ' <g:link>' . htmlspecialchars($link) . '</g:link>' . PHP_EOL; $xml .= ' <g:image_link>' . htmlspecialchars($image) . '</g:image_link>' . PHP_EOL; $xml .= ' <g:price>' . htmlspecialchars($price) . ' JPY</g:price>' . PHP_EOL; $xml .= ' <g:condition>new</g:condition>' . PHP_EOL; $xml .= ' <g:availability>'.$stock.'</g:availability>' . PHP_EOL; if (!empty($gtin)) { $xml .= ' <g:gtin>' . htmlspecialchars($gtin) . '</g:gtin>' . PHP_EOL; } elseif (!empty($mpn) && !empty($brand)) { $xml .= ' <g:mpn>' . htmlspecialchars($mpn) . '</g:mpn>' . PHP_EOL; $xml .= ' <g:brand>' . htmlspecialchars($brand) . '</g:brand>' . PHP_EOL; } else { $xml .= ' <g:identifier_exists>FALSE</g:identifier_exists>' . PHP_EOL; } $xml .= ' </item>' . PHP_EOL; } // XMLのフッター $xml .= ' </channel>' . PHP_EOL; $xml .= '</rss>' . PHP_EOL; return $xml; } // XMLを出力 echo generate_google_merchant_xml(); /* // XMLデータを生成 $xml_content = generate_google_merchant_xml(); // 保存するファイルパス $file_path = __DIR__ . '/generate_google_merchant.xml'; // ファイルに書き込み if (file_put_contents($file_path, $xml_content)) { echo "XMLファイルが正常に生成されました: " . $file_path; } else { echo "XMLファイルの生成中にエラーが発生しました。"; } */ |