Pages

Tuesday, June 25, 2013

How to hide shopping cart sidebar when it is empty

Here is how you could hide the shopping cart side bar when it is empty:
1. Open: app/design/frontend/default/<your template>/template/checkout/cart/sidebar.phtml
2. Go to line 32 Look for
<div class="box base-mini mini-cart">
3. Add these lines right before that opening div tag
<?php $_cartQty = $this->getSummaryCount() ?>
<?php if ($_cartQty >0): ?>
4.Add this line to the bottom of the file
<?php endif ?>
That is it your are all set, the shopping cart will only show if user adds items to their shopping cart.

Programmatically creating a coupon (including the rule) with conditions in Magento

<?php
    // load product
    /** @var Mage_Catalog_Model_Product $product */
    $product = Mage::getModel('catalog/product')
                    ->setStoreId($storeId)
                    ->load($productId);

    // set length of coupon code
    /** @var Mage_SalesRule_Model_Coupon_Codegenerator $generator */
    $generator = Mage::getModel('salesrule/coupon_codegenerator')
                        ->setLength(8);
    /** @var Mage_SalesRule_Model_Rule_Condition_Product $conditionProduct */
    $conditionProduct = Mage::getModel('salesrule/rule_condition_product')
                                                ->setType('salesrule/rule_condition_product')
                                                ->setAttribute('sku')
                                                ->setOperator('==')
                                                ->setValue($product->getSku());
                                               
    /** @var Mage_SalesRule_Model_Rule_Condition_Product_Found $conditionProductFound */
    $conditionProductFound = Mage::getModel('salesrule/rule_condition_product_found')
                                            ->setConditions(array($conditionProduct));
    /** @var Mage_SalesRule_Model_Rule_Condition_Combine $condition */
    $condition = Mage::getModel('salesrule/rule_condition_combine')
                    ->setConditions(array($conditionProductFound));
                                               
    /** @var Mage_SalesRule_Model_Coupon $coupon */
    $coupon = Mage::getModel('salesrule/coupon');
    // try to generate unique coupon code
    $attempts = 0;
    do {
        if ($attempts++ >= 8) {
            Mage::throwException(Mage::helper('mymodule')->__('Unable to create requested Coupons. Please try again.'));
        }
        $code = $generator->generateCode();
    } while ($coupon->getResource()->exists($code));

    // create rule
    /** @var Mage_SalesRule_Model_Rule $rule */
    $rule = Mage::getModel('salesrule/rule');
    $rule->setName(Mage::helper('mymodule')->__('Name of the coupon'))
        ->setDescription($rule->getName())
        ->setFromDate(date('Y-m-d'))
        ->setCustomerGroupIds($this->_getCustomerGroups())
        ->setIsActive(1)
        ->setConditionsSerialized(serialize($condition->asArray()))
        //->setActionsSerialized  
        //->setStopRulesProcessing
        //->setIsAdvanced                  
        ->setSimpleAction(Mage_SalesRule_Model_Rule::BY_FIXED_ACTION)
        ->setDiscountAmount($product->getFinalPrice())
        ->setDiscountQty(1)
        //->setDiscountStep                  
        ->setStopRulesProcessing(0)
        ->setIsRss(0)
        ->setWebsiteIds(array(1))
        ->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
        ->setConditions($condition)
        ->save();         
   
    // create coupon
    $coupon->setId(null)
        ->setRuleId($rule->getRuleId())
        ->setCode($code)
        ->setUsageLimit(1)
        //->setUsagePerCustomer
        //->setTimesUsed
        //->setExpirationDate
        ->setIsPrimary(1)
        ->setCreatedAt(time())
        ->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)
        ->save();
?>
<?php
    // load product
    /** @var Mage_Catalog_Model_Product $product */
    $product = Mage::getModel('catalog/product')
                    ->setStoreId($storeId)
                    ->load($productId);
 
    // set length of coupon code
    /** @var Mage_SalesRule_Model_Coupon_Codegenerator $generator */
    $generator = Mage::getModel('salesrule/coupon_codegenerator')
                        ->setLength(8);
    /** @var Mage_SalesRule_Model_Rule_Condition_Product $conditionProduct */
    $conditionProduct = Mage::getModel('salesrule/rule_condition_product')
                                                ->setType('salesrule/rule_condition_product')
                                                ->setAttribute('sku')
                                                ->setOperator('==')
                                                ->setValue($product->getSku());
                                                 
    /** @var Mage_SalesRule_Model_Rule_Condition_Product_Found $conditionProductFound */
    $conditionProductFound = Mage::getModel('salesrule/rule_condition_product_found')
                                            ->setConditions(array($conditionProduct));
    /** @var Mage_SalesRule_Model_Rule_Condition_Combine $condition */
    $condition = Mage::getModel('salesrule/rule_condition_combine')
                    ->setConditions(array($conditionProductFound));
                                                 
    /** @var Mage_SalesRule_Model_Coupon $coupon */
    $coupon = Mage::getModel('salesrule/coupon');
    // try to generate unique coupon code
    $attempts = 0;
    do {
        if ($attempts++ >= 8) {
            Mage::throwException(Mage::helper('mymodule')->__('Unable to create requested Coupons. Please try again.'));
        }
        $code = $generator->generateCode();
    } while ($coupon->getResource()->exists($code));
 
    // create rule
    /** @var Mage_SalesRule_Model_Rule $rule */
    $rule = Mage::getModel('salesrule/rule');
    $rule->setName(Mage::helper('mymodule')->__('Name of the coupon'))
        ->setDescription($rule->getName())
        ->setFromDate(date('Y-m-d'))
        ->setCustomerGroupIds($this->_getCustomerGroups())
        ->setIsActive(1)
        ->setConditionsSerialized(serialize($condition->asArray()))
        //->setActionsSerialized    
        //->setStopRulesProcessing
        //->setIsAdvanced                    
        ->setSimpleAction(Mage_SalesRule_Model_Rule::BY_FIXED_ACTION)
        ->setDiscountAmount($product->getFinalPrice())
        ->setDiscountQty(1)
        //->setDiscountStep                    
        ->setStopRulesProcessing(0)
        ->setIsRss(0)
        ->setWebsiteIds(array(1))
        ->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
        ->setConditions($condition)
        ->save();           
     
    // create coupon
    $coupon->setId(null)
        ->setRuleId($rule->getRuleId())
        ->setCode($code)
        ->setUsageLimit(1)
        //->setUsagePerCustomer
        //->setTimesUsed
        //->setExpirationDate
        ->setIsPrimary(1)
        ->setCreatedAt(time())
        ->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)
        ->save();
- See more at: http://www.magentron.com/blog/2012/05/22/programmatically-creating-a-coupon-including-the-rule-with-conditions-in-magento#sthash.QH5CMY3x.dpuf
<?php
    // load product
    /** @var Mage_Catalog_Model_Product $product */
    $product = Mage::getModel('catalog/product')
                    ->setStoreId($storeId)
                    ->load($productId);
    // set length of coupon code
    /** @var Mage_SalesRule_Model_Coupon_Codegenerator $generator */
    $generator = Mage::getModel('salesrule/coupon_codegenerator')
                        ->setLength(8);
    /** @var Mage_SalesRule_Model_Rule_Condition_Product $conditionProduct */
    $conditionProduct = Mage::getModel('salesrule/rule_condition_product')
                                                ->setType('salesrule/rule_condition_product')
                                                ->setAttribute('sku')
                                                ->setOperator('==')
                                                ->setValue($product->getSku());
                                                 
    /** @var Mage_SalesRule_Model_Rule_Condition_Product_Found $conditionProductFound */
    $conditionProductFound = Mage::getModel('salesrule/rule_condition_product_found')
                                            ->setConditions(array($conditionProduct));
    /** @var Mage_SalesRule_Model_Rule_Condition_Combine $condition */
    $condition = Mage::getModel('salesrule/rule_condition_combine')
                    ->setConditions(array($conditionProductFound));
                                                 
    /** @var Mage_SalesRule_Model_Coupon $coupon */
    $coupon = Mage::getModel('salesrule/coupon');
    // try to generate unique coupon code
    $attempts = 0;
    do {
        if ($attempts++ >= 8) {
            Mage::throwException(Mage::helper('mymodule')->__('Unable to create requested Coupons. Please try again.'));
        }
        $code = $generator->generateCode();
    } while ($coupon->getResource()->exists($code));
    // create rule
    /** @var Mage_SalesRule_Model_Rule $rule */
    $rule = Mage::getModel('salesrule/rule');
    $rule->setName(Mage::helper('mymodule')->__('Name of the coupon'))
        ->setDescription($rule->getName())
        ->setFromDate(date('Y-m-d'))
        ->setCustomerGroupIds($this->_getCustomerGroups())
        ->setIsActive(1)
        ->setConditionsSerialized(serialize($condition->asArray()))
        //->setActionsSerialized    
        //->setStopRulesProcessing
        //->setIsAdvanced                    
        ->setSimpleAction(Mage_SalesRule_Model_Rule::BY_FIXED_ACTION)
        ->setDiscountAmount($product->getFinalPrice())
        ->setDiscountQty(1)
        //->setDiscountStep                    
        ->setStopRulesProcessing(0)
        ->setIsRss(0)
        ->setWebsiteIds(array(1))
        ->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
        ->setConditions($condition)
        ->save();           
     
    // create coupon
    $coupon->setId(null)
        ->setRuleId($rule->getRuleId())
        ->setCode($code)
        ->setUsageLimit(1)
        //->setUsagePerCustomer
        //->setTimesUsed
        //->setExpirationDate
        ->setIsPrimary(1)
        ->setCreatedAt(time())
        ->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)
        ->save();
- See more at: http://www.magentron.com/blog/2012/05/22/programmatically-creating-a-coupon-including-the-rule-with-conditions-in-magento#sthash.QH5CMY3x.dpuf
<?php
    // load product
    /** @var Mage_Catalog_Model_Product $product */
    $product = Mage::getModel('catalog/product')
                    ->setStoreId($storeId)
                    ->load($productId);
    // set length of coupon code
    /** @var Mage_SalesRule_Model_Coupon_Codegenerator $generator */
    $generator = Mage::getModel('salesrule/coupon_codegenerator')
                        ->setLength(8);
    /** @var Mage_SalesRule_Model_Rule_Condition_Product $conditionProduct */
    $conditionProduct = Mage::getModel('salesrule/rule_condition_product')
                                                ->setType('salesrule/rule_condition_product')
                                                ->setAttribute('sku')
                                                ->setOperator('==')
                                                ->setValue($product->getSku());
                                                 
    /** @var Mage_SalesRule_Model_Rule_Condition_Product_Found $conditionProductFound */
    $conditionProductFound = Mage::getModel('salesrule/rule_condition_product_found')
                                            ->setConditions(array($conditionProduct));
    /** @var Mage_SalesRule_Model_Rule_Condition_Combine $condition */
    $condition = Mage::getModel('salesrule/rule_condition_combine')
                    ->setConditions(array($conditionProductFound));
                                                 
    /** @var Mage_SalesRule_Model_Coupon $coupon */
    $coupon = Mage::getModel('salesrule/coupon');
    // try to generate unique coupon code
    $attempts = 0;
    do {
        if ($attempts++ >= 8) {
            Mage::throwException(Mage::helper('mymodule')->__('Unable to create requested Coupons. Please try again.'));
        }
        $code = $generator->generateCode();
    } while ($coupon->getResource()->exists($code));
    // create rule
    /** @var Mage_SalesRule_Model_Rule $rule */
    $rule = Mage::getModel('salesrule/rule');
    $rule->setName(Mage::helper('mymodule')->__('Name of the coupon'))
        ->setDescription($rule->getName())
        ->setFromDate(date('Y-m-d'))
        ->setCustomerGroupIds($this->_getCustomerGroups())
        ->setIsActive(1)
        ->setConditionsSerialized(serialize($condition->asArray()))
        //->setActionsSerialized    
        //->setStopRulesProcessing
        //->setIsAdvanced                    
        ->setSimpleAction(Mage_SalesRule_Model_Rule::BY_FIXED_ACTION)
        ->setDiscountAmount($product->getFinalPrice())
        ->setDiscountQty(1)
        //->setDiscountStep                    
        ->setStopRulesProcessing(0)
        ->setIsRss(0)
        ->setWebsiteIds(array(1))
        ->setCouponType(Mage_SalesRule_Model_Rule::COUPON_TYPE_SPECIFIC)
        ->setConditions($condition)
        ->save();           
     
    // create coupon
    $coupon->setId(null)
        ->setRuleId($rule->getRuleId())
        ->setCode($code)
        ->setUsageLimit(1)
        //->setUsagePerCustomer
        //->setTimesUsed
        //->setExpirationDate
        ->setIsPrimary(1)
        ->setCreatedAt(time())
        ->setType(Mage_SalesRule_Helper_Coupon::COUPON_TYPE_SPECIFIC_AUTOGENERATED)
        ->save();
- See more at: http://www.magentron.com/blog/2012/05/22/programmatically-creating-a-coupon-including-the-rule-with-conditions-in-magento#sthash.QH5CMY3x.dpuf

How to add new custom category attribute in Magento

To create the custom attribute for category we have two option

1) Make a copy of your app/code/core/Mage/Catalog/sql/catalog_setup and change the name of your copied folder and put the mysql4-install-1.4.0.0.0.php
Like you wan to create a extra image attribute for a category then first you have to copy the catalog_setup and paste it into the  app/code/core/Mage/Catalog/sql/ then you will change the name catalogimage_setup and put mysql4-install-1.4.0.0.0.php paste the below code

<?php
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('catalog_category', 'image1', array(
    'input'         => 'image', // you can change here
    'type'          => 'varchar',
    'group' => 'General Information',/// Change here whare you want to show this
    'label'         => 'Additional Image for Mouse hover',
    'visible'       => 1,
    'backend' => 'catalog/category_attribute_backend_image',
    'required'      => 0,
    'user_defined' => 1,
    'frontend_input' =>'',
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible_on_front'  => 1,
));
?>
After it got to app/code/core/Mage/Catalog/etc/config.xml
then find the
 <catalog_setup>
                <setup>
                    <module>Mage_Catalog</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
</catalog_setup>

make a copy of the above line and paste it just after
 with your setup name
<catalogimage_setup>
                <setup>
                    <module>Mage_Catalog</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
 </catalogimage_setup>

and run the admin site now you attribute has been created.

2)
 Just copy paste the below code in header.phtml and run yourmagento once, your attribute will be created and you can see in backend under manage category. After you are done remove this code again.

 ------------------------------------------------------------------------------------------
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('catalog_category', 'sliderimage', array(
    'group'         => 'General',
    'input'         => 'image',
    'type'          => 'varchar',
    'label'         => 'Slider Image',
    'backend'       => 'catalog/category_attribute_backend_image',
    'visible'       => 1,
    'required'        => 0,
    'user_defined' => 1,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
));

Related product is not coming, upsell product is coming by default

1)\app\design\frontend\default\magikjewellery\layout\catalog.xml

Change

<block type="catalog/product_list_upsell" name="product.info.upsell" as="upsell_products" template="catalog/product/list/upsell.phtml">
                    <action method="setColumnCount"><columns>4</columns></action>
                    <action method="setItemLimit"><type>upsell</type><limit>4</limit></action>
                </block>
With

<block type="catalog/product_list_related" name="product.info.related"  template="catalog/product/list/related.phtml" as="related_products">
         
</block>

2) \app\design\frontend\default\magikjewellery\template\catalog\product\view.phtml

change

            <?php //echo $this->getChildHtml('upsell_products') ?>    

With

<?php echo $this->getChildHtml('related_products') ?>      

3)Go to admin

Catalog/manage product/add product/related product
Click on reset filter
Check the products you want to make related and save product.

Get top menu link

top.phtml in app/design/frontend/default/yourtheme/template/catalog/navigation/

please enable cookies in web browser in magento 1.5 for front end login

app\code\core\Mage\Core\Model\Session\Abstract\varien.php
comment following line
// call_user_func_array('session_set_cookie_params', $cookieParams);

redirect page after 5 second


<script>
setTimeout(function(){ location.href = '<?php echo $this->getUrl() ?>'},5000);
</script>

Magento admin login issues

The solution for this modify the core Magento code. Open the page app/code/core/Mage/Core/Model/Session/Abstract/Varien.php. Comment out the lines 80 to 83. The line number may vary according to the Magento version. But these lines are present somewhere near line 80.

You have to comment the comma (,) in line:

$this->getCookie()->getPath()//,
// set session cookie params
session_set_cookie_params(
$this->getCookie()->getLifetime(),
$this->getCookie()->getPath()//,
//$this->getCookie()->getDomain(),
//$this->getCookie()->isSecure(),
//$this->getCookie()->getHttponly()
);
Well, I am out of this problem. Hope, this solution you also help you.
Update (For Magento 1.4.*)
In Magento 1.4, you have to comment code from line 86 to 98 in app/code/core/Mage/Core/Model/Session/Abstract/Varien.php. Like this:-
/* if (!$cookieParams['httponly']) {
unset($cookieParams['httponly']);
if (!$cookieParams['secure']) {
unset($cookieParams['secure']);
if (!$cookieParams['domain']) {
unset($cookieParams['domain']);
}
}
}
if (isset($cookieParams['domain'])) {
$cookieParams['domain'] = $cookie->getDomain();
} */

How to show recently viewed product on product description page

<block type="reports/product_viewed" name="reports.product.viewed" as="recently_viewed" template="reports/product_viewed.phtml"> 
<action method="setColumnCount">
<columns>4</columns>
</action>
  <action method="setItemLimit">
<type>recently_viewed</type><limit>4</limit>
</action>           
</block>


in the template file: catalog/product/view.phtml:

<?php echo $this->getChildHtml('recently_viewed') ?>

Find final price and tier price in magento

<?php
                     
                        if(Mage::getSingleton('customer/session')->isLoggedIn()): ?> 
                                                           
                                                            <?php $this->setProduct(Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($_product->getId()))?>
                                                            INK<?php echo $this->getTierPriceHtml() ?>VK<?php echo Mage::helper('core')->currency($_product->getFinalPrice());?>     
                                                           
                        <?php else: ?>
                       
                                                            <?php echo Mage::helper('core')->currency($_product->getFinalPrice());?>
                                                           
                <?php endif; ?>