\Magento\Store\Model\StoreManagerInterface $storeManage
$this->storeManager = $storeManager;
$id = $this->storeManager->getStore()->getId();
$baseUrl = $this->storeManager->getStore()->getBaseUrl();
NOTE: Use the constants in \Magento\Framework\UrlInterface
, as parameters (UrlInterface::URL_TYPE_LINK)
instead of 'link'
strings.
Constance in Magento\Framework\UrlInterface
const URL_TYPE_LINK = 'link';
const URL_TYPE_DIRECT_LINK = 'direct_link';
const URL_TYPE_WEB = 'web';
const URL_TYPE_MEDIA = 'media';
const URL_TYPE_STATIC = 'static';
const URL_TYPE_JS = 'js';
More details:
NOTE: To Inject the UrlInterface
within a _controller- that is extending:
Magento\Framework\App\Action\Action
using the dependency injection
/**
* Inject via DI
*
* @param Context $context
* @param UrlInterface $urlBuilder
*/
public function __construct(
Context $context,
UrlInterface $urlBuilder
) {
parent::__construct($context);
$this->urlBuilder = $urlBuilder;
}
$baseCurrency = $this->storeManager->getStore()->getBaseCurrencyCode();
$currentCurrency = $this->storeManager->getStore()->getCurrentCurrencyCode();
$baseMediaDir = $this->storeManager->getStore()->getBaseMediaDir();
$baseStaticDir = $this->storeManager->getStore()->getBaseStaticDir();
Magento\Checkout\Model\Cart $cart
$this->cart = $cart;
$cartCount = $this->cart->getItemsCount();
$cartQuote= $this->cart->getQuote()->getData();
\Zend_Debug::dump($cartCount);
echo $cartQuote['base_grand_total'];
Magento\Checkout\Model\Session $session
$this->session = $session;
foreach ($this->session->getQuote()->getAllItems() as $item) {
echo $item->getName();
}
$this->session ->clearQuote();
\Magento\Framework\Registry $registry
$this->registry = $registry;
$category = $this->registry->registry('current_category');
$cid = $this->registry->registry('current_category')->getId();
$level = $this->registry->registry('current_category')->getLevel();
$pid = $this->registry->registry('current_product');
Related products for PDP
$products = [];
foreach ($this->registry->registry('current_product')->getCrossSellProducts() as $product) {
$products[$product->getId()] = ['position' => $product->getPosition()];
}
\Zend_debug::dump($products);
$products = [];
foreach ($this->registry->registry('current_product')->getRelatedProducts() as $product) {
$products[$product->getId()] = ['position' => $product->getPosition()];
}
\Zend_debug::dump($products);
$products = [];
foreach ($this->registry->registry('current_product')->getUpSellProducts() as $product) {
$products[$product->getId()] = ['position' => $product->getPosition()];
}
\Zend_debug::dump($products);
if ($this->registry->registry('current_product')) {
$product = $this->registry->registry('current_product');
} else {
throw new \LogicException('Product is not defined');
}
\Magento\Framework\Registry $registry
$this->registry = $registry;
$page = $this->registry->registry('cms_page');
$cmsId = $this->registry->registry('cms_page')->getId();
if ($this->registry->registry('cms_page')->getId()) {
return $this->registry>registry('cms_page')->getTitle();
} else {
return __('New Page');
}
Within any Block
that extends \Magento\Framework\View\Element\Template
,
$loader = $this->getViewFileUrl("ibnab_lazy::images/logo.svg");
$js = $this->getViewFileUrl("js/yetanotherdamnJSlib.js");
\Magento\Framework\View\LayoutFactory $layoutFactory
$this->_layoutFactory->createBlock('\Vendor\Module\Block\Foo', 'custom.block.foo.bar', ['data' => ['title' => "Bar"]]);`
<block class="Vendor\Module\Block\Foo" name="custom.block.foo.bar">
<arguments>
<argument translate="true" name="title" xsi:type="string">Bar</argument>
</arguments>
</block>
Magento\Framework\View\Layout\Data\Structure
setChild($parentName, $elementName, $alias)
ex:
$this->structure->setAsChild('custom.block', 'product.info.details') ;
Which will allow for calling of a child of the parent inside of a .phtml file, foo.phtml
for example.
\Zend_Debug::dump(getBlock($name));
\Zend_Debug::dump(getXml());
addContainer($name, $label, array $options = [], $parent = '', $alias = '');
Including the type of element, block, or container, and class layout.
For example to determine IF an element is a container or block:
\Zend_Debug::dump($this->structure->isContainer($name));
\Zend_Debug::dump($this->structure->isBlock($name))";
Frontend Magento2.x useful code snippets:
Original sauce, Ibnab
With minor changes & additions of my own.