Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Wednesday, 16 December 2015

How to use helpers in magento ?

In magento helpers is very useful functionality.Basically we can call helper functions anywhere in the site .

Each module has its own helper function

like in magento you use a below cart helper function to get cart url 

Mage::helper('checkout/cart')->getCartUrl()

the above helper function is created at below location

app/code/core/Mage/Checkout/Helper/Cart.php 

Lets create one new helper function in the end of Cart.php 

let say we need a function in which we need to get all ordered product items data 

 
For this i have create a new function as below

public function getordered_products_data($order_id){
        $cart =  Mage::getModel('sales/order')->load($order_id);
        print_r($cart->getData());die;
        foreach ($cart->getAllItems() as $item) {
        $loadproduct =     Mage::getModel('catalog/product')->load($item->getProduct()->getId());
        print_r($loadproduct->getData());
        }

}


and now we can use this function anywhere in the site as below

Mage::helper('checkout/cart')->getordered_products_data($your_order_id); // pass your order id here

and with this you can see all products data in order anywhere in the site by using above function.

thanks and hope it will help the magento comunity 


Tuesday, 13 May 2014

How to check url contains http or htttps in php

Hi with the following code you can get the protocol of the server  
 
 
 
if (isset($_SERVER['HTTPS']) &&
    ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
    isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
    $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
  $protocol = 'https://';
}
else {
  $protocol = 'http://';
}

Wednesday, 16 April 2014

Get all images tags from the remote url through php

In some project i need to get all image tags from the remote url
I have google a lot but not getting any particular solution.
But atlast i got a very good code which works like a charm

you can find that code on

https://docs.google.com/file/d/0B9cbYvYQ9b0YY2xxUW5kUms0NWM/edit

in this file you will see the below code at very last of file


// Create DOM from URL or file
$html = file_get_html('https://www.google.co.in/'); // replace your url

// Find all images
foreach($html->find('img') as $element) // to get all image src  from the url
       echo $element->src . '<br>';

// Find all links
foreach($html->find('a') as $element) // to get all anchor tags  from the url
       echo $element->href . '<br>';


with this script you can get any paramaeter from the remote url.  Remember to copy the full file from the downlaod file from here






      

Friday, 13 December 2013

get custom options programatically Magento

Hi with the below code you can get the product custom  option attributes all data


$product = Mage::getModel("catalog/product")->load(1); //product id 1
$i = 1;
echo "<pre>";
foreach ($product->getOptions() as $o) {
    echo "<strong>Custom Option:" . $i . "</strong><br/>";
    echo "Custom Option TITLE: " . $o->getTitle() . "<br/>"; //Colors
    echo "Custom Option TYPE: " . $o->getType() . "<br/>"; //drop_down
    echo "Custom Option Values: <br/>";
    $values = $o->getValues();
    foreach ($values as $v) {
        print_r($v->getData());
    }
    $i++;
    echo "<br/>";
}

Tuesday, 5 November 2013

submit multiplr forms on single click

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#submitButton").click(function () {
                $.post($("#form1").attr("action"), $("#form1").serialize(),
                  function () {
                      alert('Form 1 submitted');
                  });

                $.post($("#form2").attr("action"), $("#form1").serialize(),
                  function () {
                      alert('Form 2 submitted');
                  });
            });
        });
    </script>
</head>
<body>
    <form id="form1" action="#" method="post">
        <input type="button" id="submitButton" value="Submit" />
    </form>

    <!-- Hidden form -->
    <form id="form2" action="#" method="post">
        <input type="text" id="data1" value="testing1" />
        <input type="text" id="data2" value="testing2" />
    </form>
</body>
</html>

Sunday, 25 August 2013

get odd even in forloop php

<?php $i  = 1;
$range = range(0,10);
foreach($range as $range1){
    $rangenew = $i++ ;
if ($rangenew % 2 == 0) {
  echo  $rangenew."It's even".'</br>';  
    }
else{
  
    echo  $rangenew."It's odd".'</br>';  
    }
}

?>

Thursday, 31 January 2013

skip entry if already exist Mysql

Hi we can easily do this with the use of single parameter  ignore
 after insert

like

$insert1= mysql_query("INSERT IGNORE INTO your_table_name(your column name,..) VALUES('your values')",$conn1);

this will help you to skip a entry if already exist in table and continue after that with other entries

Friday, 18 January 2013

Print star with php using loops

  
You can print any shape with php like i have create a simple star shape shown below by using loops




<?php $nbsp = "*";
      $a = 100;
      $b = 0 ;
       $e = 10 ;
      for($i=0; $i<=10;$i++){ ?>
    <div ><div style = "text-align:center;width:<?php echo $a++ ;?>px;"><?php echo str_repeat($nbsp,$b++) ;?></div></div>
     <?php } ?>
<?php
           for($i=0; $i<=10;$i++){ ?>
    <div ><div style = "text-align:center;width:<?php echo $a++ ;?>px;"><?php echo str_repeat($nbsp,$e--) ;?></div></div>
     <?php } ?>

Tuesday, 15 January 2013

Get product quantity in magento

with this code you can get the product quantity of a product           

   
 $qty = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();

echo   $qty ;

You can use this quantity also to show almost gone or soldout image for a product on category or product view page.

need to place in condition like this

<?php if($qty<10){ ?>
  show almost gone image...
    <?php }elseif ($qty<1) {?>
       show soldout image.............
    <?php } ?>

Monday, 14 January 2013

Get custom attribute value in magento admin orders

with this code you can get any custom attribute value in magento admin order section , you can show only in admin or in frontend depends upon your need
   

 <div style="color: black; font-size: 14px; font-weight: bold;">proviser <span><?php $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_item->getSku(), array('provider')); echo $product->getprovider(); ?>  </span>  </div>

in above code just replace your attribute code which you specify in admin for that like for my case i use "provider "
just replace provider with your attribute code and place this in your 

/public_html/app/design/adminhtml/default/default/template/sales/items/column/name.phtml file   

Show custom error messages on cart page

Hi in this article i will try to tell you all about how to show custom error messages or popups on cartpage

in the below folder
app/code/core/Mage/Checkout/controllers/CartController.php

near about line 537 you will find code something like this

 else {
                    $this->_getSession()->addError(
                        $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->htmlEscape($couponCode))
                    );
                }




which contains a error message for wrong coupon code apply

which i have change to

else {
                    $this->_getSession()->addError(
               
                        $this->__('Coupon code "%s" is not valid.', Mage::helper('core')->htmlEscape($couponCode))
                    );
                    $myValue = 'Hello World';
                   Mage::getSingleton('customer/session')->setMyvalue($myValue);
                                              
                }



in this i have make use of magento sessions , i have set a session as setMyvalue
for the wrong code apply and than check the value of this in cart file

cart file path = under your theme /template/checkout/cart.phtm
i have retrieve the session value something like

 <?php $myValue=  Mage::getSingleton('customer/session')->getMyvalue(); ?> 
<?php if ($myValue!= ''){ ?>
do something:: you can do anything here like show your html ,js popups.....
<?php } else {?>
do somethindo something:: you can do anything here like show your html ,js popups.....
<?php }?>  

thats it ......


Get store code in Magento

$storeCode = Mage::app()->getStore()->getCode();
echo $storeCode;

Thursday, 10 January 2013

Get all cms pages in header dynamically Magento

  Place this code in header file below topcontainer

  <?php $collection = Mage::getModel('cms/page')->getCollection()->addStoreFilter(Mage::app()->getStore()->getId());?>
<?php  $collection->getSelect()
->where('is_active = 1'); ?>
<ul id="nav">
<?php foreach ($collection as $page): ?>
<?php $PageData = $page->getData(); ?>
<?php// print_r($PageData);?>
<?php if($PageData['identifier']!='no-route' && $PageData['identifier']!='enable-cookies' && $PageData['identifier']!='home2') { ?>
<li>
<a href="/magento/index.php/<?php echo $PageData['identifier']?>"><span><?php echo $PageData['title'] ?></span></a>
</li>
<?php } ?>
<?php endforeach; ?>
</ul>


Note change this part of code "/magento/index.php/" according to your domain 

Wednesday, 9 January 2013

code to show category images on custom phtml files and link to their category pages

<?php

 $root_category = Mage::getModel('catalog/category')->load(2); // Put your root category ID here. like 2
$subcategories = $root_category->getChildren();
foreach(explode(',',$subcategories) as $subcategory) {
$category = Mage::getModel('catalog/category')->load($subcategory);
echo'<pre>';
//print_r($category);
echo  $category->getImageUrl(); //place this url in your image tag to show the image

  }

  ?>