Saturday 4 November 2017

How to Rewrite / Override block in magento

How to Rewrite / Override block in magento

Here we are Overriding / Rewriting magento block of Mage_Catalog_Block_Product_View with custom module named Chand_Software.

(1) Create module file in app/etc/modules/Chand_Software.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Chand_Software>
            <active>true</active>
            <codePool>local</codePool>
        </Chand_Software>
    </modules>
</config>

(2) Create module config file in app/code/local/Chand/Software/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Chand_Software>
            <version>1.0.0</version>
        </Chand_Software>
    </modules>
    <global>
        <blocks>
            <catalog>  // This is the block identifier which we want rewrite
                <rewrite>
                    <product_view>Chand_Software_Block_Product_View</product_view>
                </rewrite>
            </catalog>
        </blocks>
    </global>
</config>

(3) Now Create block file of our custom module in app/code/local/Chand/Software/Block/Product/View.php

class Chand_Software_Block_Product_View extends Mage_Catalog_Block_Product_View
{
    /**
     * Put your logic here
     *
     */
}

How to add css in only product type simple in magento

How to add css in only product type simple in magento

First to create local.xml file at this path app/design/frontend/yourpackage/default/layout/local.xml and paste below code in local.xml

<?xml version="1.0"?>

<layout version="0.1.0">

    <PRODUCT_TYPE_simple>
   
            <reference name="head">
   
                <action method="addCss"><stylesheet>css/your_simple.css</stylesheet></action>
   
            </reference>
   
    </PRODUCT_TYPE_simple>

</layout>

Handle of different product types :

<PRODUCT_TYPE_simple>
<PRODUCT_TYPE_configurable>
<PRODUCT_TYPE_grouped>
<PRODUCT_TYPE_virtual>
<PRODUCT_TYPE_downloadable>
<PRODUCT_TYPE_bundle>

How to make timer that not reset when page refresh javascript

<form name="counter">
    <input type="text" size="8" name="jimmy" id="counter">
</form>

<script type="text/javascript">
function getCookie(cname) {
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.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 "";
}

var cnt = 60;
function counter(){
    if(getCookie("cnt") > 0){
  cnt = getCookie("cnt");
 }
 cnt -= 1;
 document.cookie = "cnt="+ cnt;
 jQuery("#counter").val(getCookie("cnt"));

 if(cnt>0){
  setTimeout(counter,1000);
 }

}

counter();
</script>

How to add css and js file in head section opencart


Include Css file code:

$this->document->addStyle('catalog/view/theme/themename/stylesheet/product.css');


Include Js file code:

$this->document->addScript('catalog/view/theme/themename/js/product.js');

How to append GET parameters in url PHP

First make one function like below

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

$param = GET;

function filter_url($url,$param){

    $parsed = parse_url($url);
    $query = str_replace("&amp;","&",$parsed['query']);
    parse_str($query,$params);
    unset($params[$param]);
    $new_query = http_build_query($params);
    $newUrl = $parsed['scheme']."://".$parsed['host'].$parsed['path'];
    if($new_query){
        $newUrl .= "?".$new_query;
    }
    return $newUrl;

}