ds42774 2018-11-23 21:43
浏览 44

在Wordpress上使用PHP创建XML Feed(Google产品评论)

I am trying to submit my customer reviews to Google so I can start showing my product reviews on Google shopping.

I'm required to submit a feed that looks like this,

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation=
 "http://www.google.com/shopping/reviews/schema/product/2.2/product_reviews.xsd">
    <version>2.2</version>
    <aggregator>
        <name>Sample Reviews Aggregator (if applicable)</name>
    </aggregator>
    <publisher>
        <name>Sample Retailer</name>
        <favicon>http://www.example.com/favicon.png</favicon>
    </publisher>
    <reviews>
        <review>
            <!-- full sample - includes all optional elements/attributes -->
            <review_id>14295</review_id>
            <reviewer>
                <name is_anonymous="true">Anonymous</name>
                <reviewer_id>509769</reviewer_id>
            </reviewer>
            <review_timestamp>2014-04-21T07:07:07Z</review_timestamp>
            <title>Excellent</title>
            <content>Got it for a friend and he loved it.</content>
            <pros>
                <pro>Sleek design</pro>
                <pro>Protects the tablet well</pro>
            </pros>
            <cons>
                <con>Pricey</con>
            </cons>
            <review_url type="singleton">http://www.example.com/review_14295.html</review_url>
            <reviewer_images>
                <reviewer_image>
                    <url>https://example.com/test.jpg</url>
                </reviewer_image>
                <reviewer_image>
                    <url>https://example.com/test.gif</url>
                </reviewer_image>
            </reviewer_images>
            <ratings>
                <overall min="1" max="5">4.4</overall>
            </ratings>
            <products>
                <product>
                    <product_ids>
                        <gtins>
                            <gtin>541710238425</gtin>
                        </gtins>
                        <mpns>
                            <mpn>60101-10000</mpn>
                        </mpns>
                        <skus>
                            <sku>6206</sku>
                        </skus>
                        <brands>
                            <brand>Acme</brand>
                        </brands>
                    </product_ids>
                    <product_name>Tablet Sleeve</product_name>
                    <product_url>http://www.example.com/product_6206.html</product_url>
                </product>
            </products>
            <is_spam>false</is_spam>
            <collection_method>post_fulfillment</collection_method>
            <transaction_id>fulfillment_transaction_11198373</transaction_id>
        </review>
        <review>
            <!-- minimal sample - no optional elements/attributes -->
            <reviewer>
                <name>Jane</name>
            </reviewer>
            <review_timestamp>2014-04-21T07:14:21Z</review_timestamp>
            <content>I was a little skeptical at first, but it grew on me.</content>
            <review_url type="singleton">http://www.example.com/review_59684.html</review_url>
            <ratings>
                <overall min="1" max="5">4.3</overall>
            </ratings>
            <products>
                <product>
                    <product_url>http://www.example.com/product_6207.html</product_url>
                </product>
            </products>
        </review>
    </reviews>
    <deleted_reviews>
        <review_id>10438</review_id>
    </deleted_reviews>
</feed>

So I created a php file that grabs all that data and wraps it in xml, but I'm not sure how to create an xml file using PHP... I reckon I'd want the php to recreate this page as more reviews come in.

I have the following code in a php file.

<?php


    //Get all comments
    $comments = get_comments(
        array(
            'status'      => 'approve',
            'post_status' => 'publish',
            'post_type'   => 'product',
            'parent'      => 0,
            'order'       => 'DESC',
        )
    );
    foreach ( (array) $comments as $comment ) :
        if ( !empty($comment->comment_author) ) {
            if (!empty($comment->user_id)){
                $user = get_userdata($comment->user_id);
                $author = ucfirst(strtolower($user->first_name)).' '.ucfirst(strtolower(substr($user->last_name,0,1))); // this is the actual line you want to change
            } else {
                $author_name = $comment->comment_author;
                $parts = explode(" ", $author_name);
                $author = ucfirst(strtolower($parts[0]).' '.(isset($parts[1]) ? ucfirst($parts[1][0]) : ''));
            }
        } else {
            $author = __('Customer');
        }

////////////////COMMENT VARIABLES
        $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
        $rating_html = wc_get_rating_html( $rating );
        $_date = get_comment_date('', $comment);
        $_product = wc_get_product($comment->comment_post_ID);



$content = <<<EOT
<review>
<review_id><?php $comment->comment_ID; ?></review_id>
<reviewer>
<name><?php echo $author; ?></name>
</reviewer>
<review_timestamp><?php echo $_date; ?></review_timestamp>
<content><?php echo $comment->comment_content; ?></content>
<review_url type="group"><?php echo get_permalink($comment->comment_post_ID); ?></review_url>
<ratings>
<overall min="1" max="5"><?php echo $rating; ?></overall>
</ratings>
<products>
<product>
<product_name><?php echo $_product->get_title(); ?></product_name>
<product_url><?php echo $_product->get_permalink(); ?></product_url>
</product>
</products>
<is_spam>false</is_spam>
<collection_method>post_fulfillment</collection_method>
</review>
EOT;




    endforeach; ?>


    <?xml version="1.0" encoding="UTF-8"?>
    <feed xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation=
        "http://www.google.com/shopping/reviews/schema/product/2.2/product_reviews.xsd">
        <version>2.2</version>
        <publisher>
            <name>Herbal Nitro</name>
            <favicon>https://static.herbalnitro.com/wp-content/uploads/2018/07/Favicon-new.png</favicon>
        </publisher>
        <reviews>
            <?php echo $content; ?>
        </reviews>
    </feed>

Any help would be tremendously helpful!

Sincerely, Jordan

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算