Guest User

Untitled

a guest
Mar 11th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. // *************************************************************
  2. // Function Name: get_custom_product_data
  3. // Summary: Pass SCF values to RankMath Scheme
  4. // *************************************************************
  5. add_filter('rank_math/snippet/rich_snippet_product_entity', function($entity) {
  6. if (!is_singular('inventory')) {
  7. return $entity;
  8. }
  9.  
  10. $product_id = get_queried_object_id();
  11. if (!$product_id) {
  12. return $entity;
  13. }
  14.  
  15. $product_info = get_custom_product_data($product_id);
  16.  
  17. if (!isset($entity['offers'])) {
  18. $entity['offers'] = [];
  19. }
  20.  
  21. if ($product_info['price'] !== null) {
  22. $entity['offers']['price'] = number_format($product_info['price'], 2);
  23. $entity['offers']['priceCurrency'] = 'JPY';
  24. }
  25.  
  26. $entity['offers']['availability'] = 'https://schema.org/' . $product_info['availability'];
  27.  
  28. return $entity;
  29. }, 10, 1);
  30.  
  31. function get_custom_product_data($product_id) {
  32. if (!$product_id) {
  33. return [
  34. 'price' => null,
  35. 'availability' => 'OutOfStock'
  36. ];
  37. }
  38.  
  39. // Get inventory status
  40. $inventory_situation = SCF::get('inventory_situation', $product_id);
  41. if (!$inventory_situation) {
  42. $inventory_situation = 'sold'; // Set default value
  43. }
  44.  
  45. // Get price
  46. $inventory_price = SCF::get('inventory_price', $product_id);
  47.  
  48. // Set price based on inventory status
  49. if ($inventory_situation === 'normal') {
  50. $price = $inventory_price ? floatval($inventory_price) : null;
  51. } else {
  52. $price = null;
  53. }
  54.  
  55. return [
  56. 'price' => $price,
  57. 'availability' => $inventory_situation === 'normal' ? 'InStock' : 'OutOfStock'
  58. ];
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment