I am grabbing a product object with:
$productId = 9184;
$objectManagerProd = \Magento\Framework \App\ObjectManager::getInstance();
$currentproduct = $objectManagerProd->create('Magento\Catalog\Model\Product')->load($productId);
I have tried to grab the product image with the image helper but it is not working. I have tried getImage() as well. Any body can help? Thanks!
Get product image from Product in PHTML file for Magento 2
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$imageHelper = $_objectManager->get('\Magento\Catalog\Helper\Image');
<?php $image_url = $imageHelper->init($product, 'product_thumbnail_image')->setImageFile($product->getFile())->resize($imagewidth, $imageheight)->getUrl(); ?>
try using imageHelper like following :
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$helperImport = $objectManager->get('\Magento\Catalog\Helper\Image');
$imageUrl = $helperImport('Magento\Catalog\Helper\Image')
->init($currentproduct, 'product_page_image_large')
->setImageFile($currentproduct->getFile())
->getUrl();
Related
I'm trying to get brand_label of a category in Magento2. Here is my code at the moment :
$categoryObj = $this->categoryRepository->get($parent_category_id);
$Categories = $categoryObj->getChildrenCategories()->addAttributeToFilter('include_in_menu', array('eq' => 1));
$categoryArray = [];
foreach($Categories as $category){
$categoryObject->brand_label = $category->getCustomAttribute('brand_label');
array_push($categoryArray,$categoryObject);
}
The $categoryObject->brand_label is returning null
You can retrieve the data from a category by using these ways:
$category = $this->categoryRepository->get($categoryId);
$category->getData('brand_label');
Or:
$category = $this->categoryRepository->get($categoryId);
$category->getBrandLabel();
Make sure your attribute code of category is brand_label
I have category ID and need to get all custom attribute for example thumbnail image.
my code does not return all attributes
$category = $this->categoryRepository->get($childId, $this->_storeManager->getStore()->getId());
$category->getData();
You can use Category's CollectionFactory class and select all attributes by using a star (*) symbol in addAttributeToSelect method. You can use this code example below in your class.
protected $_categoryFactory;
public function __construct(
// ...
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $collecionFactory,
) {
// ...
$this->_categoryFactory = $collecionFactory;
}
public function yourFunctionName()
{
$catId = 3; // your category id
$collection = $this->_categoryFactory
->create()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id',['eq'=>$catId])
->setPageSize(1);
$catObj = $collection->getFirstItem();
$thumbnail = $catObj->getThumbnail(); // it should return value if attribute name is thumbnail
$catData = $catObj->getData(); // dump this line to check all data
// ...
}
Try below code:
$categoryId = 5;
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$object_manager = $_objectManager->create('Magento\Catalog\Model\Category')->load($categoryId);
echo "<pre>";
print_r($object_manager->getData());
die('shasha test');
I hope it will help...!!!
I am new in magento2 and using version 2.1.
I have to add multiple product in cart, the product can be any type and I want to add ajax validation as well with this functionality.
Can any one has idea to achieve this?
Thanks,
Chandan
Hi I got solution for adding multiple products in cart in for loop:
Please refer below code to implement this functionality in MAGENTO 2
if ($product_id) {
$storeId = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
try {
if(isset($params['super_attribute'][$product_id])){
$params_post = array();
$params_post['form_key'] = $this->formKey->getFormKey();
$params_post['qty'] = 1;
$params_post['super_attribute'] = $params['super_attribute'][$product_id];
$finalproduct = $this->productRepository->getById($product_id, false, $storeId);
$this->cart->addProduct($finalproduct, $params_post);
} else {
$params_post = array();
$params_post['form_key'] = $this->formKey->getFormKey();
$params_post['qty'] = 1;
$finalproduct = $this->productRepository->getById($product_id, false, $storeId);
$this->cart->addProduct($finalproduct, $params_post);
}
} catch (NoSuchEntityException $e) {
}
}
My question was after yopu add a image to te prodcut in magento 2, how to set is as the default image (base image) for the product.
I found out how to add images to the product, but coulnd find a was to set it as base image for the product.
after loading the product:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->load(PRODUCT ID);
You can get all the images of a product with the following code:
$images = $product->getMediaGalleryImages();
This will return a collection with all the images added to the product.
You can pick any image or take the first one like this:
$image = $images->getFirstItem();
After that you van get the image url with
$imageUrl = $image->getFile();
To set it to the product is this:
$product->setImage($image);
$product->setSmallImage($image);
$product->setThumbnail($image);
$product->setSwatchImage($image);
And save the product like this:
$product->save();
After get the product ids you want, for each product write the codes like below
$product = $this->productRepository->getById($data['product_id']);
$mediaGalleryEntriesTemp = [];
$i = 1;
foreach ($product->getMediaGalleryEntries() as $item) {
if ($i === 1) {
$item->setTypes(['image', 'small_image', 'thumbnail']);
}
$mediaGalleryEntriesTemp[] = $item;
$i++;
}
$product->setMediaGalleryEntries($mediaGalleryEntriesTemp);
$this->productRepository->save($product);
Notice: 'swatch_image' role only available in simple product or
configurable child product, not configurable parent product.
After my test, the solutions work in Magento 2.2.3, other version should work too.
My solution for Magento 2.3.3 and working fine
for me, my scenario is a little bit different I need the base and small image for my existing products and I have images name :)
After saving the image you can use this code to make the base image.
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId());
$images = $product->getMediaGalleryEntries();
$mediaGallery = $product->getMediaGallery();
//if there are images
if (isset($mediaGallery['images'])){
//loop through the images
foreach ($mediaGallery['images'] as $image){
// make the base image by name
if(strpos($image['file'], 'PRODUCT_NAME') !== false){
$objectManager->get('Magento\Catalog\Model\Product\Action')->updateAttributes(array($product->getId()), array('image'=>$image['file'], 'small_image'=>$image['file']), 0);
//stop
break;
} else{
echo "can't find";
}
}
}
Change the PRODUCT_NAME or if no need it removes the if-else condition
I hope it helps other
Not sure if it is possible with ZF 1.11 to do the following
Normal method of displaying a photo
www.mysite.com/photos/display/377486
Where 377486 is the id of a photo within mysql and can be any integer
Is it possible to have the url shortened so I get the same with
www.mysite.com/photos/377486
If this is possible what is this technique/method called?
Many thanks
No, You need 2 different route.
You can test :
routes.photos.type = "Zend_Controller_Router_Route_Regex"
routes.photos.route = "photos/display/(\d+)"
routes.photos.defaults.controller = "photos"
routes.photos.defaults.action = "display"
routes.photos.map.1 = "id"
routes.photos.reverse = "books/%d"
routes.photos_short.type = "Zend_Controller_Router_Route_Regex"
routes.photos_short.route = "photos/(\d+)"
routes.photos_short.defaults.controller = routes.photos.defaults.controller
routes.photos_short.defaults.action = routes.photos.defaults.action
routes.photos_short.map.1 = "id"
routes.photos_short.reverse = "books/%d"