VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: Alexb65 on July 26, 2023, 12:10:59 PM

Title: Bug (and fix) for metadata snippet
Post by: Alexb65 on July 26, 2023, 12:10:59 PM
Hi all

The snippet for product microdata builds a broken JSON if "pricevalidunitl" is not filled.

The bug apperas in valid data becouse the url field in metadata becomes last BUT it is followed by a comma that breaks JSON

The fix is to move the required comma before the addictional "pircevaliunitl" field and $extra variable

in sublayouts/snippets.php the code should look like the following that builds balid JSON

Starting at line 159:

<?php } ?>
  "offers": {
    "@type": "Offer",
    "priceCurrency": "<?php echo $currency->_vendorCurrency_code_3; ?>",
    "availability": "<?php echo $stockog; ?>",
    "price": "<?php echo $product->prices['salesPrice']; ?>",
    "url": "<?php  echo $canonicalUrl; ?>"
<?php if ($priceUntil) { ?>
    ,"priceValidUntil": "<?php  echo $priceUntil; ?>"
<?php  }
if(strlen($extra)){
    echo ",".$extra;
}
?>
--- end at line 172

@Milbo would you please take it in consideration?
Title: Re: Bug (and fix) for metadata snippet
Post by: Milbo on August 14, 2023, 09:47:25 AM
Thank you for this fix. is added.

Btw, did you note the extra?

$extra = '';
if(!empty($product->customfieldsSorted['snippet'])){
foreach ($product->customfieldsSorted['snippet'] as $customfield) {
$extra .= '"'.$customfield->custom_title.'": "'.$customfield->customfield_value.'",';
}
$extra = substr($extra,0,-1);
}


So you can add your own snippets just bei a customfield.
Title: Re: Bug (and fix) for metadata snippet
Post by: Ghost on August 14, 2023, 10:32:25 AM
Just wondering, what is the reason for building a JSON string manually instead of using json_encode()?
Title: Re: Bug (and fix) for metadata snippet
Post by: Milbo on August 14, 2023, 14:12:30 PM
Someone did it that way and now it is a legacy
Title: Re: Bug (and fix) for metadata snippet
Post by: Alexb65 on August 31, 2023, 11:57:39 AM
Great job as usual Milbo

Thank you

BTW the dev svn is out of sync now with version 4.2

I have a couple of tests on possible bugs :-)

Quote from: Milbo on August 14, 2023, 09:47:25 AM
Thank you for this fix. is added.

Btw, did you note the extra?

$extra = '';
if(!empty($product->customfieldsSorted['snippet'])){
foreach ($product->customfieldsSorted['snippet'] as $customfield) {
$extra .= '"'.$customfield->custom_title.'": "'.$customfield->customfield_value.'",';
}
$extra = substr($extra,0,-1);
}


So you can add your own snippets just bei a customfield.
Title: Re: Bug (and fix) for metadata snippet
Post by: Milbo on September 04, 2023, 10:07:30 AM
Should work since some days again. I just had to commit the stuff of the last 2 months.
Title: Re: Bug (and fix) for metadata snippet
Post by: Alexb65 on September 05, 2023, 17:28:50 PM
Yes svn works

Great job as usual @Milbo