Return products if Category is not Active - magento

How can I make $cat->getProductCollection(); to return products if the category is NOT ACTIVE
It's been a very long time. I could not make this to work.
This is what I am doing. But no Luck
private function _getCollection() {
$category = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('is_active',array('eq'=>False))
->addAttributeToFilter('entity_id',array('eq'=>'61'))
->load();
foreach($category as $cat){
$Prods = $cat->getProductCollection();
$Prods->addWebsiteFilter();
$Prods->addAttributeToSelect('*');
}
return $Prods;
}

Try this code
$cat = Mage::getModel('catalog/category')->getCollection()->addAttributeToFilter('is_active', 1);
foreach ($cat as $category){
$cat_id = $category->getId();
$category = new Mage_Catalog_Model_Category();
$category->load($cat_id);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) {
echo $_product->getName();
}
}

Related

How do I get all subcategories of parent categories in Laravel?

I have all parent categories in the $categories variable, and all subcategories in the $subCats variable. The problem is that, in addition to the subcategories, empty parent categories also fall into the $subCats variable. How can you fix this?
public function showForm()
{
$subCats = [];
$categories = Category::where('parent_slug', null)->get();
foreach($categories as $cats) {
$subCats[] = Category::where('parent_slug', $cats->slug)->get();
}
return view('user.form', compact('categories', 'subCats'));
}
I think you don't need a loop :
$categories = Category::where('parent_slug', null)->get();
$subCats = Category::where('parent_slug', '!=', null)->get();
return view('user.form', compact('categories', 'subCats'));
For more performance, you can filter with whereNull() method on a collection :
$data = Category::get();
$categories = $data->whereNull('parent_slug')->all();
$subCats = $data->whereNotNull('parent_slug')->all();
return view('user.form', compact('categories', 'subCats'));
Try this:
public function showForm()
{
$subCats = [];
$categories = Category::where('parent_slug', null)->get();
foreach($categories as $cats) {
$subCat = Category::where('parent_slug', $cats->slug)->get();
if($subCat->count()) {
$subCats[] = $subCat;
}
}
return view('user.form', compact('categories', 'subCats'));
}

Magento: how to get the quantity of each bundle option that was ordered

//Observer function
$order = $observer->getEvent()->getOrder();
foreach($order->getAllItems() as $item){
if($item->getProductType() == 'bundle')
{
//Loading bundle product object
$bundle_product = Mage::getModel('catalog/product')->load($item->getProductId());
//Getting bundle items collection
$selectionCollection = $bundle_product->getTypeInstance(true)->getSelectionsCollection($bundle_product->getTypeInstance(true)->getOptionsIds($bundle_product), $bundle_product);
foreach($selectionCollection as $option)
{
//Loading each bundle item
$bundle_item = Mage::getModel('catalog/product')->load($option->getId());
//How to get the quantity that was ordered? example:
$bundle_item->getQtyOrdered(); //Note: I know this is wrong, this is not the correct object.
}
}
}
Please use below code:
//Observer function $order = $observer->getEvent()->getOrder();
foreach($order->getAllItems() as $item){
if($item->getProductType() == 'bundle')
{
$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
$bundleOption=$options['info_buyRequest']['bundle_option_qty'];
foreach($bundleOption as $bundleItemQty){
echo 'Bundle Item Qty='.$bundleItemQty;
}
}
}

Magento2 get product images externally

I have a external script i use to fetch the current quote products (as external cart for example) This works perfect.
In this code i use the following:
$quote = $obj->get('Magento\Checkout\Model\Session')->getQuote();
$helper = $obj->get('\Magento\Checkout\Helper\Cart');
$quote = $helper->getQuote();
$quoteitems = $quote->getAllItems();
$cart= $helper->getCart();
foreach ($quoteitems as $item)
{
$_imagehelper = $obj->get('\Magento\Catalog\Helper\Image');
$product = $item->getProduct();
$img = $_imagehelper->init($product,'category_page_list',array('height' => '100' , 'width'=> '100'))->getUrl();
}
This results in: /pub/static/frontend/_view/nl_NL/Magento_Catalog/images/product/placeholder/.jpg
While in a layout block i use the following and that works perfect:
$helper = $this->helper('\Magento\Checkout\Helper\Cart');
$quote = $helper->getQuote();
$quoteitems = $quote->getAllItems();
$cart= $helper->getCart();
foreach ($quoteitems as $item)
{
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
$product = $item->getProduct();
$img = $_imagehelper->init($product,'category_page_list',array('height' => '100' , 'width'=> '100'))->getUrl();
}
Im out of ideas.
use the following code
// your product's id here
$pid = 7;
// set image width and height
$imagewidth = 500;
$imageheight = 500;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_product = $objectManager->get('Magento\Catalog\Model\Product')->load($pid);
$imageHelper = $objectManager->get('\Magento\Catalog\Helper\Image');
$image_url = $imageHelper->init($_product, 'product_page_image_small')->setImageFile($_product->getFile())->resize($imagewidth, $imageheight)->getUrl();
Use below code to get product image externally
use \Magento\Framework\App\Bootstrap;
include('app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$url = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $url->get('\Magento\Store\Model\StoreManagerInterface');
$mediaurl= $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$state = $objectManager->get('\Magento\Framework\App\State');
$state->setAreaCode('frontend');
$pid=1;
$_product = $objectManager->create('\Magento\Catalog\Model\Product')->load($pid);
$data=$_product->getData();
echo $imageurl=$_product->getImage();
//print_r($data);
?>
<img src="<?php echo $mediaurl.'catalog/product'.$imageurl;?>">

Magento products from attribute

I have the following code, which will get all products from all orders for one logged in customer which works fine. I want to add to this code so that it only returns products from a specified attribute set. I believe I have both bits of code that I need they just won't work together.
Code I want to add to is:
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();
$customer_id = $customer->getId();
}
$collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));
$uniuqProductSkus = array();
foreach ($collection as $order) {
$order_id = $order->getId();
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach ($ordered_items as $item)
{
if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) {
continue;
} else {
array_push($uniuqProductSkus, $item->getProduct()->getSku());
echo various variables here;
}
}
}
?>
Code I have used before to get products from a specified attribute set
$attrSetName = 'Beer';
$attributeSetId = Mage::getModel('eav/entity_attribute_set')
->load($attrSetName, 'attribute_set_name')
->getAttributeSetId();
//Load product model collecttion filtered by attribute set id
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('attribute_set_id', $attributeSetId);
You can add this piece of code to make your script work in the foreach loop.
$product = Mage::getModel('catalog/product')->load($sku, 'sku');
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($product->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
if(0 == strcmp($attributeSetName, 'Beer') {
//add your logic
}else{
continue;
}
Update:
<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {
/* Get the customer data */
$customer = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();
$customer_id = $customer->getId();
}
$collection =
Mage::getModel('sales/order')->getCollection();
$collection->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));
$uniuqProductSkus = array();
foreach ($collection as $order) {
$order_id = $order->getId();
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach ($ordered_items as $item)
{
$item->getProduct()->getSku();
if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) {
continue;
} else {
$attributeSetModel = Mage::getModel("eav/entity_attribute_set");
$attributeSetModel->load($item->getProduct()->getAttributeSetId());
$attributeSetName = $attributeSetModel->getAttributeSetName();
if(0 == strcmp($attributeSetName, 'Beer')) {
array_push($uniuqProductSkus, $item->getProduct()->getSku());
// echo various variables here;
}else{
continue;
}
}
}
}
print_r($uniuqProductSkus);
?>

Programmatically create new category in magento

I just want to create new category programmatically in magento. I have a code and tried this but can't success.
code is here:
<?php
$parentId = '2';
$category = new Mage_Catalog_Model_Category();
$category->setName('check');
$category->setUrlKey('new-category');
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(0);
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
$category->save();
unset($category);
?>
actually the problem with this code whenever I tried it than it create new category but can't set is_active yes in admin.
Please set Store id for that category. To code out side magento page, use codes below:
try{
$category = Mage::getModel('catalog/category');
$category->setName('check');
$category->setUrlKey('new-category');
$category->setIsActive(1);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(1); //for active achor
$category->setStoreId(Mage::app()->getStore()->getId());
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
$category->save();
} catch(Exception $e) {
var_dump($e);
}
Please refer my tutorial which explains how to create the category and subcategories pro-grammatically.
function addManufacturers( $manufacturers ) {
Mage::register('isSecureArea', 1);
$parentId = '2';
$list = array();
foreach ($manufacturers as $key => $manufacturer) {
try {
$enabled = 1;
if ($key == 0) {
$parentId = '2';
}
else {
$parentId = $list[0];
}
$category = Mage::getModel('catalog/category');
$category->setName($manufacturer);
$category->setMetaTitle($manufacturer);
$category->setIncludeInMenu(1);
$category->setUrlKey(strtolower($manufacturer));
$category->setDescription(strip_tags($manufacturer));
$category->setMetaDescription($manufacturer);
$category->setMetaKeywords($manufacturer);
$category->setIsActive($enabled);
$category->setDisplayMode('PRODUCTS');
$category->setIsAnchor(1); //for active anchor
$category->setStoreId(Mage::app()->getStore()->getId());
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$category->setPath($parentCategory->getPath());
$category->setCustomUseParentSettings(true);
$category->save();
$list[$key] = $category->getId();
echo 'Category ' . $category->getName() . ' ' . $category->getId() . ' imported successfully' . PHP_EOL;
} catch (Exception $e) {
echo 'Something failed for category ' . $manufacturer . PHP_EOL;
print_r($e);
}
}
return $list;
}
http://www.pearlbells.co.uk/how-to-create-new-categories-and-assigned-products-to-category-programmatically-magento/

Resources