Archive for the ‘ Magento ’ Category

Manufacturer Search in Magento

Put following code into controller file.

$prod_name = Mage::app()->getRequest()->getParam(‘schar’);
//  $model_data = Mage::getModel(‘catalog/product’)->getCollection()->addAttributeToSelect(‘name’)->addFieldToFilter(‘name’,array(‘like’=>$prod_name.’%’));
// print_r($model_data->getData());exit;
$store_id=Mage::app()->getStore()->getStoreId();
$product = Mage::getModel(‘catalog/product’);
$attributes = Mage::getResourceModel(‘eav/entity_attribute_collection’)
->setEntityTypeFilter($product->getResource()->getTypeId())
->addFieldToFilter(‘attribute_code’, ‘manufacturer’);

$attributes->getSelect()
->join(
array(‘attlable’=>’eav_attribute_option’),
‘additional_table.attribute_id = attlable.attribute_id’,
array(‘attlable.option_id’)
);
$attributes->getSelect()
->join(
array(‘attlableval’=>’eav_attribute_option_value’),
‘attlable.option_id = attlableval.option_id’,
array(‘*’)
)
->where(‘attlableval.value like (?)’,$prod_name.’%’)
->where(‘attlableval.store_id =’.$store_id);

$data = $attributes->getData();
// echo “<pre>”;print_r($data);exit;
$result=array();
foreach($data as $sdata)
{
$result[]=array(“data” => $sdata[‘value’],”id” => $sdata[‘option_id’]);
}
$this->getResponse()->setBody(Mage::helper(‘core’)->jsonEncode($result));

Advertisements

How to get configurable product options and attributes

$productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
$attributeOptions = array();
foreach ($productAttributeOptions as $productAttribute) {
foreach ($productAttribute[‘values’] as $attribute) {
$attributeOptions[$productAttribute[‘label’]][$attribute[‘value_index’]] = $attribute[‘store_label’];
}
}
echo “<pre>”;print_r($attributeOptions);

Add Multiple products into cart simultaneously in Magento

Here, I am going to explain how to add multiple products into cart simultaneously.
Step 1: Create namespace
First thing’s first, we have to create our namespace XML. In app/etc/modules, create a file called Namespace_AddMultipleProducts.xml, and put this inside of it:

<?xml version=”1.0″?>
<config>
<modules>
<Namespace_AddMultipleProducts>
<active>true</active>
<codePool>local</codePool>
</Namespace_AddMultipleProducts>
</modules>
</config>

Step 2: Create directory structure
Now we create the directories where our code will sit. Under app/code/local, create this directory structure:

app/code/local/Namespace
app/code/local/Namespace/AddMultipleProducts
app/code/local/Namespace/AddMultipleProducts/controllers
app/code/local/Namespace/AddMultipleProducts/etc

Step 3: Create our module’s configuration

Step 1 we told Magento that we made a module, and where it is. Now we have to tell it what to do. In app/code/local/Namespace/AddMultipleProducts/etc, create a file called config.xml, and put this inside of it:

<?xml version=”1.0″ encoding=”UTF-8″?>
<config>
<modules>
<Namespace_AddMultipleProducts>
<version>1.0</version>
</Namespace_AddMultipleProducts>
</modules>
<frontend>
<routers>
<addmultipleproducts>
<use>standard</use>
<args>
<module>Namespace_AddMultipleProducts</module>
<frontName>multiadd</frontName>
</args>
</addmultipleproducts>
</routers>
</frontend>
</config>

Step 4: Create the controller that will handle our request

Finally, we have to put the PHP code in a controller which contains the logic of systematically adding multiple products to the cart at the same time. In app/code/local/Namespace/AddMultipleProducts/controllers, create a file called AddController.php (watch the CaSe here!), and put this inside of it:

$<?php
class Namespace_AddMultipleProducts_AddController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$products = $this->getRequest()->getParam(‘products’);
$cart = Mage::getModel(‘checkout/cart’);
$cart->init();
/* @var $pModel Mage_Catalog_Model_Product */
foreach ($products as $product_id) {
if ($product_id == ”) {
continue;
}
$pModel = Mage::getModel(‘catalog/product’)->load($product_id);
if ($pModel->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) {
try {
$cart->addProduct($pModel, array(‘qty’ => ‘1’));
}
catch (Exception $e) {
continue;
}
}
}
$cart->save();
if ($this->getRequest()->isXmlHttpRequest()) {
exit(‘1’);
}
$this->_redirect(‘checkout/cart’);
}

}

Step 5: Add checkbox and submit button in list file
In app/design/frontend/[your_package]/[your_theme]/template/catalog/product/list.phtml, add form tag at starting of file and close form tag at end of file.

For example,

<form name=”multiadd” action=”<?php echo Mage::getBaseUrl().’multiadd/add/’?>”>
Your List File Code….
</form>

Now, add checkbox just after add to cart button in list as well as grid mode in List file.

<input type=”checkbox” value=”<?php echo $_product->getId()?>” name=”products[]” id=”products-<?php echo $_product->getId()?>” onclick=”SetButtonStatus(‘products-<?php echo $_product->getId()?>’)”/>

Now, add submit button just before close form tag </form> in List file..

<button type=”submit” title=”<?php echo $this->__(‘Multiple Add to Cart’) ?>” id=”btnButton” disabled=”disabled”><span><span><?php echo $this->__(‘Multiple Add to Cart’) ?></span></span></button>

At last, add following code at bottom of List file.

<script type=”text/javascript”>
function SetButtonStatus(sender)
{
var list, index, item, checkedCount;

checkedCount = 0;
list = document.getElementsByTagName(‘input’);
for (index = 0; index < list.length; ++index)
{
item = list[index];
if (item.getAttribute(‘type’) === “checkbox”
&& item.checked
&& item.name === “products[]”)
{
++checkedCount;
}
}

if(document.getElementById(sender).checked == 1)
{
document.getElementById(‘btnButton’).removeAttribute(‘disabled’);
}
else
{
if(checkedCount > 0)
{
document.getElementById(‘btnButton’).removeAttribute(‘disabled’);
}
else
{
document.getElementById(‘btnButton’).setAttribute(‘disabled’, ‘disabled’);
}
}
}

</script>

Note: This will only work with Simple Products, as there is no real way of knowing what the options are for other products type.

Thats it!!!

Get All the Data from IP Address

Get all the data from IP address, add following code

$gdata = file_get_contents('http://j.maxmind.com/app/geoip.js');
echo "<pre>";print_r($gdata);

From this, get only country add following code

$gdata1 = explode ("{ return", $gdata);
$gdata2 = explode ("'", $gdata1[2]);
echo "<pre>"; echo ($gdata2[1]);

Thats it!!!

Add Custom Pagination in Magento

Add following code in block PHP file.

protected $_pagesCount = null;
protected $_currentPage = null;
protected $_itemsOnPage = 10;
protected $_itemsLimit;
protected $_pages;
protected $_displayPages   = 5;

protected function _construct()
{
$this->_currentPage = $this->getRequest()->getParam(‘page’);
if (!$this->_currentPage) {
$this->_currentPage=1;
}

$itemsPerPage = 5;
if ($itemsPerPage > 0) {
$this->_itemsOnPage = $itemsPerPage;
}
}

public function getNewsList()
{
$collection = Mage::getModel(‘news/news’)->getCollection();

if ($this->_itemsLimit!=null && $this->_itemsLimit<$collection->getSize()) {
$this->_pagesCount = ceil($this->_itemsLimit/$this->_itemsOnPage);
} else {
$this->_pagesCount = ceil($collection->getSize()/$this->_itemsOnPage);
}
for ($i=1; $i<=$this->_pagesCount;$i++) {
$this->_pages[] = $i;
}
$this->setLastPageNum($this->_pagesCount);

$offset = $this->_itemsOnPage*($this->_currentPage-1);
if ($this->_itemsLimit!=null) {
$_itemsCurrentPage = $this->_itemsLimit – $offset;
if ($_itemsCurrentPage > $this->_itemsOnPage) {
$_itemsCurrentPage = $this->_itemsOnPage;
}
$collection->getSelect()->limit($_itemsCurrentPage, $offset);
} else {
$collection->getSelect()->limit($this->_itemsOnPage, $offset);
}
return $collection;

}

public function isFirstPage()
{
if ($this->_currentPage==1) {
return true;
}
return false;
}

public function isLastPage()
{
if ($this->_currentPage==$this->_pagesCount) {
return true;
}
return false;
}

public function isPageCurrent($page)
{
if ($page==$this->_currentPage) {
return true;
}
return false;
}

public function getPageUrl($page)
{
return $this->getUrl(‘*’, array(‘page’ => $page));
}

public function getNextPageUrl()
{
$page = $this->_currentPage+1;
return $this->getPageUrl($page);
}

public function getPreviousPageUrl()
{
$page = $this->_currentPage-1;
return $this->getPageUrl($page);
}

public function getPages()
{
$collection = Mage::getModel(‘news/news’)->getCollection();
$pages = array();
if ($this->_pagesCount <= $this->_displayPages) {
$pages = range(1, $this->_pagesCount);
}
else {
$half = ceil($this->_displayPages / 2);
if ($this->_currentPage >= $half && $this->_currentPage <= $this->_pagesCount – $half) {
$start  = ($this->_currentPage – $half) + 1;
$finish = ($start + $this->_displayPages) – 1;
}
elseif ($this->_currentPage < $half) {
$start  = 1;
$finish = $this->_displayPages;
}
elseif ($this->_currentPage > ($this->_pagesCount – $half)) {
$finish = $this->_pagesCount;
$start  = $finish – $this->_displayPages + 1;
}

$pages = range($start, $finish);
}
return $pages;
//return $this->_pages;
}

 

Now , add following code into phtml file.

 

<table>
<?php foreach($this->getNewsList() as $item): ?>
<tr>
<td><img src=”<?php echo Mage::getBaseUrl(‘media’) . ‘news’. ‘/’ . $item[‘filename’] ?>” alt=”<?php echo $item[‘title’]
?>” width=”64″ height=”64″ style=”border: 1px solid
#d5d5d5;padding:3px;margin-right: 10px;” /></td>
<td>
<h5><?php echo $item[‘title’] ?></h5>
<p><?php echo $this->limitCharacter($item[‘content’], 180, ”
<a href='”.Mage::getBaseUrl().”news/index/view/id/”.$item[‘news_id’].”‘>read more</a>”)?></p>
</td>
</tr>
<?php endforeach; ?>
</table>

<?php if($this->getLastPageNum()>1): ?>
<div>
<p>
<?php if (!$this->isFirstPage()): ?>
<a href=”<?php echo $this->getPreviousPageUrl() ?>”><?php echo $this->__(‘Previous’) ?></a>
<?php endif;?>
<?php foreach ($this->getPages() as $_page): ?>
<?php if ($this->isPageCurrent($_page)): ?>
<span><?php echo $_page ?></span>
<?php else: ?>
<a href=”<?php echo $this->getPageUrl($_page) ?>”><?php echo $_page ?></a>
<?php endif ?>
<?php endforeach; ?>
<?php if (!$this->isLastPage()): ?>
<a href=”<?php echo $this->getNextPageUrl() ?>”><?php echo $this->__(‘Next’) ?></a>
<?php endif ?>
</p>
</div>
<?php endif;?>

 

Thats it!!!

Hide Bundle Product Price From Product View Page in Magento

Put following code in view.phtml

<?php
if($this->hasOptions() || $this->getTypeId() == ‘bundle’)
{?>
<script>
$$(‘div.product-info .price-box’).invoke(‘setStyle’, { display: ‘none’ });
</script>
<?php } ?>

 

Thats it!!!

How To Calculate Facebook Like, Twitter, Pin It and Google Plus Count With PHP

For Facebbok Like count,

$url = “http://allfacebook.com/calculate-facebook-like_b44023&#8221;;
$json_string = file_get_contents(‘http://api.facebook.com/restserver.php?method=links.getStats&urls=&#8217;.$url);
$xml = (array)simplexml_load_string($json_string);
print_r($xml);

 

For Twitter count,

$url = “http://blog.chapagain.com.np/magento-send-confirmation-email-on-new-customer-registration/&#8221;;
$json_string = file_get_contents(‘http://cdn.api.twitter.com/1/urls/count.json?url=&#8217;.$url.’&callback=?%27&callback=?&format=json’);
$json = json_decode($json_string, true);
print_r($json);

 

For Pin It count,

$url = “http://oss1.magentoprojects.net/fablogue/jewelry/caviar-ring.html&#8221;;
$json_string = file_get_contents(‘http://api.pinterest.com/v1/urls/count.json?callback=&url=&#8217;.$url);
$json = json_decode(str_replace(‘)’,”,str_replace(‘(‘,”,$json_string)), true);
print_r($json);

 

For Google Plus count,

$url = “http://blog.chapagain.com.np/magento-send-confirmation-email-on-new-customer-registration/&#8221;;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, “https://plusone.google.com/_/+1/fastbutton?bsv&size=tall&hl=it&url=&#8221;.urlencode($url));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$html = curl_exec ($curl);
curl_close ($curl);
$doc = new DOMDocument();
$doc->loadHTML($html);
$counter=$doc->getElementById(‘aggregateCount’);
echo $counter->nodeValue;

 

Thats it!!