• Resolved Dominic Heselmans

    (@better-world-webdevelopment)


    Hi,

    How do you create a custom label in WordPress and get it into the feed?

    We want to add a custom made text for Facebook dynamic ads per product, get this into the feed and in the ad.

    How do we do this?

    Thank you for your help.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Antonino Scarfì

    (@antoscarface)

    Hi,

    first off, you should know how to add this custom made text. Then, you should need to add a custom code in the functions.php file of your current theme (or child theme):

    
    add_filter('aepc_feed_item', function($fields, $item) {
    	$fields['g:custom_label_0'] = get_post_meta($item['g:id']);
    	return $fields;
    }, 10, 2);
    

    In this example, I assumed that it’s a custom field of the product saved through the post metas.

    If the custom text will be a woocommerce attribute, you can also replace:

    
    get_post_meta($item['g:id'])
    

    with

    
    wc_get_product($item['g:id'])->get_attribute('attribute_name')
    
    Thread Starter Dominic Heselmans

    (@better-world-webdevelopment)

    Hi Antonio,

    It would be a global attribute (added in /post_type=product&page=product_attributes) we use called facebook_meta and it has an unique value per product. Then do we still use this code in functions.php?

    
    add_filter('aepc_feed_item', function($fields, $item) {
    	$fields['g:custom_label_0'] = wc_get_product($item['g:id'])->get_attribute('facebook_meta')
    	return $fields;
    }, 10, 2);
    

    Additionally do I need to add something to the special mapping section in Pixel caffeine?

    Thank you for your help

    Can you clarify this? I’m trying to do the same but am unsure how to utilise the code snippet.

    My understanding is that I want to include an attribute called Size in the feed, I need to add this code:

    add_filter(‘aepc_feed_item’, function($fields, $item) {
    $fields[‘g:custom_label_0’] = wc_get_product($item[‘g:id’])->get_attribute(‘size’);
    return $fields;
    }, 10, 2);

    Once I have done this, I can add Size as one of the Custom labels, and it will add the Size for all products to the feed.

    Is this correct?

    Plugin Author Antonino Scarfì

    (@antoscarface)

    Hi,

    the snippet should be good for your case. Just add it at the end of the functions.php file and it should be already active. Then, refresh the feed manually and you should have the new field inside each item of the feed.

    Thread Starter Dominic Heselmans

    (@better-world-webdevelopment)

    Hi Antonio,
    I can’t seem to get it to work.

    I added the code to functions.php (I use genesis extender for this)
    2019-10-24_1124

    I added a global term facebook_meta
    2019-10-24_1127

    I’ve added Facebook meta to a product
    2019-10-24_1125

    Then I refreshed the feed but the extra field facebook_meta is not showing even for that specific product.

    Additionally I get a parse error when I use the following code

    add_filter('aepc_feed_item', function($fields, $item) {
    	$fields['g:custom_label_0'] = wc_get_product($item['g:id'])->get_attribute('facebook_meta')
    	return $fields;
    }, 10, 2);

    2019-10-24_1129

    What am I doing wrong?

    Thank you for the help

    Kind regards,
    Dominic

    Plugin Author Antonino Scarfì

    (@antoscarface)

    Hi,

    sorry for the late response.

    You simply missed the final ; in the $fields['g:custom_label_0'] = ... line.
    Here the fixed snippet:

    
    add_filter('aepc_feed_item', function($fields, $item) {
    	$fields['g:custom_label_0'] = wc_get_product($item['g:id'])->get_attribute('facebook_meta');
    	return $fields;
    }, 10, 2);
    
    Thread Starter Dominic Heselmans

    (@better-world-webdevelopment)

    Hi Antonio,

    It’s still not showing up in the feed (I cleaned cache).

    What could I be doing wrong?

    Plugin Author Antonino Scarfì

    (@antoscarface)

    Ok, sorry my fault!

    I reproduced your case on my end and I fixed the code, here it is:

    
    add_filter('aepc_feed_item', function($fields, \PixelCaffeine\ProductCatalog\FeedMapper $item) {
        if ($product = wc_get_product($item->get_item()->get_id())) {
            $fields['g:custom_label_0'] = $product->get_attribute('facebook_meta');
        }
        return $fields;
    }, 10, 2);
    

    It works for me, let me know if it’s so on your end as well ??

    Thread Starter Dominic Heselmans

    (@better-world-webdevelopment)

    Hi Antonino,

    It works!

    Thank you very much for all the help ??

    Plugin Author Antonino Scarfì

    (@antoscarface)

    Awesome! Glad to hear this solved ??
    If you need further help, don’t hesitate to open a new topic ??

    Also, would you take 1 minute to leave a review about your experience with Pixel Caffeine? Here’s the link. Thanks for your help!

    Have a nice day!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Custom Label’ is closed to new replies.