• Hi, I have a problem with the product feed generation. I wrote a function for adding sale price and some attributes to the feed. But there’s a problem: if I manually refresh the product feed via the pixel caffeine setting page the feed is correct (there are all the values insered by the function) but if the feed automatically refresh (every hour), the sale price and the discount percentage (mapped in the custom field) are blank.
    How can i resolve this bug?

    The function code

    add_filter('aepc_feed_item', function($fields, \PixelCaffeine\ProductCatalog\FeedMapper $item) {
        if ($product = wc_get_product($item->get_item()->get_id())) {
            $fields['g:brand'] = $product->get_attribute('pa_marca');
            $fields['g:color'] = $product->get_attribute('pa_colore');
    		if($product->is_on_sale()){
    			if( $product->is_type('variable')){
            		$percentages = array();
            		$prices = $product->get_variation_prices();
            		foreach( $prices['price'] as $key => $price ){
                		if( $prices['regular_price'][$key] !== $price ){
                            $pezzi[] = $price;
                            $percentages[] = round(100 - ($prices['sale_price'][$key] / $prices['regular_price'][$key] * 100));
                		}
            		}
                    $field['g:sale_price'] = '' +  max($prezzi); + ' EUR';
                   $fields['g:custom_label_1'] = max($percentages) . '%';
        			} else{
                        $field['g:sale_price'] = '' + $product->get_sale_price() + ' EUR';
                        $regular_price = (float) $product->get_regular_price();
                        $sale_price = (float) $product->get_sale_price();
                        $percentage = round(100 - ($sale_price / $regular_price * 100));
                        $fields['g:custom_label_1'] = $percentage . '%';
    				}
    				$fields['g:sale_price_effective_date'] = "2020-01-01T23:59+00:00/2030-01-01T23:59+00:00";	
    			}
        }
        return $fields;
    }, 10, 2);

    Entry manual refresh

    <entry>
    <g:id>030 NERO</g:id>
    <g:title>
    <![CDATA[ 030 DELICE STIVALETTO NERO ]]>
    </g:title>
    <g:description>
    <![CDATA[ Stivaletto con tacco basso prodotto dell’azienda Chiarini Codice: 030 Colore: delice nero &nbsp; ]]>
    </g:description>
    <g:link>https://www.mirell.it/prodotto/030-delice-nero/</g:link>
    <g:brand>Chiarini</g:brand>
    <g:condition>new</g:condition>
    <g:availability>in stock</g:availability>
    <g:price>179 EUR</g:price>
    <g:checkout_url>https://www.mirell.it/prodotto/030-delice-nero/</g:checkout_url>
    <g:image_link>https://www.mirell.it/wp-content/uploads/2019/11/030-DELICE-NERO1.jpg</g:image_link>
    <g:sale_price>143.2 EUR</g:sale_price>
    <g:additional_image_link>https://www.mirell.it/wp-content/uploads/2019/11/030-DELICE-NERO.jpg</g:additional_image_link>
    <g:google_product_category>Clothing & Accessories</g:google_product_category>
    <g:color>Nero</g:color>
    <g:custom_label_1>20%</g:custom_label_1>
    <g:sale_price_effective_date>2020-01-01T23:59+00:00/2030-01-01T23:59+00:00</g:sale_price_effective_date>
    </entry>

    Entry automatic refresh

    <entry>
    <g:id>030 NERO</g:id>
    <g:title>
    <![CDATA[ 030 DELICE STIVALETTO NERO ]]>
    </g:title>
    <g:description>
    <![CDATA[ Stivaletto con tacco basso prodotto dell’azienda Chiarini Codice: 030 Colore: delice nero &nbsp; ]]>
    </g:description>
    <g:link>https://www.mirell.it/prodotto/030-delice-nero/</g:link>
    <g:brand>Chiarini</g:brand>
    <g:condition>new</g:condition>
    <g:availability>in stock</g:availability>
    <g:price>179 EUR</g:price>
    <g:checkout_url>https://www.mirell.it/prodotto/030-delice-nero/</g:checkout_url>
    <g:image_link>https://i1.wp.com/www.mirell.it/wp-content/uploads/2019/11/030-DELICE-NERO1.jpg?fit=600%2C750&ssl=1</g:image_link>
    <g:additional_image_link>https://www.mirell.it/wp-content/uploads/2019/11/030-DELICE-NERO.jpg</g:additional_image_link>
    <g:google_product_category>Clothing & Accessories</g:google_product_category>
    <g:color>Nero</g:color>
    </entry>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Antonino Scarfì

    (@antoscarface)

    Hi,

    I tried to replicate the problem on my local end, but it works without any problem from the both automatic and manual way.

    Where did you put that code snippet?

    Thread Starter martinimarcello00

    (@martinimarcello00)

    I created a custom plugin with all function stored in function.php child theme folder.

    Plugin Author Antonino Scarfì

    (@antoscarface)

    Could you please give more info on what you did to create the custom plugin?
    Did you enable it?
    Did you add the plugin header in the main file?
    Did the code above work when it was in functions.php of the child theme?
    Are you sure the theme file is functions.php and not function.php?
    Was the child theme activated in your website?

    Sorry for the questions, I just want to be sure you are writing the code in the correct way ??

    Thread Starter martinimarcello00

    (@martinimarcello00)

    Did you enable it? yes
    Did you add the plugin header in the main file?yes
    Did the code above work when it was in functions.php of the child theme? No, it doesn’t work. Before switching to a custom plugin I had all the functions (including this one) in the child theme’s functions.php file.
    Are you sure the theme file is functions.php and not function.php? yes
    Was the child theme activated in your website? yes
    Thank you

    Plugin Author Antonino Scarfì

    (@antoscarface)

    Ok thanks for the info.

    I checked better the code you posted and I see two possible reasons why it might not work:

    1) $product = wc_get_product($item->get_item()->get_id()) this might be false because of wc_get_product which my return false if the ID is not found. It may be helpful debug the return value of this to understand if it’s false and then understand why debugging what is the ID returned from $item->get_item()->get_id()

    2) I noticed a typo: the line $field['g:sale_price'] = '' + max($prezzi); + ' EUR'; refers to the variable $prezzi which is not existing and I think the error is a little bit above in $pezzi[] = $price; which I think should be $prezzi[] = $price;

    Give a check and let me know.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Problem with automatic product catalog catagol’ is closed to new replies.