Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // *************************************************************
- // Function Name: get_custom_product_data
- // Summary: Pass SCF values to RankMath Scheme
- // *************************************************************
- add_filter('rank_math/snippet/rich_snippet_product_entity', function($entity) {
- if (!is_singular('inventory')) {
- return $entity;
- }
- $product_id = get_queried_object_id();
- if (!$product_id) {
- return $entity;
- }
- $product_info = get_custom_product_data($product_id);
- if (!isset($entity['offers'])) {
- $entity['offers'] = [];
- }
- if ($product_info['price'] !== null) {
- $entity['offers']['price'] = number_format($product_info['price'], 2);
- $entity['offers']['priceCurrency'] = 'JPY';
- }
- $entity['offers']['availability'] = 'https://schema.org/' . $product_info['availability'];
- return $entity;
- }, 10, 1);
- function get_custom_product_data($product_id) {
- if (!$product_id) {
- return [
- 'price' => null,
- 'availability' => 'OutOfStock'
- ];
- }
- // Get inventory status
- $inventory_situation = SCF::get('inventory_situation', $product_id);
- if (!$inventory_situation) {
- $inventory_situation = 'sold'; // Set default value
- }
- // Get price
- $inventory_price = SCF::get('inventory_price', $product_id);
- // Set price based on inventory status
- if ($inventory_situation === 'normal') {
- $price = $inventory_price ? floatval($inventory_price) : null;
- } else {
- $price = null;
- }
- return [
- 'price' => $price,
- 'availability' => $inventory_situation === 'normal' ? 'InStock' : 'OutOfStock'
- ];
- }
Advertisement
Add Comment
Please, Sign In to add comment