Magento 2 Get product image


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

<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>$imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image'); $imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image'); $image_url = $imageHelper->init($_item, 'product_page_image_small')->setImageFile($_item->getFile())->resize(100, 100)->getUrl();

Advertisements

How to remove or limit the Mass Action in Magento2

1) Create the file in your module with the path  /app/code/Vendor/Module/view/adminhtml/ui_component/product_listing.xml

Here i am taking the example of Product page.


<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top">
<massaction name="listing_massaction" class="Vendor\Module\Ui\MassAction"/>
</listingToolbar>
</listing>

2) Create The class for modify the mass option at below path.
/app/code/Vendor/Module/Ui/MassAction.php


<?php namespace Vendor\Module\Ui; class MassAction extends \Magento\Ui\Component\MassAction { private $authorization; public function __construct( \Magento\Framework\View\Element\UiComponent\ContextInterface $context, \Magento\Framework\AuthorizationInterface $authorization, $components, array $data ) { $this->authorization = $authorization;
parent::__construct($context, $components, $data);
}

public function prepare()
{
parent::prepare();
$config = $this->getConfiguration();
//if (!$this->authorization->isAllowed('Magento_Catalog::the_acl_youd_like_to_use')) {
$allowedActions = [];
foreach ($config['actions'] as $action) {
if ('delete' != $action['type']) {
$allowedActions[] = $action;
}
}
$config['actions'] = $allowedActions;
//}
$this->setData('config', (array)$config);
}
}

Magento add checkbox and searchbox in Layer Filter

1)Replace the below code in files app/design/frontend/{yourtheme}/default/template/catalog/layer/view.phtml

<?php if($this->canShowBlock()): ?>
<div class="block block-layered-nav">
<div class="block-title">
<strong><span><?php echo $this->__('Shop By') ?></span></strong></div>
<div class="block-content">
<?php echo $this->getStateHtml() ?>
<?php if ($this->getLayer()->getState()->getFilters()): ?>
<div class="actions"><a class="btn btn-default" href="<?php echo $this->getClearUrl() ?>"><i class="icon-trash"></i><?php echo $this->__('Clear All') ?></a></div>
<?php endif;?>
<?php if( $this->canShowOptions()): ?>

<?php echo $this->__('Shopping Options') ?>
<dl id="narrow-by-list" class="clearfix">
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getItemsCount()): ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>

<dd<?php echo ($this->__($_filter->getName()) == $this->__('Category') ? ' class="category-filter"' : ''); ?>>
<?php if($this->__($_filter->getName())!= $this->__('Price') ):?>
<input type="text" placeholder="<?php echo $this->__('Search for option'); ?>" class="quick-search form-control" />
<?php endif;?>
<?php echo $_filter->getHtml() ?>
</dd>

<?php endif; ?>
<?php endforeach; ?>
</dl>

<script type="text/javascript">
 decorateDataList('narrow-by-list');
 </script>
<?php endif; ?></div>
</div>
<?php endif; ?>

<script type="text/javascript">
jQuery(document).ready(function(){
jQuery(document).on('change','.ccheckbox', function() {
 jQuery(this).siblings('a').trigger("click");
 });
 jQuery(document).on('keyup','.quick-search',function(){
if(jQuery(this).val() != ''){
//alert('');
 var term = jQuery(this).val();
term = term.toLowerCase();
 jQuery(this).siblings('ol').find('li').each(function(){
 if( jQuery(this).find('a').text().toLowerCase().indexOf(term) >= 0 ){
 jQuery(this).show();
 } else {
 jQuery(this).hide();
 }
 });
}else{
jQuery(this).siblings('ol').find('li').each(function(){ 
 jQuery(this).show();
 });}
 });
 
});
 </script>

2)  Add below code in file /app/design/frontend/{yourtheme}/default/template/catalog/layer/filter.phtml

<ol>
<?php $selection = FALSE; $items = $this->getItems();
foreach ($this->getItems() as $_item)
{
if ($_item->isSelected())
{
$selection = TRUE;
$removal_url=$_item->getRemoveUrl();
break;
}
}
?>

<?php foreach ($this->getItems() as $_item): ?>
<?php $code =$_item->getFilter()->getAttributeModel()->getAttributeCode();
if($code !="price") {
$lable = $_item->getLabel();
}else{
$lable = '';
}

?>
	<li label="<?php echo $lable ?>">
<?php if( $_item->isSelected() ) : ?>
<input class="ccheckbox" type="checkbox" checked/>
<a style="color: #D96708" class="multi-select isselected" href="<?php echo $removal_url ?>">
<?php echo $_item->getLabel() ?></a>
<?php else : ?>
<input type="checkbox" class="ccheckbox"/>
<a style="color: #666633" class="multi-select unselected" href="<?php echo $this->urlEscape($_item->getUrl()) ?>">
<?php echo $_item->getLabel() ?></a>
<?php endif; ?></li>
<?php endforeach ?></ol>

3) Replace below line in file app/code/core/Mage/Catalog/Model/Resource/Layer/Filter/Attribute.php


// $tableAlias = $attribute->getAttributeCode() . '_idx';
$tableAlias = $attribute->getAttributeCode() . '_idx_'.$value;

4)  Comment below line in file app/code/core/Mage/Catalog/Model/Layer/Filter/Attribute.php

// $this->_items = array(); Line 96

5) Add below function in file app/code/core/Mage/Catalog/Model/Layer/Filter/Item.php


public function isSelected(){
$selected = Mage::getSingleton('core/app')->getRequest()->getParam($this->getFilter()->getRequestVar());

if($selected == $this->getValue()){
return true;
}else{
return false;
}
}

 

How to create new command in magento2

Step1:- Declare module in etc/module.xml


<?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="Ecommoco_Command" setup_version="1.0.0" />
</config>

Step2: Register module in registration.php


<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Ecommoco_Command',
__DIR__
);

Step3: Declare command in etc/di.xml


<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="firstcommand" xsi:type="object">Ecommoco\Command\Console\Firstcommand</item>
</argument>
</arguments>
</type>
</config>

Step4: Create the class for execution


<?php
namespace Ecommoco\Command\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Firstcommand extends Command
{
protected function configure()
{
$this->setName('ecommoco:first');
$this->setDescription('First Command');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("First command");
}
}

Activate the module

Export products to csv by script

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$collection = Mage::getModel(‘catalog/product’)
->getCollection()
->addAttributeToSelect(‘*’)
->addUrlRewrite();

// create a file pointer connected to the output stream
header(‘Content-Type: text/csv; charset=utf-8’);
header(‘Content-Disposition: attachment; filename=all_products.csv’);
$output = fopen(‘php://output’, ‘w’);

// output the column headings
fputcsv($output, array(‘id’, ‘sku’, ‘title’, ‘url’, ‘image0′,’image1′,’image2′,’image3′,’status’,’price’,’special_price’,’special_from_date’,’special_to_date’,’qty’,’is_in_stock’,’type’));
foreach($collection as $product) {
//Load the product categories
$categories = $product->getCategoryIds();
//Select the last category in the list
$categoryId = end($categories);
//Load that category
$category = Mage::getModel(‘catalog/category’)->load($categoryId);
// Collect details in variables
$title=$product->getName();
$sku=$product->getSku();
$id=$product->getId();

$url=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB).$product->getUrlPath();
//$image=str_replace(‘export.php/’,”,Mage::getBaseUrl()).$product->getUrlPath($category);
$image=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA) . ‘catalog/product’ . $product->getImage();

//Gallary
//$_product = $product->load($product->getId());
$gallary = [”,”,”,”];
$mediaApi = Mage::getModel(‘catalog/product_attribute_media_api’);
$mediaItems = $mediaApi->items($product->getId());
foreach ($mediaItems as $key=> $images) {
$gallary[$key]=$images[‘url’];
}

$price=$product->getPrice();
$status = ‘Disabled’;
if(‘1’ == $product->getStatus()){
$status = ‘Enabled’;
}
$type=$product->getTypeId();
$sp=$product->getFinalPrice();
$from=$product->getSpecialFromDate();
$to = $product->getSpecialToDate();

$stock_item = Mage::getModel(‘cataloginventory/stock_item’)->loadByProduct($id);
$qty = $stock_item->getQty();
$stock =$stock_item->getIsInStock();
fputcsv($output, array( $id, $sku, $title, $url,$gallary[0],$gallary[1],$gallary[2],$gallary[3], $status,$price,$sp,$from,$to,$qty,$stock, $type));
}

Magento get image gallery

$gallery = [];

$product = Mage::getModel(‘catalog/product’)->load(1);
$mediaApi = Mage::getModel(‘catalog/product_attribute_media_api’);
$mediaItems = $mediaApi->items($product->getId());
foreach ($mediaItems as $key=> $images) {
$gallery[]=$images[‘url’];
}

var_dump($gallery);