Monday 28 September 2015

Difference between Echo And Print In Php

Hello,

Here I can Give both of use echo and print in php. I give example and use.

Echo and Print are almost same. they are both output on screen. they are used with and without
parentheses.

Difference Echo And Print



  1. print return value and echo not return value.
  2. echo faster than print.
  3.  echo without parentheses can take multiple parameters, which get concatenated but print give error.

How Can Use.



$data = "hello".
$data1 ="jaydip";

echo "<h2>My echo information</h2>";
echo "namste !<br>";

echo $data."".$data1;


echo without parentheses can take multiple parameters 
echo "jj","123",1,3;

same like in Print:

$data = "hello".
$data1 ="jaydip";

print "<h2>My echo information</h2>";
print "namste !<br>";

print $data."".$data1;

you can use as print in 
$test=1;
($test==2) ? print "true" : print "false";


Thursday 24 September 2015

Filtering a Magento product collection by multiple categories and Stock item and filter current product in product detail page

Hello,

I  Want to create section to get same product of different categories of products with Stock or quantity.

after i want get product of minimum of product quantity 50%.

for that i will join 'cataloginventory/stock_item', and  'catalog/category_product'  get this collection.

this section create in product detail page that why i am filter current product using it product id.

Please Below Code it help to you.

$product_id = Mage::registry('current_product')->getId();
    $category_ids = $_product->getCategoryIds();
$collection = Mage::getModel('catalog/product')->getCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('entity_id', array('neq' => $product_id))
 ->addAttributeToFilter('status', 1)
 ->addStoreFilter();
$conditions = array();
foreach ($category_ids as $categoryId) {
 if (is_numeric($categoryId)) {
   
  $conditions[] = "{{table}}.category_id = $categoryId";
 }
}
$collection->distinct(true)
->joinField('category_id', 'catalog/category_product', null, 'product_id = entity_id', implode(" OR ", $conditions), 'inner');
$collection->joinField(
    'qty',
    'cataloginventory/stock_item',
    'qty',
    'product_id=entity_id',
    '{{table}}.stock_id=1',
    'left'
);
 foreach ($collection as $_item) {
   
    //echo $_item->getName()."</br>";
     $product_qty = $_item->getQty();
     $product_qty = round($product_qty);
    $product_qty = $product_qty/100;
    if($product_qty >= 0.5){
       
        echo $_item->getName()."</br>";
       
        }
       
 }

Please Below code it help getting products.

Thursday 17 September 2015

Price LessThan 1000 Get Products in Magento

Hello,

Here I am Create Custom Query for Product list. I want to get all product that price
less than 1000 in magento. I am Giving Example for that

First Create Cms page goto Cms=>page

in cms page
goto design=>Layout Update XML

Write below code in xml

<reference name="root">
<action method="setTemplate"><template>page/2columns-left.phtml</template></action>
 <action method="unsetChild"><alias>breadcrumbs</alias></action>    
    <block type="page/html_breadcrumbs" name="breadcrumbs" as="breadcrumbs">
        <action method="addCrumb">
            <crumbName>Home</crumbName>
            <crumbInfo><label>Home</label><title>Home</title><link>/</link></crumbInfo>
        </action>
        <action method="addCrumb">
            <crumbName>Custom Page</crumbName>
            <crumbInfo><label>1000 Under</label><title>1000 Under</title></crumbInfo>
        </action>      
    </block>
</reference>

<reference name="left">
     <block type="catalog/navigation" name="catalog.categorymenu" before="catalog.leftnav" template="catalog/product/categorymenu.phtml"/>

        </reference>
<reference name="content">
<block type="catalog/product_new" name="product_list" template="catalog/product/list.phtml">
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
</block>
</reference>

after create block in your app\code\local\Mage\Catalog\Block\Product\new.php

Write this code for that

<?php

class Mage_Catalog_Block_Product_New extends Mage_Catalog_Block_Product_List 
   function get_prod_count() 
   { 
      //unset any saved limits 
      Mage::getSingleton('catalog/session')->unsLimitPage(); 
      return (isset($_REQUEST['limit'])) ? intval($_REQUEST['limit']) : 12;//set your page limit 
   }// get_prod_count 

   function get_cur_page() 
   { 
      return (isset($_REQUEST['p'])) ? intval($_REQUEST['p']) : 1; 
   }// get_cur_page 

   /** 
    * Retrieve loaded category collection 
    * 
    * @return Mage_Eav_Model_Entity_Collection_Abstract 
   **/ 
   protected function _getProductCollection() 
   { 
      

      $collection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSelect('*')
                        ->addPriceData()
                        ->setOrder('price', 'ASC')
                        ->addAttributeToFilter('price', array('lt' => '1001'))
                        ->addFieldToFilter('status', array('eq' => '1')
                    );

      $this->setProductCollection($collection); 

      return $collection; 
   }// _getProductCollection 
}// Mage_Catalog_Block_Product_New 

?>



Tuesday 15 September 2015

Magento Category Product Listing Sort by Most Viewed Product

Hello,

You Just want to orverride app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php

paste in code\local\Mage\Catalog\Block\Product\List\Toolbar.php

here 


first add your most viewed in sort by product select box


 public function getAvailableOrders()

    {

       $this->addOrderToAvailableOrders('mostviewd', 'MOST POPULAR');

        return $this->_availableOrder;\\you can add this code here
    }


 public function setCollection($collection)

    {

if ($this->getCurrentOrder()) {

               if($this->getCurrentOrder()=='mostviewd') {
              $this->_collection->getSelect()->
joinInner('report_event AS _table_views',
' _table_views.object_id = e.entity_id',
'COUNT(_table_views.event_id) AS views')->
group('e.entity_id')->order('views DESC');
       
    
            }

}


}

add this code and check the result.


Monday 14 September 2015

Magento Product View Count in Product List Page and Product View Page

Hello

Some Time You Want to display how many time product view count in magento


I am Give Simple Code it can be used in both product list page it can paste in loop then

it will display every product totoal view count. and product view page current product view


$id = $id=$_helper->productAttribute($_product, $_product->getId(), 'id');


$mostviewedProducts = Mage::getResourceModel('reports/product_collection')->addViewsCount();

            foreach($mostviewedProducts as $product) {
if($product->getData('entity_id')==$id)
{
 
    echo $id."<br/>";
    echo $_product->getId()."<br/>";
   echo $product->getData('entity_id')."<br/>";
    echo  "Total View Count: " . $product->getData('views');
}}?>


You can add time duration in

$fromDate = '2013-12-10';
$toDate   = now();

$id = $id=$_helper->productAttribute($_product, $_product->getId(), 'id');


$mostviewedProducts = Mage::getResourceModel('reports/product_collection')->addViewsCount($fromDate,$toDate);

            foreach($mostviewedProducts as $product) {
if($product->getData('entity_id')==$id)
{
 
    echo $id."<br/>";
    echo $_product->getId()."<br/>";
   echo $product->getData('entity_id')."<br/>";
    echo  "Total View Count: " . $product->getData('views');
}}?>


Sunday 13 September 2015

Get Most view products of current product categories in product detail page(view.phtml) in magento

Hello

Here Qurery for Create Most View product of current product of categories

This Section Query used Most view Product view in magento. it's difficult to get because

I am seaching but to get proper result. after i get solution from different different reference

site. i am will give proper guide for that. it success to use it.

    $storeId = Mage::app()->getStore()->getId();
            $category_ids = $_product->getCategoryIds();
            /** date range product view count */
            $fromDate=date('Y-m-d',strtotime("yesterday"));
            $toDate   = now();
           $product_id = Mage::registry('current_product')->getId();

$products = Mage::getResourceModel('reports/product_collection')
    ->addAttributeToSelect('*')
    ->addViewsCount()
    ->joinField('category_id',
    'catalog/category_product',
    'category_id',
    'product_id=entity_id',
    null,
    'left'
)
    ->setStoreId($storeId)
    ->addStoreFilter($storeId)
    ->setPageSize(5)//set the pae size
    ->addAttributeToFilter('category_id', array('in' => $category_ids ));

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);


here one problem you face if current product detail page in this section then you have

I have issue when I go to  section on product detail page; the current product is also getting displayed on this. I just need to remove the current product from  section.

it's soultion is that without change query

foreach($products as $_item){

    $myid =  $_item->getId();
 

    if ($myid !=  $product_id ){

<?php echo $_item->getProductUrl() ?>

}

}

Wednesday 9 September 2015

When On Particular Category adding a product in admin side then product name should be created automatically using product attributes like weight,sizing Magento

Hello,

I am Adding Product Admin Side. But When Particular category then it can be automatically
generate Product name. like it's Attributes. I Give Reference for that I give Example For That.

Create Simple Module.

app\etc\modules


Create Easylife_Meta.xml Here.

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Meta>
            <codePool>local</codePool>
            <active>true</active>
            <depends>
                <Mage_Catalog />
            </depends>
        </Easylife_Meta>
    </modules>
</config>

app\code\local\Easylife\Meta\etc

Create config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Meta>
            <version>0.0.1</version>
        </Easylife_Meta>
    </modules>
    <global>
        <models>
            <easylife_meta>
                <class>Easylife_Meta_Model</class>
            </easylife_meta>
        </models>
    </global>
    <adminhtml>
        <events>
    <catalog_product_save_before>
        <observers>
            <create_name>
                <class>easylife_meta/observer</class>
                <method>createName</method>
            </create_name>
        </observers>
    </catalog_product_save_before>
</events>
    </adminhtml>
</config>

\app\code\local\Easylife\Meta\Model

Create File Observer.php

<?php


class Easylife_Meta_Model_Observer
{
    public function createName($observer)
    {
        
        $product = $observer->getEvent()->getProduct();
        
        
      
      
       $data = $observer->getEvent()->getCategory();
       
       
        $product_category_id = $product['category_ids'];
        $productColor = $product->getWeight('weight');
        $productsizing = $product->getAttributeText('sizing');
        
       if (!empty($product_category_id)) {
        
        if (in_array("9", $product_category_id)){ // Here 9 in My Category ID
            
            $productSku = $productColor." ".$productsizing;
            $product->setName($productSku);
            
        }
        
        }
    }
}

Here You can generate name for particular category. you can also change all product remoce if
contion  $product->setName($productSku); set here your value. here 9 is my category
you can check your category id then check it.

Monday 7 September 2015

Magento Admin Password Reset in Localhost or Local Machine (Magento Password Reset is not Working)


Hello,

How To Reset Magento Password In Localhost and Magento Site Using Phpmyadmin

I am changing Magento password reset. I Follow The tutorial but in my magento it's not working.

Simple How to reset Password in magento Check this.

You can reset your Magento admin password directly through the database related to your website.

You can use Phpmyadmin Tool.

UPDATE `admin_user` SET `password` = MD5('NEWPASSWORD') WHERE `username` = 'ADMINUSERNAME';

some time you don't know your magento database then you can check you database

/app/etc/local.xml


<![CDATA[test_magento]]> =>//it's your database name.

you can reset your password using php myadmin ui 

check out below step 


Step 1:



Step :2 





Step 3:





I am Set this but some time it's not Working it make issue in localhost

You can remove Cache and session Folder Var Folder

otherwise not woking then 
follow below Step

app/code/core/Mage/Core/Model/Session/Abstract/Varien.php

$cookieParams = array(
            'lifetime' => $cookie->getLifetime(),
            'path'     => $cookie->getPath() //,
            // 'domain'   => $cookie->getConfigDomain(),
            // 'secure'   => $cookie->isSecure(),
            // 'httponly' => $cookie->getHttponly()
        );

commet this display in code.




Sunday 30 August 2015

Jquery Popup Not Display If Use Visit Site

Hello

Here Example Of how to create simple popup using jquery. but new things that if you have to add
functionality if user visited site  then not display popup. if user in site then i can be see popup.

For this simple example how to create popup with jquery and if have to add functionality to only one
time see the popup and it will display after some time. here 5 second time out session.

Please check Below Code.

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>
<script type="text/javascript">
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
}

$(document).ready(function(){
   
   
     var popup = getCookie("newpopup");
     alert(popup);
   
    if(popup == null || popup == undefined || popup == ""){
     setTimeout(function () {
           
          $.cookie('newpopup',"true");
            $(".popup").show();
          }, 5000);
    }else{
        $(".popup").hide();
    }
    $(".close_button").click(function(){
        $(".popup").hide();
    });
   
   
});

</script>
<style>
.popup {
    background: rgba(0, 0, 0, 0.5) none repeat scroll 0 0;
    height: 100%;
    position: fixed;
    text-align: center;
    width: 100%;
    z-index: 99999999;
}
</style>

<div>
<div class="popup" style="display: none;">

    <div class="popupbox">
    <div class="close_button"><a href="javascript:void(0)">Close</a></div>
<h1>This is our popup</h1>
<ul>
<li><a href="https://twitter.com/" >twitter</a></li>
<li><a href="https://www.facebook.com/" >twitter</a></li>
<li><a href="https://in.pinterest.com/" >facebook</a></li>
<li><a href="https://instagram.com/">instagram</a></li>
</ul>
</div>
</div>
</div>

Friday 28 August 2015

Use Ternary Operator in Php

Hello

I am Here Create Some Example How to use Ternary operator in php.

Ternary Operator use as simple if and else condition

$bool_val = 5;

echo ($bool_val <= 5) ? 'true' : 'false';.

it result is true.

if $bool_val = 6. then out put will be different. it false.

before ? it's like if condition  and : else condition.

For Check this

<?php
$age = 18;
    $agestr = ($age < 18) ? 'child' : 'adult';

it same like this

<?php
      if ($age < 16) {
        $agestr = 'child';
    } else {
        $agestr = 'adult';
    }
 

?>

here

another example


        $valid = false;
    $lang = 'french';
    $x = $valid ? ($lang === 'french' ? 'oui' : 'yes') : ($lang === 'french' ? 'non' : 'no');
 
    echo $x;

like this

if($valid){
   
    if($lang === 'french')
    {
        $x='oui';
    }else{
        $x = 'yes';
    }
} else{
   
      if($lang === 'french')
    {
        $x='non';
    }else{
        $x = 'no';
    }
   
}
echo $x;