magento2-composer-bundled
This is a great fork of Magento 2 from Mageinferno. It includes all composer packages (vendor folder), and you can get in on Github:
More tips from Magento 2 Developer’s Cookbook Continue Reading
This is a great fork of Magento 2 from Mageinferno. It includes all composer packages (vendor folder), and you can get in on Github:
More tips from Magento 2 Developer’s Cookbook Continue Reading
If the simple and configurable products are not assigned together like in the folowing example, there is a reliable solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
//simple product $simple_product = $this->_objectManager->create('\Magento\Catalog\Model\Product'); $simple_product->setSku('test-simple'); $simple_product->setName('test name simple'); $simple_product->setAttributeSetId(4); $simple_product->setSize_general(193); // value id of S size $simple_product->setStatus(1); $simple_product->setTypeId('simple'); $simple_product->setPrice(10); $simple_product->setWebsiteIds(array(1)); $simple_product->setCategoryIds(array(31)); $simple_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 ) ); $simple_product->save(); $simple_product_id = $simple_product->getId(); echo "simple product id: ".$simple_product_id."\n"; //configurable product $configurable_product = $this->_objectManager->create('\Magento\Catalog\Model\Product'); $configurable_product->setSku('test-configurable'); $configurable_product->setName('test name configurable'); $configurable_product->setAttributeSetId(4); $configurable_product->setStatus(1); $configurable_product->setTypeId('configurable'); $configurable_product->setPrice(11); $configurable_product->setWebsiteIds(array(1)); $configurable_product->setCategoryIds(array(31)); $configurable_product->setStockData(array( 'use_config_manage_stock' => 0, //'Use config settings' checkbox 'manage_stock' => 1, //manage stock 'is_in_stock' => 1, //Stock Availability ) ); $configurable_product->getTypeInstance()->setUsedProductAttributeIds(array(152),$configurable_product); //attribute ID of attribute 'size_general' in my store $configurableAttributesData = $configurable_product->getTypeInstance()->getConfigurableAttributesAsArray($configurable_product); $configurable_product->setCanSaveConfigurableAttributes(true); $configurable_product->setConfigurableAttributesData($configurableAttributesData); $configurableProductsData = array(); $configurableProductsData[$simple_product_id] = array( //[$simple_product_id] = id of a simple product associated with this configurable '0' => array( 'label' => 'S', //attribute label 'attribute_id' => '152', //attribute ID of attribute 'size_general' in my store 'value_index' => '193', //value of 'S' index of the attribute 'size_general' 'is_percent' => 0, 'pricing_value' => '10', ) ); $configurable_product->setConfigurableProductsData($configurableProductsData); $configurable_product->save(); echo "configurable product id: ".$configurable_product->getId()."\n"; |
You can review on API functional test aimed at
1 2 3 4 5 6 |
$product = $productFactory->create(['name'=> 'configurable product', ... ]); $configurableOption = $optionFactory->create([]); $linkedProduct = $linkFactory->create([]); $product->getExtensionAttributes()->setConfigurableProductOptions($configurableOption)l $product->getExtensionAttributes()->setConfigurableProductLinks($linkedProduct); $productRepository->save($product) |
It is also necessary to mention that the API does not generate simple product, so there is the need to create in advance.
You call theme images from static block as follows: magento2 : {{view url=”images/demo.jpg”}}
To override a base view file in Magento 2, create a file with the following name: app/design/frontend/[VendorName]/[theme]/Magento_Tax/templates/pricing/adjustment.phtml.
base folder is a default fallback for all arias used to share resources across areas.
To add a custom block to specific product type view pages, use product type dependent layout updates catalog_product_view_type_{prodyct_type}.xml (catalog_product_view_type_ grouped.xml). For instance:
1 |
<?xml version="1.0"?> <layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/layout_generic.xsd"> <referenceContainer name="content"> <block class="..." /> </referenceContainer> </layout> |
Since there is no ‘core’ module in Magento 2, you can do this by following way inside view file (.phtml):
1 |
$this->helper('Magento\Framework\Pricing\Helper\Data')->currency(number_format(50,2),true,false); |
You can put a search box after the navigation block and use css to make display it in one line, since html/topmenu.phtml block doesn’t print its child’s
1 |
<move element="top.search" destination="page.top" after="catalog.topnav" /> |
Alternatively, you can overwrite html/topmenu.phtml to
1 |
<?php $columnsLimit = $block->getColumnsLimit() ?: 0; ?> <?php $_menu = $block->getHtml('level-top', 'submenu', $columnsLimit) ?> <nav role="navigation"> <ul data-mage-init='{"menu":{"responsive":true, "expanded":true, "position":{"my":"left top","at":"left bottom"}}}'> <?php echo $_menu; ?> </ul> <?php echo $block->getChildHtml() ?> </nav> |
Probably, you have a problem related to
Because of Dependency Injection pattern Magento 2 have huge constructors for every class. World best PHP IDE PHP Storm can help you generate constructors and save lots of time in Magento 2 development.
With constructor generator you will be able to create constructors with arguments, with the value assigned to the field variables. Continue Reading
To get a URL from your Magento root directory, use getUrl. Since it inherits from the AbstractBlock class (Magento\Framework\View\Element\AbstractBlock), you are able to use it with any of your blocks. Check the below example:
1 |
$this->getUrl('pub/media/video/', ['_secure' => $this->getRequest()->isSecure()]).$fileName |
The first parameter is the path you need, while the second one sets the _secure option if the user is browsing over https. You can add to the path by concatenating a specific filename onto the getUrl call or you could add it to the first parameter as well. Please note that the path is relative to the root directory of your Magento install.