Problem/Motivation

The Commerce Product Availability module exposes availability information for Drupal Commerce products and variations.
However, when site builders want to output this information in Schema.org JSON‑LD Offer markup, there is currently no token support for:

  • availability
  • deliveryLeadTime
  • shippingDetails

This prevents easy integration with JSON‑LD generators (e.g., Metatag, custom preprocessors, or theme templates) and requires custom code to expose these values.

Providing tokens would allow site builders to output Offer‑related structured data without writing custom PHP.

Steps to reproduce

Need these tokens for Metatags

Proposed resolution

Add a Token type for Commerce Product Availability and expose the following tokens:
Token Description Example

  • [commerce_product:current_variation:commerce_product_availability:schemaorg_availability] Returns the Schema.org availability URL https://schema.org/InStock
  • [commerce_product:current_variation:commerce_product_availability:schemaorg_delivery_lead_time] Returns ISO 8601 duration or textual lead time P3D
  • [[commerce_product:current_variation:commerce_product_availability:schemaorg_shipping_details] Returns shippingDetails JSON fragment or structured array { "shippingRate": ... }
  1. Add token definitions for the Offer properties.
  2. Map the module’s internal availability data to Schema.org URLs (e.g., InStock, OutOfStock, PreOrder).
  3. Provide values for delivery lead time and shipping details if configured.
  4. Add automated tests for token output.

Remaining tasks

User interface changes

API changes

Data model changes

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

anybody created an issue. See original summary.

anybody’s picture

Attached you can find some crappy AI code which uses non-existing classes, but it should show the expected token outcome. Unsure what is usable and what is not. Simply should not be used for anything than demonstrating the idea behind ;)

anybody’s picture

Issue summary: View changes
anybody’s picture

Status: Active » Needs work
anybody’s picture

Issue summary: View changes
anybody’s picture

Assigned: Unassigned » grevil

Let's do a quick fresh implementation by time :)

grevil made their first commit to this issue’s fork.

grevil’s picture

@anybody, there is no "TokenTypeInterface" can we not simply define the tokens like in the already existing module file "CommerceProductAvailabilityTokenHooks"?

anybody’s picture

I know, see #3. It's just about the outcome.

grevil’s picture

Issue summary: View changes

grevil’s picture

Alright, I still need to properly test this.
Especially the

    $variation = NULL;
    if (!empty($data['commerce_product_variation'])) {
      $variation = $data['commerce_product_variation'];
    }
    elseif (!empty($data['commerce_product'])) {
      $variation = $data['commerce_product']->getDefaultVariation();
    }

part. Let me go through this with xdebug tommorow.

anybody’s picture

Yes this is indeed wrong. Instead of providing a general global token, this token should only be available on commerce_product_variation entities!

So I guess will be something like (pseudocode)

    return [
      'types' => ['commerce_product_variation']['commerce_product_availability'] = $type,
      'tokens' => ['commerce_product_variation']['commerce_product_availability'] = $tokens,
    ];

Guess there are existing examples how to add further tokens on the entity level. Then we'll also always have the $variation given, no need for a fallback.

anybody’s picture

Issue summary: View changes

PS: The final token will then look like this:
[commerce_product:current_variation:commerce_product_availability:schemaorg_availability] (for a product - most typical)
[commerce_product_variation:commerce_product_availability:schemaorg_availability] (for a product variation - less typical but one step shorter)

Logic is always the same: The tokens act on the commerce_product_variation entity level!

Sorry my requirement specification was too general and buggy. Shit in, shit out... Updated it accordingly.

grevil’s picture

Assigned: grevil » anybody
Status: Needs work » Needs review

Please review, the implementation with the computed property works even better!

anybody’s picture

Assigned: anybody » Unassigned
Status: Needs review » Reviewed & tested by the community

LGTM, thx!!

  • anybody committed 17d56918 on 1.x authored by grevil
    feat: #3591534 Provide tokens for Schema.org JSON‑LD Offer properties (...
anybody’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

anybody’s picture

Works like a charm! :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.