Post by devken » Wed Aug 29, 2018 9:27 pm

Hello,
I am creating schema.org JSON+LD markup for my opencart. I'm using the repeater for breadcrumbs and it's working wonderfully, however to get valid markup I need to remove the comma from the last repeater/breadcrumb item. Here's my code:

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "CollectionPage","breadcrumb":
{"@type": "BreadcrumbList","ItemListElement":
[
<?php $crumbly = 0; ?>
<?php foreach ($breadcrumbs as $breadcrumb):
$crumbly++;
?>
{"@type":"ListItem","position":<?php echo $crumbly; ?>,"item":
{"@id":"<?php echo $breadcrumb['href']; ?>","name":"<?php echo $breadcrumb['text']; ?>"}},
<?php endforeach ?>
]
}}
</script>


...........
The result if there were just two breadcrumb items (minus the comma on the last item) should be:

<script type="application/ld+json">
{"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":
[{"@type":"ListItem","position":1,"item":
{"@id":"https://example.com/","name":"Home"}}, // <-- Comma required because there's another list item to follow this one.
{"@type":"ListItem","position":2,"item":
{"@id":"https://example.com/MyCategory","name":"MyCategory"}} // <-- No comma required as this is the last list item. However, as per my code, it will show a comma here.
]}
</script>


Please advise how I can modify my script. Thanks in advance!

Newbie

Posts

Joined
Wed Aug 29, 2018 8:47 pm

Post by straightlight » Thu Aug 30, 2018 1:03 am

You could do it in four ways:

- By using a for loop statement
- By using an each statement
- By using an array ending with .get()
- By using AJAX requests

and then use the join(',', var) to assemble your JSON strings with commas. If you are not sure how to do it, you can always create a new service request in the Commercial Support section of the forum to get this done as a custom job.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by nobreak » Fri Jan 31, 2020 5:52 pm

devken wrote:
Wed Aug 29, 2018 9:27 pm
Hello,
I am creating schema.org JSON+LD markup for my opencart. I'm using the repeater for breadcrumbs and it's working wonderfully, however to get valid markup I need to remove the comma from the last repeater/breadcrumb item. Here's my code:

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "CollectionPage","breadcrumb":
{"@type": "BreadcrumbList","ItemListElement":
[
<?php $crumbly = 0; ?>
<?php foreach ($breadcrumbs as $breadcrumb):
$crumbly++;
?>
{"@type":"ListItem","position":<?php echo $crumbly; ?>,"item":
{"@id":"<?php echo $breadcrumb['href']; ?>","name":"<?php echo $breadcrumb['text']; ?>"}},
<?php endforeach ?>
]
}}
</script>


...........
The result if there were just two breadcrumb items (minus the comma on the last item) should be:

<script type="application/ld+json">
{"@context":"http://schema.org","@type":"BreadcrumbList","itemListElement":
[{"@type":"ListItem","position":1,"item":
{"@id":"https://example.com/","name":"Home"}}, // <-- Comma required because there's another list item to follow this one.
{"@type":"ListItem","position":2,"item":
{"@id":"https://example.com/MyCategory","name":"MyCategory"}} // <-- No comma required as this is the last list item. However, as per my code, it will show a comma here.
]}
</script>


Please advise how I can modify my script. Thanks in advance!
Hi devken, found a solution?

New member

Posts

Joined
Wed Jul 02, 2014 3:23 pm

Post by thekrotek » Fri Jan 31, 2020 6:00 pm

nobreak wrote:
Fri Jan 31, 2020 5:52 pm
Hi devken, found a solution?
The easiest way is to add all elements to array instead of directly displaying them and then use implode() function.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by nobreak » Fri Jan 31, 2020 6:22 pm

Тhanks. But I'm not a programmer.... ;)

New member

Posts

Joined
Wed Jul 02, 2014 3:23 pm

Post by thekrotek » Fri Jan 31, 2020 6:25 pm

nobreak wrote:
Fri Jan 31, 2020 6:22 pm
Тhanks. But I'm not a programmer.... ;)
Then don't program anything.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by nobreak » Fri Jan 31, 2020 6:29 pm

You're right here, but no one will do it ....

New member

Posts

Joined
Wed Jul 02, 2014 3:23 pm

Post by straightlight » Fri Jan 31, 2020 8:07 pm

nobreak wrote:
Fri Jan 31, 2020 6:29 pm
You're right here, but no one will do it ....
The solution has already been provided when those moments happens: viewtopic.php?f=190&t=206686#p733126 .

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by OSWorX » Fri Jan 31, 2020 10:56 pm

There is a fifth option: post a request in the commercial forum: viewforum.php?f=88
Or a sixth: hire by yourself a developer ...

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria

Post by agatha65 » Sat Feb 01, 2020 4:55 am

@nobreak, what about my extension?
https://www.opencart.com/index.php?rout ... n_id=22943
The stuctured data is very tricky and Google changes the requriments 5-6 times per year.

Suppliers Module - XML, CSV, XLS Product Feed Import and Update
Rich Snippets | Facebook Open Graph Meta Tags | WebP Images
Image


User avatar
Active Member

Posts

Joined
Fri Mar 16, 2012 10:18 am
Location - Canada, QC

Post by nobreak » Sat Feb 01, 2020 8:38 am

agatha65 wrote:
Sat Feb 01, 2020 4:55 am
@nobreak, what about my extension?
https://www.opencart.com/index.php?rout ... n_id=22943
The stuctured data is very tricky and Google changes the requriments 5-6 times per year.
Is this extension work with 1.5.6.4?

New member

Posts

Joined
Wed Jul 02, 2014 3:23 pm

Post by pprmkr » Sat Feb 01, 2020 4:17 pm

devken wrote:
Wed Aug 29, 2018 9:27 pm
...
Please advise how I can modify my script. Thanks in advance!
- Count elements in breadcrumbs
- substract 1 ( for starts at 0, not 1 )
- echo all but last with trailing comma
- echo last without ,

Code: Select all

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "CollectionPage","breadcrumb":
{"@type": "BreadcrumbList","ItemListElement":
[
<?php $crumblies = (count($breadcrumbs)-1); ?>
<?php for ($crumbly = 0; $crumbly < $crumblies; $crumbly++) { ?>
{"@type":"ListItem","position":<?php echo $crumbly; ?>,"item":
{"@id":"<?php echo $breadcrumbs[$crumbly]['href']; ?>","name":"<?php echo $breadcrumbs[$crumbly]['text']; ?>"}},
<?php } ?>
{"@type":"ListItem","position":<?php echo $crumblies; ?>,"item":
{"@id":"<?php echo $breadcrumbs[$crumblies]['href']; ?>","name":"<?php echo $breadcrumbs[$crumblies]['text']; ?>"}}
]
}}
</script>

User avatar
Active Member

Posts

Joined
Sat Jan 08, 2011 11:05 pm
Location - Netherlands

Post by OSWorX » Sat Feb 01, 2020 4:47 pm

nobreak wrote:
Sat Feb 01, 2020 8:38 am
agatha65 wrote:
Sat Feb 01, 2020 4:55 am
@nobreak, what about my extension?
https://www.opencart.com/index.php?rout ... n_id=22943
The stuctured data is very tricky and Google changes the requriments 5-6 times per year.
Is this extension work with 1.5.6.4?
Because - why?!
This here is a thread about OpenCart 2.x .. NOT 1.5.x

Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.


User avatar
Guru Member

Posts

Joined
Mon Jan 11, 2010 10:52 pm
Location - Austria
Who is online

Users browsing this forum: No registered users and 126 guests