2

I have a default file, where rendering price and adding some microdata, I need to add stock availbility, but I can't find a right way.

/app/design/frontend/Theme/myTheme/Magento_Catalog/templates/product/price/amount/default.phtml

 

<?php /** @var \Magento\Framework\Pricing\Render\Amount $block */ ?>

<span class="price-container <?php /* @escapeNotVerified */ echo $block->getAdjustmentCssClasses() ?>">
    <?php if ($block->getDisplayLabel()): ?>
        <span class="price-label"><?php /* @escapeNotVerified */ echo $block->getDisplayLabel(); ?></span>
    <?php endif; ?>
    <span <?php if ($block->getPriceId()): ?> id="<?php /* @escapeNotVerified */ echo $block->getPriceId() ?>"<?php endif;?>
        <?php echo($block->getPriceDisplayLabel()) ? 'data-label="' . $block->getPriceDisplayLabel() . $block->getPriceDisplayInclExclTaxes() . '"' : '' ?>
        data-price-amount="<?php /* @escapeNotVerified */ echo $block->getDisplayValue(); ?>"
        data-price-type="<?php /* @escapeNotVerified */ echo $block->getPriceType(); ?>"
        class="price-wrapper <?php /* @escapeNotVerified */ echo $block->getPriceWrapperCss(); ?>"
        <?php echo $block->getSchema() ? ' itemprop="price"' : '' ?>>
        <?php /* @escapeNotVerified */ echo $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?>
    </span>
    <?php if ($block->hasAdjustmentsHtml()): ?>
        <?php echo $block->getAdjustmentsHtml() ?>
    <?php endif; ?>
    <?php if ($block->getSchema()): ?>
        <meta itemprop="priceCurrency" content="<?php /* @escapeNotVerified */ echo $block->getDisplayCurrencyCode()?>" />
    <?php endif; ?>
</span>

If I add this part:

<?php if ($block->displayProductStockStatus()): ?>
    <?php if ($_product->isAvailable()): ?>
          <link itemprop="availability" href="http://schema.org/InStock">
    <?php else: ?>
          <link itemprop="availability" href="http://schema.org/OutOfStock">
    <?php endif; ?>
<?php endif; ?>

I get error Invalid Method: displayProductStockStatus()

Please provide me, how can I do it right. Thanks.

1

You're editing the wrong file, Do it in the default.phtml file https://github.com/magento/magento2/blob/b1e56ed3b6f13f1ab780d8eddc33fc04cb6ecf75/app/code/Magento/Catalog/view/frontend/templates/product/view/type/default.phtml First copy it to your theme, then edit it.

0

I ran into the same problem and I used the following solution with Magneto 2.3.2 in my custom theme based on the Magento blank theme:

First, I removed the schema.org markup from

/app/design/frontend/Vendor/Theme/Magento_Catalog/templates/product/price/amount/default.phtml

I added the block with schema.org/Offer markup to:

/app/design/frontend/Vendor/Theme/Magento_Catalog/Magento_Catalog/templates/product/view/attributes.phtml

My final code for the schema.org/offers markup looks like:

<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
  <meta itemprop="price" content="<?php echo $_product->getFinalPrice(); ?>" />
  <meta itemprop="priceCurrency" content="<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $currencysymbol = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
  echo $currency = $currencysymbol->getStore()->getCurrentCurrencyCode(); ?>" />
  <meta itemprop="priceCurrency" content="<?php echo $_product->getCurrencyCode() ?>" />
  <meta itemprop="url" content="<?php echo $_product->getProductUrl(); ?>">
  <?php if ($_product->isAvailable()): ?>
      <link itemprop="availability" href="http://schema.org/InStock">
  <?php else: ?>
      <link itemprop="availability" href="http://schema.org/OutOfStock">
  <?php endif; ?>
</div>

Depending on the product type you sell, you may have to extend the existing markup with custom product attributes. Here in this file I can add all other custom markups. To include these additional attributes, I use the following code (replace custom_attribute with the attribute you wish to include here):

<meta itemprop="custom_attribute" content="<?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('custom_attribute')->getFrontend()->getValue($_product); ?>" />

Example for SKU:

<meta itemprop="sku" content="<?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('sku')->getFrontend()->getValue($_product); ?>" />

You can check your final code with with the structured data testing tool from Google: https://search.google.com/structured-data/testing-tool

New contributor
Plastikschnitzer is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.