@@ -14,44 +14,56 @@ public function render(siteBlockData $data, bool $is_backend, array $tmpl_vars=[
1414 if ($ tmpl_vars ['product ' ]['status ' ] < 0 ) {
1515 $ data ->data ['additional ' ]['html ' ] = _w ('Unavailable for purchase ' );
1616 }
17+
1718 return parent ::render ($ data , $ is_backend , $ tmpl_vars );
1819 }
1920
2021 public function additionalData (siteBlockData $ data )
2122 {
2223 $ hash = '' ;
23- $ fields = 'id,name,summary,images,price,status ' ;
24+ $ fields = 'id,name,summary,description, images,price,status,sku_stock,skus,compare_price ' ;
2425 if (!empty ($ data ->data ['product_id ' ])) {
2526 $ hash = 'id/ ' .$ data ->data ['product_id ' ];
26- }
27- $ products = (new shopProductsCollection ($ hash ))->getProducts ($ fields , 0 , 1 );
28- if (empty ($ products ) || $ hash == '' ) {
29- $ p = ['id ' => null , 'sku_id ' => null , 'skus ' => [], 'sku_type ' => '1 ' , 'status ' => '0 ' ,'price ' => _w ('Product price ' ), 'name ' => _w ('Product name ' ), 'summary ' => _w ('Product description ' )];
30- if (empty ($ products )) {
31- $ p ['status ' ] = '-1 ' ;
27+ $ prod_data = new shopProduct ($ data ->data ['product_id ' ]);
28+ //
29+ $ p = $ prod_data ->data ;
30+ $ p ['skus ' ] = $ prod_data ->skus ;
31+
32+ switch (ifset ($ data ->data , 'info_type ' , 'name ' )) {
33+ case 'price ' :
34+ //$result['html'] = shop_currency($p['price'], ["unit" => $p['currency'], "in_currency" => $p['currency'], "format" => "price_wrapper"]);
35+ $ result_html = wa_currency_html ($ p ['price ' ], $ p ['currency ' ]);
36+ break ;
37+ case 'compare_price ' :
38+ $ result_html = '<span style="text-decoration: line-through;"> ' .wa_currency_html ($ p ['compare_price ' ], $ p ['currency ' ]).'</span> ' ;
39+ break ;
40+ case 'description ' :
41+ $ result_html = strip_tags ($ p ['description ' ]);
42+ break ;
43+ case 'summary ' :
44+ $ result_html = strip_tags ($ p ['summary ' ]);
45+ break ;
46+ case 'stock ' :
47+ $ result_html = strip_tags ($ this ->getStockInfo ($ p ['skus ' ][$ p ['sku_id ' ]]['count ' ]));
48+ break ;
49+ case 'name ' :
50+ default :
51+ $ result_html = htmlspecialchars ($ p ['name ' ]);
52+ break ;
3253 }
54+
3355 } else {
34- $ p = reset ($ products );
35- //unset($data->data['html']);
56+ $ p = ['id ' => null , 'sku_id ' => null , 'skus ' => [], 'sku_type ' => '1 ' , 'status ' => '0 ' ,'price ' => _w ('Product price ' ), 'name ' => _w ('Product name ' ), 'summary ' => _w ('Product description ' )];
57+
58+ $ result_html = _w ('Choose product ' );
3659 }
37-
38- $ data ->data ['product_id ' ] = $ p ['id ' ];
60+ //wa_dump($prod_data['skus']);
3961 $ result = [
4062 'product ' => $ p ,
4163 ];
64+ $ result ['html ' ] = $ result_html ;
65+
4266
43- switch (ifset ($ data ->data , 'info_type ' , 'name ' )) {
44- case 'price ' :
45- $ result ['html ' ] = shop_currency ($ p ['price ' ]);
46- break ;
47- case 'description ' :
48- $ result ['html ' ] = strip_tags ($ p ['summary ' ]);
49- break ;
50- case 'name ' :
51- default :
52- $ result ['html ' ] = htmlspecialchars ($ p ['name ' ]);
53- break ;
54- }
5567 return $ result ;
5668 }
5769
@@ -119,4 +131,22 @@ public function getTypeId()
119131 {
120132 return 'shop.ProductInfo ' ;
121133 }
134+
135+ public function getStockInfo ($ n =0 )
136+ {
137+ $ low =5 ;
138+ $ critical =2 ;
139+
140+ if ($ n > $ low || $ n === null )
141+ $ result = '<span class="text-green"> ' ._w ("In stock " ).'</span> ' ;
142+ elseif ($ n > $ critical )
143+ $ result = '<span class="stock-low"> ' ._w ("Only %d left in stock " , "Only %d left in stock " , $ n ).'</span> ' ;
144+ elseif ($ n > 0 )
145+ $ result = '<span class="text-orange"> ' ._w ("Only %d left in stock " , "Only %d left in stock " , $ n ).'</span> ' ;
146+ else
147+ $ result = '<span class="stock-none"> ' ._w ("Out of stock " ).'</span> ' ;
148+
149+ return $ result ;
150+ }
151+
122152}
0 commit comments