Google Product Feed – Customizations

Note: This is a Developer level section. If you are unfamiliar with code and resolving potential conflicts, select a WooExpert or Codable for assistance. We are unable to provide support for customizations under our  Support Policy.

Use the short product description instead of description

↑ Back to top

The plugin provides options for choosing how to create the description in the feed. The correct option to choose will depend on how you’ve populated your product data. The full list of available options is shown in the screenshot below:

The default option is “Main product description (full preferred) plus variation description“. 

Creating a bespoke description

↑ Back to top

If you need to completely customise the description used in the feed you can use the snippet below:

<?php
function lw_woocommerce_gpf_feed_item_google( $feed_item, $product ) {
// Modify $feed_item->description here.
return $feed_item;
}
add_filter( 'woocommerce_gpf_feed_item_google', 'lw_woocommerce_gpf_feed_item_google', 10, 2 );
view raw functions.php hosted with ❤ by GitHub

Populate the feed with custom values

↑ Back to top

You can add or override any element value with one of your own using the filters in the plugin. Here’s an example of populating the “brand” element from a custom field called supplier_brand_name:

<?php
function lw_woocommerce_gpf_add_brand( $elements, $product_id, $variation_id = null ) {
if ( ! is_null( $variation_id ) ) {
$id = $variation_id;
} else {
$id = $product_id;
}
$product = wc_get_product( $id );
$brand = $product->get_meta( 'supplier_brand_name' );
if ( ! empty( $brand ) ) {
$elements['brand'] = array( $brand );
}
return $elements;
}
add_filter( 'woocommerce_gpf_elements', 'lw_woocommerce_gpf_add_brand', 11, 3 );
view raw functions.php hosted with ❤ by GitHub

Programatically excluding products

↑ Back to top

If you want to programatically exclude products from the feed based on your own logic, the following snippet shows how to do so.

<?php
function lw_gpf_exclude_product( $excluded, $product_id, $feed_format ) {
// return TRUE to exclude this product
// return FALSE to include this product
// return $excluded to keep the standard behaviour for this product.
return $excluded;
}
add_filter( 'woocommerce_gpf_exclude_product', 'lw_gpf_exclude_product', 11, 3 );
view raw functions.php hosted with ❤ by GitHub

Change the image size used in the feed

↑ Back to top

By default, the feed uses the “full” image style. If you want to send an alternative image size, you can adapt this snippet to change the image size used to any of the registered image sizes. This example sets it to the “shop_catalog” image size.

<?php
function filter_woocommerce_gpf_image_style( $style ) {
return 'shop_catalog';
}
add_filter( 'woocommerce_gpf_image_style', 'filter_woocommerce_gpf_image_style' );
view raw functions.php hosted with ❤ by GitHub

Disable additional images from being included

↑ Back to top

The following snippet will cause only the single, main product image to be sent in the feed.

<?php
add_filter( 'woocommerce_gpf_include_product_gallery_images', '__return_false' );
add_filter( 'woocommerce_gpf_include_attached_images', '__return_false' );
view raw functions.php hosted with ❤ by GitHub

Choose which images are sent

↑ Back to top

The extension automatically includes images in the feed, including:

  • Variation image (on variable products)
  • The “Product Image” set on the main product
  • Any “Product Gallery” images added to the product
  • Any other media files attached to the product in WordPress

The extension will automatically select the first image found from the above as the primary image to use in the feed. If you want to choose exactly which images are / aren’t included and which image is used as the primary image, then you can do so on the edit product page under Product Feed Information. By default, the plugin will show thumbnails of all images that have been found for that product, and indicate whether they are currently included, or excluded, and will indicate any primary image selected by marking with a star.

Screenshot of image management in Google Product Feed extension

Clicking on “Manage images” allows you to modify these settings. Changes you make will be automatically saved.

Image management in Google Product Feed extension

Register custom field for pre-population

↑ Back to top

The pre-population options in the extension do not include “hidden” meta, or custom fields by default. If you want to make a custom field available as a pre-population option then you can use the following snippet, simply replace “_my_custom_field” with the field name of your custom field.

Note: Once this snippet is in place, you will need to click on the “Refresh the field list” link on the settings page before it will be available to select as an option to select.

<?php
function lw_woocommerce_gpf_custom_field_list( $list ) {
// Register the _my_custom_field meta field as a pre-population option.
$list['meta:_my_custom_field'] = 'My custom field';
return $list;
}
add_filter( 'woocommerce_gpf_custom_field_list', 'lw_woocommerce_gpf_custom_field_list' );
view raw functions.php hosted with ❤ by GitHub

Remove shipping weight from feed

↑ Back to top

If you want to remove the shipping weight from the feed you can do so with the following snippet:

<?php
function lw_exclude_shipping_weight( $feed_item ) {
$feed_item->shipping_weight = null;
return $feed_item;
}
add_filter( 'woocommerce_gpf_feed_item_google', 'lw_exclude_shipping_weight' );
view raw functions.php hosted with ❤ by GitHub

Remove shipping dimensions from feed

↑ Back to top

The following snippet can be used to remove all shipping dimensions (length, width and height) from products in the feed:

<?php
function lw_gpf_remove_shipping_attrs($elements, $product_id) {
unset($elements['shipping_width']);
unset($elements['shipping_length']);
unset($elements['shipping_height']);
return $elements;
}
add_filter( 'woocommerce_gpf_elements_google', 'lw_gpf_remove_shipping_attrs', 11, 2 );
view raw functions.php hosted with ❤ by GitHub

Hide attribute labels on variable product titles

↑ Back to top

When generating feed items for variable products, the extension generates unique titles for each variation by including attribute information in the feed title as per Google’s requirements. For example, a “Widget” available in 2 different colors and sizes might have its four items listed as:

  • Widget (Size: Small, Color: Light)
  • Widget (Size: Small, Color: Dark)
  • Widget (Size: Large, Color: Light)
  • Widget (Size: Large, Color: Dark)

If you have many attributes, or complex attribute names on your variable products then this may generate overly long item titles. The extension will allow you to drop the attribute name (“Size” and “Color” in the previous example). This would generate titles like this:

  • Widget (Small, Light)
  • Widget (Small, Dark)
  • Widget (Large, Light)
  • Widget (Large, Dark)

To enable this behaviour use the following snippet:

<?php
add_filter( 'woocommerce_gpf_include_attribute_labels_in_title', '__return_false' );
view raw functions.php hosted with ❤ by GitHub

Modifying the title used in the feed

↑ Back to top

The plugin settings (WooCommerce » Settings » Product Feeds) allow you to choose what is used for the title in the feed. You can choose from the standard product title from WooCommerce, or any of the other supported fields.

As with other feed elements, you can also override the calculated title by supplying a title explicitly in the “Product Feed Information” section of the edit product page – you’ll find the title toward the end of the ‘Common fields‘ sub-section.

If you want to modify the title used in the feed programatically, then the following code snippet shows how you can use a filter to do so.

<?php
function lw_woocommerce_gpf_title( $title, $product_id ) {
// Modify $title here as you want.
return $title;
}
add_filter( 'woocommerce_gpf_title', 'lw_woocommerce_gpf_title', 10, 2 );
view raw functions.php hosted with ❤ by GitHub

Modifying the ID generated in the feed

↑ Back to top

The extension automatically generates a unique ID for every product in the feed that meets Google’s requirements for IDs. If you need to change this then you can do so with the following snippet, although we do not recommend this.

However: Changing the ID is not recommended as the value you choose for it must meet strict requirements including that it should be unique, and non-changing over time. As such values such as the SKU, or MPN aren’t recommended.

Note: Changing the ID format for an existing feed will result in duplicate products being created, with the new version requiring approval and validation before they can be used for product ads.

If you do still wish to change the ID, then the following snippet shows the filter you can use:

<?php
function lw_woocommerce_gpf_feed_item_google( $feed_item, $product ) {
// Modify $feed_item->guid here.
// Note, you may also need to update $feed_item->item_group_id if you have variable products.
return $feed_item;
}
add_filter( 'woocommerce_gpf_feed_item_google', 'lw_woocommerce_gpf_feed_item_google', 11, 2 );
view raw functions.php hosted with ❤ by GitHub

Modifying the product URL sent in the feed

↑ Back to top

Sometimes you may want to customise the product URL sent in the feed for products. You can do this using the filter below, putting whatever logic in place to modify $feed_item->purchase_link to meet your needs.

<?php
function lw_woocommerce_gpf_feed_item_google( $feed_item, $product ) {
// Modify $feed_item->purchase_link here.
return $feed_item;
}
add_filter( 'woocommerce_gpf_feed_item_google', 'lw_woocommerce_gpf_feed_item_google', 10, 2 );
view raw functions.php hosted with ❤ by GitHub

Questions & Feedback

↑ Back to top

Have a question before you buy? Please fill out this pre-sales form.

Already purchased and need some assistance? Open a support ticket via the Help Desk.

Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.