Last active
November 8, 2024 10:33
-
-
Save Auke1810/04f9ee713d6c973083d6692975350519 to your computer and use it in GitHub Desktop.
Dynamic google campaign url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This feature will be included in a future update of the product feed manager.
Will notice when this functionality is added.