You can use this,
namespace NameSpace\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
protected $_productRepository;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ProductRepository $productRepository,
array $data = []
)
{
$this->_productRepository = $productRepository;
parent::__construct($context, $data);
}
public function getProductById($id)
{
return $this->_productRepository->getById($id);
}
public function getProductBySku($sku)
{
return $this->_productRepository->get($sku);
}
}
and in your phtml.
$id = YOUR_PRODUCT_ID;
$product = $block->getProductById($id); // load product bi ID
$attributes = $product->getAttributes();
foreach ($attributes as $attribute) {
echo $attribute->getAttributeCode(); echo '<br />';
echo $attribute->getStoreLabel(); echo '<br />';
echo $attribute->getFrontendLabel(); echo '<br />';
echo $attribute->getFrontend()->getLabel(); echo '<br />';
// you might not get value for all attributes
echo $attribute->getFrontend()->getValue($product); echo '<br />';
}