How to Create Yandex YML Feed in WordPress

How to Create Yandex YML Feed in WordPress

Category: Guides, WordPress

Introduction

Running a modern online store requires familiarity with various technologies that allow you to integrate your business with different platforms. One such technology is creating a Yandex Market YML feed, which enables you to synchronize your website’s catalog with Yandex services, such as maps and others.

While there are ready-made plugins available in WordPress for this purpose, there are situations where more flexible feed customization is needed. In such cases, creating a feed manually becomes necessary. Fortunately, this process is quite straightforward, and in this article, I’ll guide you through the steps.

Creating the Feed

The first step is to create the feed. This can easily be done using the add_feed() function. Simply open the functions.php file of your theme and insert the following code:

// Provides Yandex YML feed. add_action( 'init', function () { add_feed( 'yandex-yml', function () { header( 'Content-Type: application/xml; charset=UTF-8' ); get_template_part( 'parts/feed', 'yandex-yml' ); }); });

This code registers a new feed in WordPress, which will be accessible at site.com/feed/yandex-yml. In the callback function, we specify that the content for this page will be loaded from the file located at wp-content/themes/your-theme/parts/feed-yandex-yml.php

Generating the Feed

Next, you need to create the feed-yandex-yml.php file in the directory specified above and insert the following code:

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <yml_catalog date="<?php echo wp_date( 'c' ); ?>"> <shop> <!-- Shop --> <name><?php bloginfo( 'sitename' ); ?></name> <company>Company Name</company> <url><?php echo home_url(); ?></url> <platform>WordPress</platform> <!-- Categories --> <categories> <?php foreach ( get_terms([ 'taxonomy' => 'product_category' ]) as $category ) { printf( '<category id="%d">%s</category>', $category->term_id, $category->name, ); } ?> </categories> <!-- Offers --> <offers> <?php $product_query = new WP_Query([ 'post_type' => 'product', 'posts_per_page' => -1, ]); ?> <?php while ( $product_query->have_posts() ) : $product_query->the_post(); ?> <?php $product = wc_get_product(); ?> <!-- <?php the_title(); ?> --> <offer id="<?php the_ID(); ?>"> <name><?php the_title(); ?></name> <description> <![CDATA[ <?php the_content(); ?> ]]> </description> <?php if ( has_post_thumbnail() ) : ?> <picture><?php the_post_thumbnail_url(); ?></picture> <?php endif; ?> <url><?php the_permalink(); ?></url> <price><?php echo $product->get_regular_price(); ?></price> <oldprice><?php echo $product->get_sale_price(); ?></oldprice> <currencyId><?php echo get_woocommerce_currency(); ?></currencyId> <?php $categories = wp_get_post_terms( get_the_ID(), 'product_cat', [ 'fields' => 'ids' ] ); foreach ( $categories as $category_id ) { printf( '<categoryId>%d</categoryId>', $category_id, ); } $brands = wp_get_post_terms( get_the_ID(), 'product_brand', [ 'fields' => 'names' ] ); foreach ( $brands as $brand ) { printf( '<vendor>%s</vendor>', $brand, ); } ?> </offer> <?php endwhile; ?> <?php wp_reset_postdata(); ?> </offers> </shop> </yml_catalog>

This code serves as a template and assumes you are using WooCommerce. Creating such a feed requires some knowledge of WordPress programming and PHP.

Accessing and Configuring the Feed

Once the feed is set up, you can proceed to Yandex Business or any other required service. In the “Products” section, configure the feed import by specifying its URL: https://site.com/feed/yandex-yml/

Useful Resources

The feed template provided above does not include all possible fields. If you wish to customize the feed in more detail, you can refer to Yandex’s official documentation on YML feed configuration:

Related Posts

by the same categories