如何在缺货商品时将添加购物车按钮文本更改为缺货文本?

时间:2019-10-03 11:04:18

标签: magento2

如何在缺货商品时将添加购物车按钮文本更改为缺货文本?

我已经尝试过了,但是没有用

if($_product->getStockItem()->getBackorders()!=0){
$buttonTitle=$this->__('Backorder');
}

1 个答案:

答案 0 :(得分:0)

<?php

$obj = \Magento\Framework\App\ObjectManager::getInstance();  
$stockRegistry = $obj->get('Magento\CatalogInventory\Api\StockRegistryInterface');
$stockitem = $stockRegistry->getStockItem($_product->getId(),$_product->getStore()->getWebsiteId());

  if($stockitem->getBackorders() == 1):
    $buttonTitle = __('Backorders');
  else:
    $buttonTitle = __('Add to Cart');
  endif;

?>