Alter Feed Items
The wppfm_feed_item_value() filter allows you to alter or customize the feed items generated by the WooCommerce Product Feed Manager plugin. This can be useful if you want to dynamically change attributes like titles, descriptions, or other custom feed data based on specific conditions for your products.
Filter Overview
The filter is applied to all feed item attributes, so you can use it to modify any attribute for each product in the feed.
<?php
function function_name( $attributes, $feed_id, $product_id ) {
// Your custom code goes here
// IMPORTANT! Always return the $attributes
return $attributes;
}
add_filter( 'wppfm_feed_item_value', 'function_name', 10, 3 );
?>
Parameters
- $attributes (array): This array contains all the feed item attributes. Each attribute (e.g., title, description, price) can be accessed using $attributes[‘attribute_name’].
Example: $attributes[‘title’] provides the product title. - $feed_id (integer): The unique ID of the feed, which can be used if you want to apply custom logic to specific feeds.
- $product_id (integer): The ID of the WooCommerce product. This is helpful if you need to fetch additional product details or apply custom logic for specific products.
Example Usages
Example 1: Customizing Product Title
You might want to append specific text to the product title, such as product attributes like color and size.
<?php
function modify_product_title_with_color_size( $attributes, $feed_id, $product_id ) {
// Check if the title attribute is set
if ( isset( $attributes['title'] ) ) {
// Retrieve the color and size attributes from the product metadata
$color = get_post_meta( $product_id, 'attribute_pa_color', true );
$size = get_post_meta( $product_id, 'attribute_pa_size', true );
// append them to the title
$attributes['title'] .= " - " . ucfirst( $color ) . "- " . strtoupper( $size );
}
// Return the modified attributes
return $attributes;
}
add_filter( 'wppfm_feed_item_value', 'modify_product_title_with_color_size', 10, 3 );
?>
Example 2: retrieve data from unsupported plugin
Here’s an example script that demonstrates how to use the wppfm_feed_item_value filter to integrate with the Price Based on Country plugin for WooCommerce. This script retrieves the regular price for a specific country (in this case, Canada) and applies it to the feed if the product is set for that region.
This example assumes the user is using the Price Based on Country plugin’s API to retrieve the price for Canada and adjust the feed item accordingly.
Explanation
- Retrieve Target Country Code: The script checks if custom_label_0 exists within $attributes. If it’s set, the country code specified there will be used as the target country.
- Temporary Country Setting: The session currency is set to the target country to retrieve the country-specific price.
- Retrieve Country-Specific Price: Using wc_get_price_based_country_price, the script gets the regular price based on the target country.
- Reset Currency Context: After retrieving the specific price, the session is reset to the site’s default currency.
- Update Feed Price: If a country-specific price is found, it updates the price attribute in the feed.
This setup allows users to create multiple feeds targeting various countries by simply specifying the desired country code in custom_label_0 for each feed.
<?php
function adjust_price_for_custom_country( $attributes, $feed_id, $product_id ) {
// Check if Price Based on Country plugin functions exist
if ( function_exists( 'wc_get_price_based_country_price' ) ) {
// Check if custom_label_0 is set as the country code
$target_country = isset( $attributes['custom_label_0'] ) ? $attributes['custom_label_0'] : null;
if ( $target_country ) {
// Temporarily set the session to the target country
WC()->session->set( 'woocommerce_currency', $target_country );
// Retrieve the regular price for the specified country
$country_price = wc_get_price_based_country_price( 'regular_price', $product_id );
// Restore the original currency setting after getting the price
WC()->session->set( 'woocommerce_currency', get_woocommerce_currency() );
// Set the price attribute to the country-specific price if available
if ( $country_price ) {
$attributes['price'] = $country_price;
}
}
}
// Return the modified attributes with country-specific price
return $attributes;
}
add_filter( 'wppfm_feed_item_value', 'adjust_price_for_custom_country', 10, 3 );
?>
Example 3: Setting a Minimum Price
Here’s an example of how to use the wppfm_feed_item_value filter to exclude products from the feed if their price is below a specified minimum, such as $10. By simply not returning the $attributes array, we can prevent those products from appearing in the feed.
Explanation:
- Price Check: The function checks if the price attribute is set and whether it’s below 10.
- Exclusion Logic: If the price is below 10, the function returns null instead of the $attributes array. This effectively excludes the product from the feed.
- Returning Attributes: For products with a price of 10 or above, $attributes is returned as usual, allowing them to appear in the feed.
This setup makes it easy to filter out products that don’t meet the minimum price requirement, ensuring only products above the threshold are included in the feed.
<?php
function exclude_low_price_products( $attributes, $feed_id, $product_id ) {
// Check if the price attribute exists and is below the minimum threshold
if ( isset( $attributes['price'] ) && $attributes['price'] < 10 ) {
return null; // Exclude product from the feed by not returning $attributes
} else {
// Return $attributes for products that meet the price requirement
return $attributes;
}
}
add_filter( 'wppfm_feed_item_value', 'exclude_low_price_products', 10, 3 );
?>
Important Notes
- Return $attributes: Always ensure the function returns $attributes at the end. This is critical; otherwise, the feed data won’t be processed correctly.
- Condition Checks: Use conditional checks (e.g., $feed_id, $product_id) to ensure that modifications apply only where needed, preventing unexpected changes in other feeds or products.
Conclusion
The wppfm_feed_item_value filter is a flexible way to customize WooCommerce feed items. By adding your custom functions, you can dynamically change titles, descriptions, prices, and any other attributes based on the product’s ID or feed ID.
If you’re not familiar with using filters in your wp-admin, the best and easiest way to do this is by using a plugin like “Code Snippets“. And here you can find a great video explaining the use if this plugin.
After activating the Code Snippets plugin you can use the code in a save way.