6
6
7
7
namespace Osaka \StructuredData \Block ;
8
8
9
- use Magento \Backend \Block \Template \Context ;
10
- use Magento \Catalog \Helper \Data ;
11
9
use Magento \Catalog \Block \Product \View ;
12
10
13
11
class Product extends View
14
12
{
13
+
14
+ /**
15
+ * @return string
16
+ */
15
17
public function getStructuredJsonData (): string
16
18
{
17
19
$ productBasicInformation = $ this ->getProduct ();
@@ -25,11 +27,28 @@ public function getStructuredJsonData(): string
25
27
$ structuredData .= '"sku": " ' .$ productBasicInformation ->getSku ().'", ' . "\n\r" ;
26
28
$ structuredData .= '"description": " ' .$ shortDescription .'", ' . "\n\r" ;
27
29
$ structuredData .= '"image": [" ' .$ productBasicInformation ->getMediaConfig ()->getMediaUrl ($ productBasicInformation ->getImage ()).'"] ' . "\n\r" ;
28
-
29
30
$ structuredData .= "} " ;
30
31
32
+ return $ structuredData ;
33
+ }
34
+
35
+ /**
36
+ * @return string
37
+ */
38
+ public function getStructuredJsonDataAsArray (): string
39
+ {
40
+ $ productBasicInformation = $ this ->getProduct ();
31
41
42
+ $ shortDescription = trim (preg_replace ('/ +/ ' , ' ' , preg_replace ('/[^A-Za-z0-9 ]/ ' , ' ' , urldecode (html_entity_decode (strip_tags ($ productBasicInformation ['short_description ' ]))))));
32
43
33
- return $ structuredData ;
44
+ $ structuredData = [];
45
+ $ structuredData ["@context " ]= "https://schema.org/ " ;
46
+ $ structuredData ["@type " ]= 'Product ' ;
47
+ $ structuredData ["name " ] = $ productBasicInformation ->getName ();
48
+ $ structuredData ["sku " ] = $ productBasicInformation ->getSku ();
49
+ $ structuredData ["description " ] = $ shortDescription ;
50
+ $ structuredData ["image " ] = array ($ productBasicInformation ->getMediaConfig ()->getMediaUrl ($ productBasicInformation ->getImage ()));
51
+
52
+ return json_encode ($ structuredData );
34
53
}
35
54
}
0 commit comments