Last updated on October 18, 2023

WooCommerce product feed setup

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Create product feeds effortlessly in WooCommerce.

To set up a WooCommerce product feed, you can use the woocommerce_product_feed_available_feed_types filter hook to add a new feed type. This allows you to customize the feed format and content according to your requirements.

Here’s an example code snippet that demonstrates how to add a custom feed type called “my_custom_feed”:

function wpsnippets_add_custom_feed_type($feed_types) {
    $feed_types['my_custom_feed'] = 'My Custom Feed';
    return $feed_types;
}
add_filter('woocommerce_product_feed_available_feed_types', 'wpsnippets_add_custom_feed_type');

In this example, the wpsnippets_add_custom_feed_type function adds a new feed type to the existing feed types array. The key 'my_custom_feed' represents the feed type slug, and the value 'My Custom Feed' is the display name of the feed type.

Once you’ve added the custom feed type, you can then generate the feed content by using the woocommerce_product_feed_item filter hook. This filter allows you to modify the feed item data for each product.

Here’s an example code snippet that demonstrates how to add custom data to the feed item:

function wpsnippets_add_custom_feed_data($item, $product) {
    $item['custom_field'] = get_post_meta($product->get_id(), 'custom_field', true);
    return $item;
}
add_filter('woocommerce_product_feed_item', 'wpsnippets_add_custom_feed_data', 10, 2);

In this example, the wpsnippets_add_custom_feed_data function adds a custom field called 'custom_field' to the feed item data. It retrieves the value of the custom field using the get_post_meta function and assigns it to the 'custom_field' key in the $item array.

By using these code snippets, you can set up a WooCommerce product feed and customize its format and content to meet your specific needs.

Examples

Example 1: Creating a Custom Product Feed for WooCommerce

This example demonstrates how to create a custom product feed for WooCommerce using the woocommerce_product_feed_available_types filter hook.

function wpsnippets_custom_product_feed( $feed_types ) {
    $feed_types['custom_feed'] = 'Custom Product Feed';
    return $feed_types;
}
add_filter( 'woocommerce_product_feed_available_types', 'wpsnippets_custom_product_feed' );

In this code example, we are adding a new custom product feed type called “Custom Product Feed” using the woocommerce_product_feed_available_types filter hook. By adding this code to your theme’s functions.php file or a custom plugin, you will be able to select the “Custom Product Feed” option when configuring product feeds in WooCommerce.

Example 2: Modifying Product Feed Content

This example demonstrates how to modify the content of a product feed generated by WooCommerce using the woocommerce_product_feed_item filter hook.

function wpsnippets_modify_product_feed_content( $item, $product ) {
    // Modify the $item array as needed
    $item['custom_field'] = get_post_meta( $product->get_id(), 'custom_field', true );
    return $item;
}
add_filter( 'woocommerce_product_feed_item', 'wpsnippets_modify_product_feed_content', 10, 2 );

In this code example, we are using the woocommerce_product_feed_item filter hook to modify the content of each product item in the feed. By adding this code to your theme’s functions.php file or a custom plugin, you can customize the product feed content by adding or modifying fields. In this example, we are adding a custom field called “custom_field” to the feed item, which retrieves its value from a custom field associated with the product.

Example 3: Customizing Product Feed Output

This example demonstrates how to customize the output of a product feed generated by WooCommerce using the woocommerce_product_feed_item_output filter hook.

function wpsnippets_customize_product_feed_output( $output, $item, $product ) {
    // Customize the $output string as needed
    $output .= '<g:custom_tag>' . $item['custom_field'] . '</g:custom_tag>';
    return $output;
}
add_filter( 'woocommerce_product_feed_item_output', 'wpsnippets_customize_product_feed_output', 10, 3 );

In this code example, we are using the woocommerce_product_feed_item_output filter hook to customize the output of each product item in the feed. By adding this code to your theme’s functions.php file or a custom plugin, you can modify the XML output of the product feed. In this example, we are appending a custom XML tag called “custom_tag” to the output, which includes the value of the “custom_field” we added in the previous example.

Last updated on October 18, 2023. Originally posted on January 9, 2024.

Leave a Reply

Your email address will not be published. Required fields are marked *