0

I want to get the Product attribute using Object manager and check the availability of the product if its RTS I have to assign that product to a category id.

2
  • object manager is not best practice for Magento. Same thing you can get without object manager as well. Nov 23, 2021 at 12:34
  • Agreed but for testing purpose i am using object manager Nov 23, 2021 at 12:38

1 Answer 1

0

Product Attribute

You can get product attribute with below code -

$product = $this->getProduct();
$ressource = $product->getResource();
$store = $this->_storeManager->getStore();

$ressource->getAttributeRawValue($product->getId(),'attribute_code',$store->getId());

Product Availability

public function __construct(
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry
) {
    $this->stockRegistry = $stockRegistry;
}
// you can pass $product object to the function
public function getStockStatus($product)
{
    return $this->stockRegistry->getStockItem($product->getId());
}

You can get all the stock details by

$stockItem = $this->stockRegistry->getStockItem($product->getId());
$stockData = $stockItem->getData();
$stockItem->getIsInStock();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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