product has a custom options or not in magento? - magento

how to check that product has a custom options or not in magento?
Mage::setIsDeveloperMode(true);
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$product = Mage::getModel('catalog/product')->load($sku, 'sku');
$code = $product->getAttribute()->getAttributeCode();

$product = Mage::getModel('catalog/product')->load($sku, 'sku');
$hasOptions = $product->hasCustomOptions();
Simple as that. At the end $hasOptions should be true or false.

Related

Magento 1.9 Change Product Status Programmatically

I'm currently looking for a method to programmatically change the product status of all my products if a certain amount of stock is reached.
Ideally I would like to set all products to status Disabled if the Stock level is under 10. All others that are 10 and more , the product status should become Enabled.
You can try with this code by creating one php file to the root directory or you can create cron scheduler if you want to do this process for some time period.
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
ini_set('memory_limit', '600M');
ini_set('max_execution_time', 1800);
umask(0);
Mage::app('admin');
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addAttributeToSelect('*');
foreach ($collection as $_data) {
$productCat = $_data->getCategoryIds();
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_data);
$productqty = $stock->getQty();
if (intval($productqty) < 10) {
$productModel = Mage::getModel('catalog/product');
$productUpdate = $productModel->load($_data->getId());
$productUpdate->setStatus(2);
$productUpdate->save();
}
else
{
$productModel = Mage::getModel('catalog/product');
$productUpdate = $productModel->load($_data->getId());
$productUpdate->setStatus(1);
$productUpdate->save();
}
}

My custom Magento module won't return a category list

The normal way to get the store categories is:
$helper = Mage::helper('catalog/category');
$categories = $helper->getStoreCategories();
But when I use this in my custom admin module, it returns an
empty array. How can I use the category tree in an custom
admin module?
Try this code.
<?php
$helper = Mage::helper('catalog/category');
$categories = $helper->getStoreCategories('name', true, false);
//or below code
$categories = $helper->getStoreCategories('name', false, false);
?>

Magento Show custom attribute dropdown option for a simple product in cart page

how to Show custom attribute dropdown option for a simple product in cart page
my attribute name is meal_time
<?php
$product = Mage::getModel('catalog/product')->load($_item->getProductId());
echo $product->getAttributeText('meal_time');
?>
To get the custom attribute option in the cart , first we need to get quote visible product items and then load the product to get its options.
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
echo $_product->getMealTime();
}
To display all the option of an attribute
$cart = Mage::getSingleton('checkout/session')->getQuote()->getAllVisibleItems();
foreach ($cart as $item) {
$product_id = $item->getProduct()->getId();
$_product = Mage::getModel('catalog/product')->load($product_id);
$attribute = Mage::getModel('eav/config')-getAttribute('catalog_product', 'meal_time');
foreach ($attribute->getSource()->getAllOptions(true, true) as $instance) {
echo $myattribute[$instance['value']] = $instance['label'];
}
}
If you want to display as select dropdown , use select tag including the label and value as options

magento catalog search not working

I have to make custom catalog search. For this I have made a file in /var/www/magento/customsearch.php and put following code:
$searchText = 'test';
$query = Mage::getModel('catalogsearch/query')->setQueryText($searchText)->prepare();
$fulltextResource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
Mage::getModel('catalogsearch/fulltext'),
$searchText,
$query
);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->getSelect()->joinInner(
array('search_result' => $collection->getTable('catalogsearch/result')),
$collection->getConnection()->quoteInto(
'search_result.product_id=e.entity_id AND search_result.query_id=?',
$query->getId()
)
);
print_r($collection->getData());
But the collection returns blank array.It update catalogsearch query table each time but not update catalogsearch result table. Please help.
I put your same code on my Magento website:
<?php
include "app/Mage.php";
Mage::app();
$searchText = 'test';
$query = Mage::getModel('catalogsearch/query')->setQueryText($searchText)->prepare();
$fulltextResource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
Mage::getModel('catalogsearch/fulltext'),
$searchText,
$query
);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->getSelect()->joinInner(
array('search_result' => $collection->getTable('catalogsearch/result')),
$collection->getConnection()->quoteInto(
'search_result.product_id=e.entity_id AND search_result.query_id=?',
$query->getId()
)
);
print_r($collection->getData());
And got an array with three of my products. So I guess, check your setup? You have products right? With the keyword 'test' some where in the attribute values?

Magento: Can I load a cart by quoteId?

I need to load a cart by a quoteId, cause I want to add a product to a different cart than the current cart. Is this possible?
TIA!
$cartId = 99;
$cart = Mage::getModel('sales/quote')->load($cartId);
$productId = 55;
$product = Mage::getModel('catalog/product')->load($productId());
$cart->addProduct($product);

Resources