How to get website collection programmatically in Magento 2?

Magento 2 is robust, scalable and strong functional e-commerce software used for online shopping store. Its support Multi website, multi-language,multi-store view for an online store. Many times in custom functionality for Multi website store need to get a collection of all the website information. In this case below code is useful.

Here We will go for how to get all website collection of the system by programmatically.

Create Block file,

<?php
namespace Rbj\Website\Block;

class WebsiteList extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\ResourceModel\Website\CollectionFactory $websiteCollectionFactory,
        array $data = []
    ) {
        $this->_websiteCollectionFactory = $websiteCollectionFactory;
        parent::__construct($context, $data);
    }

     /**
     * Retrieve websites collection of system
     *
     * @return Website Collection
     */
    public function getWebsiteCollection()
    {
        $collection = $this->_websiteCollectionFactory->create();
        return $collection;
    }

Call from template file,

<?php
foreach($block->getWebsiteCollection() as $website) {
	echo $website->getCode(); //website code
	echo $website->getName(); //website name
	echo "<pre>";print_r($website->getData());
}

To get all the data output look like,

Array
(
    [website_id] => 1
    [code] => base
    [name] => Main Website
    [sort_order] => 0
    [default_group_id] => 1
    [is_default] => 1
)

 

Convert number into currency format using Magento 2.

We can simply convert a plain number into price format with a currency symbol. We can get round price of current given price as well as get current store currency symbol by Magento\Framework\Pricing\PriceCurrencyInterface class interface. We can Get Formatted Price With Currency using below code snippets.

Using Objectmanager,

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$priceHelper = $objectManager->create('Magento\Framework\Pricing\PriceCurrencyInterface'); // Pricing Interface
$price =  50; //Your Custom Price
echo $formattedPrice = $priceHelper->convertAndFormat($price); // $50.00
echo $formattedPrice = $priceHelper->round($price); //50
echo $formattedPrice = $priceHelper->getCurrencySymbol(); // $
?>

Proper way using Block PHP file and call function in a template file, Create Block file in your module and pass Dependency as Pricecurrencyinterface

<?php
namespace Rbj\Currency\Block;

class Currency extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        array $data = []
    ) {
        $this->priceCurrency = $priceCurrency;
        parent::__construct($context, $data);
    }

    /**
     * Get current store currency symbol with price
     * $price price value
     * true includeContainer
     * Precision value 2
     */
    public function getCurrencyFormat($price)
    {
        $price = $this->priceCurrency->format($price,true,2);
        return $price;
    }
    /**
     * Get round price without currency symbol
     */
    public function getRoundPrice($price)
    {
        $price = $this->priceCurrency->round($price);
        return $price;
    }
    /**
     * Get current store CurrencySymbol
     */
    public function getCurrencySymbol()
    {
        $symbol = $this->priceCurrency->getCurrencySymbol();
        return $symbol;
    }

Call function from template file,

<?php
// Get currency symbol with price format
echo $currencyFormat = $this->getCurrencyFormat(50.0000); //output $50.00
// Get round value of number
echo $roundPrice = $this->getRoundPrice(20.0000); //output 20 (without currency symbol)
echo $roundPrice = $this->getRoundPrice(20.5000); //output 20.5
// Get Currency symbol of current store
echo $roundPrice = $this->getCurrencySymbol(); //output $