How to get product collection by product id in magento 2?

You can get product data by product id in Magento 2 by below code snippets,

<?php

namespace Rbj\Training\Block;

class Product extends \Magento\Framework\View\Element\Template
{
    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        array $data = []
    ) {
        $this->productRepository = $productRepository;
        parent::__construct($context, $data);
    }

    /**
    * Get Product by Id
    * @param int
    * @return \Magento\Catalog\Model\Product $product
    */
    public function getProduct($id)
    {
        return $this->productRepository->getById($id);
    }
}

Call inside template file by below code,

$product_id = 1;
$product = $block->getProduct($product_id);
echo $product->getName(); // product name
echo $product->getSku(); // product sku

How to Create Customer Attribute Programmatically in Magento 2?

Create Magento Customer Attribute Boolean type Programmatically with the help of the Data patch feature.

Let’s create a customer attribute called ‘email_marketing‘ with the type boolean using programmatically and the bool type has only two possible values Yes/No.

You need to create a basic Magento module to create custom attributes for the customer.

We are going with all the required steps to finalize your customer attribute readymade once you follow all the steps given.

Create a registration.php file to register our module. File Path, app/code/Rbj/CustomerAttribute/registration.php

<?php
use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Rbj_EmailMarketing',
    __DIR__
);

Create a module.xml file to define our module. Continue reading “How to Create Customer Attribute Programmatically in Magento 2?”

How to get order item collection by item id magento 2?

You can get item collection data by Item id in Magento 2 by using below code snippet, Create Block file,

<?php
namespace Rbj\Training\Block;

class Item extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template $context,
        \Magento\Sales\Api\OrderItemRepositoryInterface $orderItemRepository,
        array $data = []
    ) {
        $this->orderItemRepository = $orderItemRepository;
        parent::__construct($context, $data);
    }

    /* get order Item collection */
    public function getOrderItem($itemIid)
    {
        $itemCollection = $this->orderItemRepository->get($itemId);
        return $itemCollection;
    }
}

Call function from the Template file,

$itemId = 10; // order item id
$getItemCollection = $block->getOrderItem($itemId);
echo $getItemCollection->getOrderId();
echo "<pre>";print_r($getItemCollection->debug());