HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/App/Feeds/FeedXmlWriter.php
<?php


namespace App\Feeds;


class FeedXmlWriter
{
    /**
     * Write a google feed
     *
     * @param $products
     */
    public function writeGoogleFeed($products)
    {
        $xmlString = '';

        $xmlString .= '<rss xmlns:g="http://base.google.com/ns/1.0" xmlns:c="http://base.google.com/cns/1.0" version="2.0">';
        $xmlString .= '<channel>';

        $xmlString .= '<title>Deense Kroon</title>';
        $xmlString .= '<description>Deense Kroon Google Feed</description>';
        $xmlString .= '<link>https://' . $_SERVER['HTTP_HOST'] . '/google-feed</link>';
        //$xmlString .= '<pubDate>Tue, 28 Jul 2015 12:31:25 -0000</pubDate>';

        //$xmlString .= '<items>';
        foreach($products as $product)
        {
            $xmlString .= '<!-- Product ' . $product->id . ' -->';

            $xmlString .= '<item>';

            // Basics
            $xmlString .= '<title><![CDATA[' . $product->title . ']]></title>';
            $xmlString .= '<description><![CDATA[' . $product->description . ']]></description>';
            $xmlString .= '<stock>' . $product->stock . '</stock>';
            $xmlString .= '<link><![CDATA[https://' . $_SERVER['HTTP_HOST'] . '/' . $product->link . ']]></link>';

            // Todo: Get from database
            $xmlString .= '<g:google_product_category>' . $product->google_category_id . '</g:google_product_category>';

            // Price
            $xmlString .= '<g:price>' . $product->price . '</g:price>';
            if(isset($product->sale_price))  $xmlString .= '<g:sale_price>' . $product->sale_price . '</g:sale_price>';

            // Other
            $xmlString .= '<g:id>' . $product->variation_id . '</g:id>';
            $xmlString .= '<g:brand><![CDATA[' . $product->brand . ']]></g:brand>';
            $xmlString .= '<g:item_group_id>' . $product->item_group_id . '</g:item_group_id>';
            $xmlString .= '<g:gender>' . $product->gender . '</g:gender>';
            $xmlString .= '<g:color>#' . $product->color . '</g:color>';

            if($product->ean != null) $xmlString .= '<g:ean><![CDATA[' . $product->ean . ']]></g:ean>';
            if($product->size != null) $xmlString .= '<g:size><![CDATA[' . $product->size . ']]></g:size>';

            if(isset($product->images))
            {
                $count = 0;
                foreach($product->images as $key => $image)
                {
                    if($count == 0 )
                    {
                        $xmlString .= '<g:image_link><![CDATA[' . $image . ']]></g:image_link>';
                    }
                    else
                    {
                        $xmlString .= '<g:additional_image_link><![CDATA[' . $image . ']]></g:additional_image_link>';
                    }
                    $count++;
                }
            }
            $xmlString .= '</item>';
        }

        $xmlString .= '</channel>';
        $xmlString .= '</rss>';

       // $xmlString .= '</items>';

        // Output as XML
        header("Content-type: text/xml");
        echo '<?xml version="1.0" encoding="UTF-8" ?>';

        echo $xmlString;
        exit;
    }

    public function writeDaisyconFeed($products)
    {
        $xmlString = '';
        $xmlString .= '<products>';

        foreach($products as $product)
        {
            $xmlString .= '<!-- Product ' . $product->id . ' -->';

            $xmlString .= '<product>';

            // Basics
            $xmlString .= '<title><![CDATA[' . $product->title . ']]></title>';
            $xmlString .= '<description><![CDATA[' . $product->description . ']]></description>';
            $xmlString .= '<link><![CDATA[https://' . $_SERVER['HTTP_HOST'] . '/' . $product->link . ']]></link>';
            $xmlString .= '<internal_id>' . $product->variation_id . '</internal_id>';
            $xmlString .= '<brand><![CDATA[' . $product->brand . ']]></brand>';

            // Category
            $xmlString .= '<category><![CDATA[';
            $xmlString .= empty($product->category) ? 'root' : $product->category;
            $xmlString .= ']]></category>';

            $xmlString .= '<dominant_color>' . $product->color . '</dominant_color>';

            // Gender
            $xmlString .= '<gender>';
            $xmlString .= empty($product->gender) ? 'unisex' : $product->gender;
            $xmlString .= '</gender>';

            // Current price
            isset($product->sale_price) ?
                $xmlString .= '<minimum_price>' . $product->sale_price . '</minimum_price>' :
                $xmlString .= '<minimum_price>' . $product->price . '</minimum_price>' ;
            // Normal price
            $xmlString .= '<old_price>' . $product->price . '</old_price>';

            // Images
            if( ! isset($product->img_large)) $product->img_large = 'no image';
            $xmlString .= '<img_large><![CDATA[' . $product->img_large . ']]></img_large>';
            // Medium image
            $xmlString .= '<img_medium><![CDATA[';
            $xmlString .= isset($product->img_medium) ? $product->img_medium : $product->img_large;
            $xmlString .= ']]></img_medium>';
            // Small image
            $xmlString .= '<img_small><![CDATA[';
            $xmlString .= isset($product->img_small) ? $product->img_small : $product->img_large;
            $xmlString .= ']]></img_small>';

            if($product->ean != null) $xmlString .= '<ean><![CDATA[' . $product->ean . ']]></ean>';

            $xmlString .= '<size><![CDATA[';
            $xmlString .= ($product->size != null) ? $product->size : 'onesize';
            $xmlString .= ']]></size>';

            $xmlString .= '<stock>';
            $xmlString .= $product->stock > 0 ? 'ja' : 'nee';
            $xmlString .= '</stock>';
            // Subcategory
            $xmlString .= '<subcategory><![CDATA[';
            $xmlString .= empty($product->subcategory) ? 'root' : $product->subcategory;
            $xmlString .= ']]></subcategory>';

            $xmlString .= '</product>';
        }

        $xmlString .= '</products>';

        // Output as XML
        header("Content-type: text/xml");
        echo '<?xml version="1.0" encoding="UTF-8" ?>';

        echo $xmlString;
        exit;
    }
}