1

Here is my block code and I am new in magento

All I need is shop name and title. Basically its a magento 2 Webkul Marketplace extension which store data in marketplace_userdata table.

<?php
        /**
         * Webkul Software
         *
         * @category  Webkul
         * @package   Webkul_Mpsplitcart
         * @author    Webkul
         * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
         * @license   https://store.webkul.com/license.html
         */
        namespace Webkul\Mpsplitcart\Block;

        use Magento\Customer\Model\Session;
        use Magento\Framework\UrlInterface;
        use Magento\Sales\Model\Order;

        /**
         * Mpsplitcart Block
         */
        class Index extends \Magento\Checkout\Block\Cart
        {
            /**
             * @var \Magento\Framework\App\Http\Context
             */
            protected $httpContext;

            /**
             * @var \Magento\Checkout\Helper\Cart
             */
            protected $_cartHelper;
            /**
             * @var \Magento\Framework\ObjectManagerInterface
             */
            protected $_objectManager;

            /**
             * @var \Webkul\Mpsplitcart\Helper\Data
             */
            protected $_helper;

            /**
             * @var \Magento\Checkout\Model\Session
             */
            protected $checkoutSession;

            /**
             * @var \Magento\Checkout\Model\Cart
             */
            protected $_cartModel;

            /**
             * @var \Magento\Framework\Pricing\Helper\Data
             */
            protected $_priceHelper;

            /**
             * [__construct ]
             * @param \Magento\Framework\View\Element\Template\Context $context
             * @param \Magento\Customer\Model\Session                  $customerSession
             * @param \Magento\Checkout\Model\Session                  $checkoutSession
             * @param \Magento\Framework\ObjectManagerInterface        $objectManager
             * @param \Magento\Catalog\Model\ResourceModel\Url         $catalogUrl
             * @param \Magento\Checkout\Helper\Cart                    $cartHelper
             * @param \Webkul\Mpsplitcart\Helper\Data                  $helper
             * @param \Magento\Checkout\Model\Cart                     $cart
             * @param \Magento\Framework\Pricing\Helper\Data           $priceHelper
             * @param \Magento\Framework\App\Http\Context              $httpContext
             * @param array                                            $data
             */
            public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Customer\Model\Session $customerSession,
                \Magento\Checkout\Model\Session $checkoutSession,
                \Magento\Framework\ObjectManagerInterface $objectManager,
                \Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
                \Magento\Checkout\Helper\Cart $cartHelper,
                \Webkul\Mpsplitcart\Helper\Data $helper,
                \Magento\Checkout\Model\Cart $cart,
                \Magento\Framework\Pricing\Helper\Data $priceHelper,
                \Magento\Framework\App\Http\Context $httpContext,
                array $data = []
            ) {
                parent::__construct(
                    $context,
                    $customerSession,
                    $checkoutSession,
                    $catalogUrl,
                    $cartHelper,
                    $httpContext,
                    $data
                );
                $this->_cartHelper = $cartHelper;
                $this->_helper = $helper;
                $this->_objectManager = $objectManager;
                $this->checkoutSession = $checkoutSession;
                $this->_cartModel = $cart;
                $this->_priceHelper = $priceHelper;
            }

            /**
             * [getSellerData get seller array in order to
             * show items at shopping cart accr. to sellers]
             *
             * @return [array]
             */
            public function getSellerData()
            {

                $store = Mage::getModel('core/store')->load(10);
        echo     $name = $store->getName();
        // exit;
                // print "<pre>";
                // print_r( $this->_helper );
                // exit;


                $cart = $this->_cartModel->getQuote();
                $cartArray = [];
                foreach ($cart->getAllItems() as $item) {




                    if(!$item->hasParentItemId()){
                        $options = $item->getBuyRequest()->getData();

                        if (array_key_exists("mpassignproduct_id", $options)) {
                            $mpAssignId = $options["mpassignproduct_id"];


                            $sellerId = $this->_helper->getSellerIdFromMpassign(
                                $mpAssignId
                            );

                            // $storeName = Mage::app()->getStore()->getName();
                            echo $this->_helper->getStoreName($sellerId);





                        } else {
                            $sellerId = $this->_helper->getSellerId($item->getProductId());
                        }

                        $price =  $item->getRowTotal();

                        $formattedPrice = $this->_priceHelper->currency(
                            $price,
                            true,
                            false
                        );
                        $cartArray[$sellerId][$item->getId()] = $formattedPrice;

                        if (!isset($cartArray[$sellerId]['total'])
                            || $cartArray[$sellerId]['total']==null
                        ) {
                            $cartArray[$sellerId]['total'] = $price;
                        } else {
                            $cartArray[$sellerId]['total'] += $price;
                        }

                        $formattedPrice = $this->_priceHelper->currency(
                            $cartArray[$sellerId]['total'],
                            true,
                            false
                        );
                        $cartArray[$sellerId]['formatted_total'] = $formattedPrice;

                        $cartArray[$sellerId]['seller_name'] = "Unknow";
                    }
                }
                return $cartArray;
            }

            /**
             * [getMpsplitcartEnable get splitcart is enable or not]
             *
             * @return void
             */
            public function getMpsplitcartEnable()
            {
                return $this->_helper->checkMpsplitcartStatus();
            }

            /**
             * [getCartTotal used to get cart total]
             *
             * @return [string] [returns formatted total price]
             */
            public function getCartTotal()
            {
                $cart = $this->_cartModel->getQuote();
                $cartTotal = 0;
                foreach ($cart->getAllItems() as $item) {
                    if(!$item->hasParentItemId()){
                        $sellerId=$this->_helper->getSellerId($item->getProductId());
                        $price =  $item->getProduct()->getQuoteItemRowTotal();

                        if (!$price) {
                            $price =  $item->getBaseRowTotal();
                        }

                        $cartTotal += $price;
                    }
                }
                $formattedPrice = $this->_priceHelper->currency(
                    $cartTotal,
                    true,
                    false
                );
                $cartTotal = $formattedPrice;

                return $cartTotal;
            }

            public function getSellerInfo($storeId)
            {
                // return $this->_storeManager->getStore($storeId)->getBaseUrl();

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

                $storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');


                return $storeManager->getStore(10)->getName();

            }


        }
0

No need to call store manager class again in __construct you can reuse $context

if you call store manager class inside __construct then it will through the error at the time of compile in production mode

so better approach is reuse the $context as shown below

that is

public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                   .
                   .
                   .
                   .

            ) {
               .
               .
               .
               .
                $this->_storeManager = $context->getStoreManager();
            }
0

In blocks, storeManager property is already initialized. Whenever you need the storeManager instance, use $this->__storeManager;

And in this case, in order to get store name, use this line:

$this->_storeManager->getStore($storeId)->getName();
  • Toan I am getting error 1 exception(s): Exception #0 (Magento\Framework\Exception\NoSuchEntityException): Requested store is not found – Peter Jul 26 '17 at 9:55
0

Look at this simple class how it works http://blog.chapagain.com.np/magento-2-get-store-information-store-id-code-name-url-website-id/

<?php
namespace Chapagain\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_storeManager;    

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Store\Model\StoreManagerInterface $storeManager,        
        array $data = []
    )
    {        
        $this->_storeManager = $storeManager;        
        parent::__construct($context, $data);
    }

    /**
     * Get store identifier
     *
     * @return  int
     */
    public function getStoreId()
    {
        return $this->_storeManager->getStore()->getId();
    }

    /**
     * Get website identifier
     *
     * @return string|int|null
     */
    public function getWebsiteId()
    {
        return $this->_storeManager->getStore()->getWebsiteId();
    }

    /**
     * Get Store code
     *
     * @return string
     */
    public function getStoreCode()
    {
        return $this->_storeManager->getStore()->getCode();
    }

    /**
     * Get Store name
     *
     * @return string
     */
    public function getStoreName()
    {
        return $this->_storeManager->getStore()->getName();
    }

    /**
     * Get current url for store
     *
     * @param bool|string $fromStore Include/Exclude from_store parameter from URL
     * @return string     
     */
    public function getStoreUrl($fromStore = true)
    {
        return $this->_storeManager->getStore()->getCurrentUrl($fromStore);
    }

    /**
     * Check if store is active
     *
     * @return boolean
     */
    public function isStoreActive()
    {
        return $this->_storeManager->getStore()->isActive();
    }
}
?>
  • I only need Seller Profile Information. – Peter Jul 26 '17 at 10:11
0

Use below code to get store name in magento 2. Also follow this blog to get your answer. http://www.amitbera.com/magento2-get-base-url-and-media-url-and-static-url/

use Magento\Store\Model\Store;

protected $_storeManager;

public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager  
    ) {        
      $this->_storeManager = $storeManager;
}

public function getStoreName()
{
    return $this->_storeManager->getStore()->getName();
}

echo $this->getStoreName();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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