Add Custom Category Attribute Magento2 Extension Free
USER GUIDE
- How to Register Module in Magento 2
- Tell Magento Which Extesions to Be Included
- Register Attribute in our Core
- Add Code for showing Attribute to set value from Admin Dashboard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php /* * Company Name: Cozmot Inc * Website: https://cozmot.com * Writer Name: Zaheer Ahmed * Contact: [email protected] * Number: +92-301-1000201 * Copyright: MIT Licence(Open to edit) */ ?> <?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Cozmot_CategoryAttribute', __DIR__ ); |
After Registering Tell Magento to Load Our Extension
1 2 3 4 5 6 7 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Cozmot_CategoryAttribute" setup_version="1.0.0"> </module> </config> |
Not it’s time to Implement Logic, Here are we Registering our Attribute for Category which will be Boolean Formed.
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 |
<?php /* * Company Name: Cozmot Inc * Website: https://cozmot.com * Writer Name: Zaheer Ahmed * Contact: [email protected] * Number: +92-301-1000201 * Copyright: MIT Licence(Open to edit) */ ?> <?php namespace Cozmot\CategoryAttribute\Setup; use Magento\Framework\Setup\{ ModuleContextInterface, ModuleDataSetupInterface, InstallDataInterface }; use Magento\Eav\Setup\EavSetup; use Magento\Eav\Setup\EavSetupFactory; class InstallData implements InstallDataInterface { private $eavSetupFactory; public function __construct(EavSetupFactory $eavSetupFactory) { $this->eavSetupFactory = $eavSetupFactory; } public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) { $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); $eavSetup->addAttribute(\Magento\Catalog\Model\Category::ENTITY, 'cozmotmobilemenu', [ 'type' => 'int', 'label' => 'Cozmot Category Attribute', 'input' => 'boolean', 'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean', 'visible' => true, 'default' => '0', 'required' => false, 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE, 'group' => 'Display Settings', ]); } } |
Now we have to add Admin side option in Category options to set value of our Attribute.
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 |
<!-- * Company Name: Cozmot Inc * Website: https://cozmot.com * Writer Name: Zaheer Ahmed * Contact: zaheercena@gmail.com * Number: +92-301-1000201 * Copyright: MIT Licence(Open to edit) --> <?xml version="1.0"?> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <fieldset name="display_settings"> <field name="cozmotmobilemenu"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="dataType" xsi:type="string">boolean</item> <item name="formElement" xsi:type="string">checkbox</item> <item name="label" xsi:type="string" translate="true">Cozmot Category Attribute</item> <item name="prefer" xsi:type="string">toggle</item> <item name="valueMap" xsi:type="array"> <item name="true" xsi:type="string">1</item> <item name="false" xsi:type="string">0</item> </item> <item name="default" xsi:type="number">0</item> </item> </argument> </field> </fieldset> </form> |
Hurrayyyy… That’s all needed Now run Following commands according to your Installation.
1 2 3 4 |
php bin/magento setup:upgrade php bin/magento c:c |
]]>
How to Create Barcode in Magento 2 Invoice PDF
Magento 2 facilitates the merchants to provide multiple things in invoice PDFs like order id, customer name, shipping address, product name, product quantity, product price, total amount, and so on.
However, depending on the business requirements, customization in invoice PDF can be done.
One such customization solution is offered in this post to create barcode and add it in Magento 2 invoice PDF.
Barcode in invoice PDF helps the customer to scan and get the order id using which it is easy to get order details.
Steps to Create Barcode and Add it in Magento 2 PDF Invoice
1. Create di.xml file at Vendor\Extension\etc
1 2 3 4 5 6 7 8 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Sales\Model\Order\Pdf\Invoice"> <plugin name="barcodes" type="Vendor\Extension\Plugin\Invoice" sortOrder="10"/> </type> </config> |
2. Create Invoice.php file in Vendor\Extension\Plugin\Invoice.php
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 |
namespace Vendor\Extension\Plugin; use Magento\Framework\App\Config\ScopeConfigInterface; use \Zend\Barcode\Barcode; class Invoice { const XML_PATH_BARCODES_ENABLED = 'barcodes/general/eb_barcodes_active'; private $scopeConfig; public function __construct(ScopeConfigInterface $scopeConfig) { $this->scopeConfig = $scopeConfig; } public function beforeInsertDocumentNumber($subject, $page, $text) { $config = new \Zend_Config([ 'barcode' => 'code128', 'barcodeParams' => [ 'text' => $this->getInvoiceNumber($text), 'drawText' => true ], 'renderer' => 'image', 'rendererParams' => ['imageType' => 'png'] ]); $barcodeResource = Barcode::factory($config)->draw(); ob_start(); imagepng($barcodeResource); $barcodeImage = ob_get_clean(); $image = new \Zend_Pdf_Resource_Image_Png('data:image/png;base64,' . base64_encode($barcodeImage)); if ($image) { $docHeader = $subject->getDocHeaderCoordinates(); $page->drawImage($image, $docHeader[2] - 130, $docHeader[1] + 2, $docHeader[2] + 8, $docHeader[1] + 35); } } protected function getInvoiceNumber($text) { $array_of_words = explode("#", $text); return $array_of_words[1]; } } |
Done! That’s what you need to do.
For Details Visit Cozmot.com
What's New in PHP 8.0 Release a Major Update of the PHP language
Named arguments
- Specify only required parameters, skipping optional ones.
- Arguments are order-independent and self-documented.
Attributes
Instead of PHPDoc annotations, you can now use structured metadata with PHP’s native syntax.
Constructor property promotion
Less boilerplate code to define and initialize properties.
Union types
Instead of PHPDoc annotations for a combination of types, you can use native union type declarations that are validated at runtime.
Match expression
The new match is similar to switch and has the following features:- Match is an expression, meaning its result can be stored in a variable or returned.
- Match branches only support single-line expressions and do not need a break; statement.
- Match does strict comparisons.
Nullsafe operator
Instead of null check conditions, you can now use a chain of calls with the new nullsafe operator. When the evaluation of one element in the chain fails, the execution of the entire chain aborts and the entire chain evaluates to null.Saner string to number comparisons
When comparing to a numeric string, PHP 8 uses a number comparison. Otherwise, it converts the number to a string and uses a string comparison.Consistent type errors for internal functions
Most of the internal functions now throw an Error exception if the validation of the parameters fails.
]]>How to get Product Grid by Category ID Magento2
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 65 |
<?php /* * Company Name: Cozmot Inc * Website: https://cozmot.com * Writer Name: Zaheer Ahmed * Contact: [email protected] * Number: +92-301-1000201 */ $_category = $block->getCategory(); //or any ID like 2 or 3 if you already have ?> <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categorysFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); ?> <?php foreach($_category as $category): ?> <?php $cat = $category->getID(); ?> <div class="category-name-<?= $cat;?>"> <a href="<?php echo $category->getUrl(); ?>"> <span><?php if($block->getData('title')){echo $block->getData('title');}else{echo $category->getName();} ?></span> </a> </div> <div class="category-more-<?= $cat;?>"> <a href="<?php echo $category->getUrl(); ?>"> <span>See More<?php echo $category->getID(); ?></span> </a> </div> <?php $categoryId = $cat; $cate = $categorysFactory->create()->load($categoryId); if($block->getData('filter-selection') == '0') { $categoryProducts = $cate->getProductCollection()->addAttributeToSelect('*')->addAttributeToSort('created_at','desc');; } elseif($block->getData('filter-selection') == '1') { $categoryProducts = $cate->getProductCollection()->addAttributeToSelect('*')->addAttributeToFilter('special_price', ['neq' => '']); } elseif($block->getData('filter-selection') == '2') { $categoryProducts = $cate->getProductCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_featured', '1'); } $block->getProductCollectionFromCategory($cat);?> <div id="homepage-wiget-slider" class="owl-side-arrow" style="margin: 0 -10px"> <div class="owl-carouel owl-thme" style="display: flex;"> <?php $counter = 5; foreach ($categoryProducts as $product) { if($counter > 0){ $counter--; $imagewidth=400; $imageheight=400; $imageHelper = $objectManager->get('\Magento\Catalog\Helper\Image'); $image_url = $imageHelper->init($product, 'product_page_image_small')->setImageFile($product->getFile())->resize($imagewidth, $imageheight)->getUrl(); ?> <div class="iem homeage-widget-slider-content-wrapper"> <a href="<?= $product->getProductUrl();?>"> <img src="<?php echo $image_url; ?>" alt="" /> <h4 class="price"><?php echo $product->getFormattedPrice();?></h4> <h3 class="title"><?php echo $product->getName();?></h3></a> </div> <?php }else{ break; } } ?> </div> </div> <?php endforeach; ?> |
Another solution if you only wanna fetch Products
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); $categoryId = 4; // could be any of yours $category = $categoryFactory->create()->load($categoryId); $categoryProducts = $category->getProductCollection() ->addAttributeToSelect('*'); foreach ($categoryProducts as $product) { // geting Product object print_r($product->getData()); echo $product->getName(); echo $product->getProductUrl(); } |
Object Manager Usage Good and Bad: ]]>