Создать XML Feed с PHP на Wordpress (Google Product Reviews) - PullRequest
0 голосов
/ 24 ноября 2018

Я пытаюсь отправить свои отзывы клиентов в Google, чтобы я мог начать показывать свои отзывы о товарах в Google покупках.

Мне необходимо отправить фид, который выглядит следующим образом:

<?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>

Итак, я создал php-файл, который захватывает все эти данные и упаковывает их в xml, но Я не уверен, как создать XML-файл с использованием PHP ... Я думаю, я бы хотелphp для воссоздания этой страницы по мере поступления новых отзывов.

У меня есть следующий код в php-файле.

<?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>

Любая помощь будет чрезвычайно полезна!

С уважениемИордания

...