Category Archives: Magento 2

How To Add Or Create Magento 2 Virtual Product Programmatically

How can I add or create a virtual product for my Magento 2 store programmatically

You need to use Magento\Catalog\Api\Data\ProductInterfaceFactory and \Magento\Catalog\Api\ProductRepositoryInterface  to create a virtual product programmatically.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productFactory = $objectManager->get('Magento\Catalog\Api\Data\ProductInterfaceFactory');

$productRepository = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface');

$_product = $productFactory->create();

$_product->setSku('product');

$_product->setName('Product');

$_product->setDescription('Product Description');

$_product->setShortDescription('Product Short Description');

$_product->setWebsiteIds([1]);

$_product->setCategoryIds(["1","2"]);

$_product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL);

$_product->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);

$_product->setPrice(array(1));

$_product->setAttributeSetId(4);

$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);

$product->setUrlKey("test");


$_product->setStockData(array(
        'use_config_manage_stock' => 0, //'Use config settings' checkbox
        'manage_stock' => 1, //manage stock
        'min_sale_qty' => 1, //Minimum Qty Allowed in Shopping Cart
        'max_sale_qty' => 2, //Maximum Qty Allowed in Shopping Cart
        'is_in_stock' => 1, //Stock Availability
        'qty' => 100 //qty
        )
    );

$product = $productRepository->save($product);

$imagePath = '/product/product.jpg'; 

$product->addImageToMediaGallery($imagePath, ['image', 'small_image', 'thumbnail'], false, false);

$product->save();

How To Create Credit Memo Programmatically in Magento 2

If would like to make a refund, how can I Create Credit Memo Programmatically in Magento 2?
We will need to get invoice and order object first , then use class – CreditmemoService to create credit memo.
$order = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Sales\Model\Order');
$creditmemoFactory = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Sales\Model\Order\CreditmemoFactory');
$invoice = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Sales\Model\Order\Invoice');
$creditmemoService = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Sales\Model\Service\CreditmemoService');

$order->load(1);
$invoices = $order->getInvoiceCollection();
foreach ($invoices as $invoice) {
    $invoiceincrementid = $invoice->getIncrementId();
}
$invoicedata = $invoice->loadByIncrementId($invoiceincrementid);
$creditmemo = $creditmemoFactory->createByOrder($order);
$creditmemo->setInvoice($invoicedata );
$creditmemoService->refund($creditmemo); 
 

How To Get Current Magento 2 Currency Symbol Programmatically

How can I get current currency symbol from Mangeto 2 template file programmatically.
You can use storeManager to get current currency code and retrieve the currency object. Then use method – getCurrencySymbol to get currency symbol.

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterfa‌​ce'); 
$code = $storeManager->getStore()->getCurrentCurrencyCode(); 
$currency = $objectManager->create('\Magento\Directory\Model\CurrencyFact‌​ory')->create()->loa‌​d($code); 
$symbol = $currency->getCurrencySymbol(); 

How To Get Magento 2 Block Content By Id

I am looking for insert a Magento 2 block into my theme and would like to load block by id and print the content from block file.

You can use class – “Magento\Cms\Model\Block” to load Magento 2 block by id . Here is the example

$ob = \Magento\Framework\App\ObjectManager::getInstance();
$block = $ob->create('Magento\Cms\Model\Block');
$block->load('block-id', 'identifier');
echo $block->getContent();

How To Add Comments Programmatically For Magento 2 Order

I am writing a small extension, if have an order id, how to add comments to the Magento 2 order programmtically
You need to load Magento 2 order by order id first and then call method – addStatusHistoryComment to assign the comments to the Magento 2 order.
$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
$order->addStatusHistoryComment('Test test');
$order->save();

Magento 2: How To Get Current Currency Code

I have Magento 2 store and would like to get current currency code. I knew I can use method “getCurrentCurrencyCode” to get current currency code from Magento but how can I get from Magento 2

You call method “getCurrentCurrency” from storeManager model to get current currency code.

How To Get Child Product Of Magento 2 Configurable Product

How can I get child products of Magento 2 configurable product

You can use “getUsedProducts” method to get all child products from a given Magento 2 configurable product

How To Get Magento 2 Current Customer Group ID

How to get current customer group ID for my Magento 2 store

You can use “customerSession” to check if the customer logged in or not and get current Magento 2 customer group ID

Magento 2 : How To Get Current URL From PHTML

How can I get current URL from Magento 2 theme file

You can use getUrl method to get current URL from your Magento 2 theme file. You can use “_use_rewrite” parameter to get rewrite URL or none search engine friendly URL