Skip to content

Instantly share code, notes, and snippets.

@Auke1810
Last active November 8, 2024 10:33
Show Gist options
  • Save Auke1810/04f9ee713d6c973083d6692975350519 to your computer and use it in GitHub Desktop.
Save Auke1810/04f9ee713d6c973083d6692975350519 to your computer and use it in GitHub Desktop.
Dynamic google campaign url
<?php
/*
* With the next function you can use shortcodes in the "Google Campaign URL Builder" settings of a product feed
* created with the WooCommerce Product feed manager from wpmarketingrobot.
* supported shortcodes:
* [product-title] replaced with the product title
* [product-id] replaced with the post ID
* [product-sku] replaced with the product sku
* [product-group-id] replaces with the data from the product_group_id attribute
* [product-brand] replaces with the data from the brand attribute
* Use them in the Campaign Term and Campaign content section of the Google campaign url.
*/
function product_feed_manager_google_campaign_url( $attributes, $feed_id, $product_id ) {
// Get the product object
$product = wc_get_product( $product_id );
// Check if the product exists
if ( ! $product ) {
return $attributes;
}
// Prepare data for replacements
$product_title = urlencode( $product->get_name() ); // URL-encode to handle spaces and special characters
$product_id = $product->get_id();
$product_sku = urlencode( $product->get_sku() );
$product_group_id = $attributes['item_group_id'];
$product_brand = $attributes['brand'];
// Retrieve the existing URL from attributes
$url = isset( $attributes['link'] ) ? $attributes['link'] : '';
// Replace shortcodes with actual values
$url = str_replace( '[product-title]', $product_title, $url );
$url = str_replace( '[product-id]', $product_id, $url );
$url = str_replace( '[product-sku]', $product_sku, $url );
$url = str_replace( '[product-group-id]', $product_group_id, $url );
$url = str_replace( '[product-brand]', $product_brand, $url );
// Update the attributes array with the modified URL
$attributes['link'] = $url;
return $attributes;
}
add_filter( 'wppfm_feed_item_value', 'product_feed_manager_google_campaign_url', 10, 3 );
@Auke1810
Copy link
Author

Auke1810 commented Nov 6, 2024

This feature will be included in a future update of the product feed manager.
Will notice when this functionality is added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment