magento product count for attribute - magento

how to show product count on attribute?
code/core/Mage/Catalog/Block/Category/View.php
public function getAllManu()
{
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter('attribute_code', 'product_properties');
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$product_properties = $attribute->getSource()->getAllOptions(false);
return $product_properties;
}
view.phtml
<select class="form-control" onchange="if (this.value) window.location.href=this.value">
<option>Select</option>
<?php foreach ($this->getAllManu() as $product_properties): ?>
<option value="<?php Mage::getURL() ?>catalogsearch/advanced/result/?product_properties[]=<?php echo $product_properties['value'] ?>"><?php echo $product_properties['label'] ?></option>
<?php endforeach; ?>
</select>

Gokhan,use count() function count the collection item collection
<select class="form-control" onchange="if (this.value) window.location.href=this.value">
<option>Select</option>
<?php foreach ($this->getAllManu() as $product_properties): ?>
$productcollection = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('product_properties', $product_properties['value'])
->addAttributeToFilter('status',array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));;
$productcollection = $productcollection->addCategoryFilter(Mage::getModel('catalog/category')->load($currcategory),true);
$productcollection->count();
<?php endforeach; ?>
</select>
$rel_products ->count();

Related

get value of multiselect in edit form

I want to edit a form so when i am in edit form previous edited values should shown in that field but i don't get the selected value from mysql
my edit form
<select data-placeholder="Select Rank..." class="chosen-select custom_select " multiple="" style="width: '-webkit-fill-available';" id="rank_restriction" name="rank_restriction[]">
<?php
foreach ($poll['rankname'] as $rank) { ?>
<option value="<?= $rank ?>"><?= $rank ?></option>
<?php } ?>
</select>
<?php
if (isset($edit['rank_restriction']) && !empty($edit['rank_restriction'])) {
$rank_restriction = $edit['rank_restriction']; // array format
} else {
$rank_restriction = set_value('rank_restriction'); // again in array format
}
?>
<select data-placeholder="Select Rank..." class="chosen-select custom_select "
multiple="" style="width: '-webkit-fill-available';" id="rank_restriction"
name="rank_restriction[]">
<?php
foreach ($poll['rankname'] as $rank) {
$selected = (!empty($rank_restriction) && in_array($rank, $rank_restriction)) ? 'selected' : '';
?>
<option value="<?= $rank ?>" <?php echo $selected ?>><?= $rank ?>
</option>
<?php } ?>
</select>

Dropdown Selected Value not Saving in DB

I am new to Codeigniter and created a project in which dropdown grab the data from table 1 and I want to save the data to table 2. However, the other value option is saving but not dropdown values. Code is given below.
Model:
function getPriviousScore() {
$this->db->select('*');
$this->db->from('ielts_previous_score');
$query = $this->db->get();
return $query->result_array();
}
Controller:
$data['ielts_previous_score'] = $this->enquiry_model->getPriviousScore();
View:
<select name="ielts_previous_score" class="form-control">
<option value=""><?php echo $this->lang->line('select'); ?></option>
<?php foreach ($ielts_previous_score as $key => $value) { ?>
<option value="<?php print_r($value['ielts_previous_score']); ?>" <?php if (set_value('ielts_previous_score') == $value['ielts_previous_score']) { ?>selected=""<?php } ?>><?php print_r($value['ielts_previous_score']); ?></option>
<?php } ?>
</select>
MODEL
function getPriviousScore() {
$this->db->select('*');
$this->db->from('ielts_previous_score');
$query = $this->db->get();
return $query->result_array();
}
CONTROLLER
$data['ielts_previous_score'] = $this->enquiry_model->getPriviousScore();
VIEW
<select name="ielts_previous_score" class="form-control">
<option value=""><?php echo $this->lang->line('select'); ?></option>
<?php foreach ($ielts_previous_score as $key => $value) { ?>
<option value="<?php echo $value['ielts_previous_score']; ?>" <?php if
(set_value('ielts_previous_score') == $value['ielts_previous_score']) { ?
>selected=""<?php } ?>><?php echo $value['ielts_previous_score']; ?></option>
<?php } ?>
</select>

Display magento products by category ID

I need to know how can I display products in a page like (cart, below total) only few products by ID. Eg: products with id 2,3,4 and 5.
<div class="freeProducts voucher code">
<?php
$categoryid = 64;
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
foreach ($collection as $_product) { ?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200); ?>" width="200" height="200" alt="" /> <?php echo $_product->getName(); ?>
<?php } ?>
</div>
At this moment I can see the image for each product, and the title. I need to display the ADD TO CART and price.
Anyone can help please?
get Product from specific category
$categoryIds = array(2,4);//category id
$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('in' => $categoryIds))
get Product for specific product id
$productids = array(52,62);//product ids
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addFieldToFilter('entity_id',array( 'in' => $productids));
then write this in phtml
<?php $_collectionSize = $collection->count() ?>
<?php //$_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($collection as $product): ?>
<?php if ($i++%4==0): ?>
<ul class="products-grid">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="<?php echo $product->getProductUrl()?>" title="<?php echo $product->getName()?>">
<img src="<?php echo Mage::helper('catalog/image')->init($product, 'small_image')->resize(197, 167); ?>" alt="<?php echo $product->getName()?>" border="0" />
</a>
<h2 class="product-name"><?php echo $product->getName() ?></h2>
<div class="price-box">
<?php echo Mage::helper('core')->currency($product->getPrice(),true,false);?>
</div>
<div class="actions">
<?php if($product->isSaleable()): ?>
<button class="button" onclick="setLocation('<?php echo Mage::getUrl('checkout/cart/add/')?>product/<?php echo $product->getId() ?>/')" title="<?php echo $this->__('Köp');?>" type="submit"><span><span><?php echo $this->__('Köp');?></span></span></button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
hope this help you
// print_r($productslist)
$category_id = 14; // if you know static category then enter number
$catagory_model = Mage::getModel('catalog/category')->load($category_id);
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($catagory_model); //category filter
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToSelect(array('name','url','small_image')); //add product attribute to be fetched
//$collection->getSelect()->order('rand()'); //uncomment to get products in random order
$collection->addStoreFilter();
if(!empty($collection))
{
foreach ($collection as $_product):?>
<img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(197, 167); ?>" />
<?php endforeach;
}else
{
echo 'No products exists';
}
$categoryid = 123;
$_category = Mage::getModel('catalog/category')->load($categoryid);
$productCollection = Mage::getResourceModel('catalog/product_collection')
->addCategoryFilter($_category);
<?php
require "../app/Mage.php";
Mage::app();
?>
<table>
<?php
$ris_arr = array(39,77,78,79);
foreach ($ris_arr as &$value) {
?>
<tr><td style="background-color:#foo;margin-top:50px;">
<?php
$ris_value = $value;
$category_id = $ris_value; // if you know static category then enter number
$catagory_model = Mage::getModel('catalog/category')->load($category_id);
$collection = Mage::getResourceModel('catalog/product_collection')->setPageSize(4);;
$collection->addCategoryFilter($catagory_model); //category filter
$collection->addAttributeToFilter('status',1); //only enabled product
$collection->addAttributeToSelect(array('name','url','small_image','price','sku')); //add product attribute to be fetched
//$collection->getSelect()->order('rand()'); //uncomment to get products in random order
$collection->addStoreFilter();
if(!empty($collection))
{ ?>
<div style="width:100%">
<?php foreach ($collection as $_product):?>
<div style="width:200px;float:left">
<?php $pro_url= $_product->getProductUrl();
$pro_url2=str_replace("rishabh_daily_report_cron2.php/","",$pro_url);?>
<a href="<?php echo $pro_url2;?>">
<img src="<?php echo Mage::helper('catalog/image')->init($_product, 'small_image')->resize(197, 167); ?>" /> </a>
<?php
echo $_product->getName()."<br>";
//echo $_product->getSku()."<br>";
echo $_product->getSpecialPrice();
?>
</div>
<?php endforeach; ?>
</div>
<?php
}else
{
echo 'No products exists';
}
echo "</td></tr>";
}
?>
<table>

Fetch custom dropdown in Magento frontend

I have already added custom table called quantities in database. I want to show this as drop down in front end.
$model = Mage::getModel('quantities/quantities')->load($_product->getId());
How to fetch this data and show as dropdown. I am new to Magento.
Thanks in Advance.
You could try this:
$model = Mage::getModel('quantities/quantities')->load($_product->getId());
<select>
<?php foreach($model->getData() as $_data): ?>
<option><?php echo $_data->getYourAttribute() ?></option>
<?php endforeach; ?>
</select>
Granted you know what data is contained in your model. If not just var_dump($_data) or you can print_r($_data)
In the template (*.phtml) file, using Magento Block like this...
<?php
$select = $this->getLayout()->createBlock('core/html_select')
->setName('data['.$selectName.']')
->setId("sel_$selectId")
->setClass('quantity-select')
->setOptions($model->getData())
->setValue($value);
echo $select->getHtml();
?>
or Building it from scratch...
<select name="sel_name" id="sel_id">
<option><?php echo $this->__('Choose an Option...') ?></option>
<?php foreach ($model->getData() as $key => $value): ?>
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php endforeach; ?>
</select>
Will this do?

Magento Set Multiple Currencies

I have a problem with Magento currency: My code is working fine otherwise, now I want to redirect the page after a change of currency.
Suppose my current URL is http://www.example.com/women/?color=black.
Now, When I change currency it will redirect to: http://www.example.com/women/; so it removes ?color=black.
Code:
if($this->getCurrencyCount()>1): ?>
<div class="block block-currency">
<div class="block-title">
<strong><span><?php echo $this->__('Select Your Currency') ?></span></strong>
</div>
<div class="block-content">
<select name="currency" title="<?php echo $this->__('Select Your Currency') ?>" onchange="setLocation(this.value)">
<?php foreach ($this->getCurrencies() as $_code => $_name): ?>
<option value="<?php echo $this->getSwitchCurrencyUrl($_code) ?>"<?php if($_code==$this->getCurrentCurrencyCode()): ?> selected="selected"<?php endif; ?>>
<?php echo $_name ?> - <?php echo $_code ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<?php endif;
Updated Code:
<?PHP $currentParams = $this->getRequest()->getParams(); ?>
<div style="float:left; margin-top:10px; color:#727478;">
<!--Change 1 to 0 if you wish to output selector always-->
<?php if($this->getCurrencyCount() > 1): ?>
<span>Select Currency:</span>
<select name="custom-currency-selector" id="custom-currency-selector">
<?php foreach ($this->getCurrencies() as $_code => $_name):
$currencyUrl = $this->getSwitchCurrencyUrl($_code);
foreach ($currentParams as $_param) {
$currencyUrl = $this->helper('core/url')->addRequestParam($currencyUrl, $_param);
}
?>
<option value="<?php echo $currencyUrl; ?>"
<?php if($_code == $this->getCurrentCurrencyCode()): ?>
selected="SELECTED"
<?php endif; ?>>
<?php echo $_code ?>
</option>
<?php endforeach; ?>
</select>
<?php endif;?>
Updated Code:
<!--Change 1 to 0 if you wish to output selector always-->
<?php if($this->getCurrencyCount() > 1):
$currentParams = $this->getRequest()->getParams();
?>
<span>Select Currency:</span>
<select name="custom-currency-selector" id="custom-currency-selector">
<?php foreach ($this->getCurrencies() as $_code => $_name):
$currencyUrl = $this->helper('core/url')->addRequestParam($this->getSwitchCurrencyUrl($_code),$currentParams)
?>
<option value="<?php echo $currencyUrl ?>"
<?php if($_code == $this->getCurrentCurrencyCode()): ?>
selected="SELECTED"
<?php endif; ?>>
<?php echo $_code ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>
You should add these params to the new url:
$currentParams = $this->getRequest()->getParams();
$currencyUrl = $this->helper('core/url')->addRequestParam(
$this->getSwitchCurrencyUrl($_code),
$currentParams
)
Then use $currencyUrl as url for redirect.

Resources