Monday, 23 December 2013

Magento Add "addAttributeToFilter() function" with Conditionals.

1) Equals: eq

 $_products->addAttributeToFilter('status', array('eq' => 1));

 2) Not Equals - neq

 $_products->addAttributeToFilter('sku', array('neq' => 'test-product'));

 3) Like - like

 $_products->addAttributeToFilter('sku', array('like' => 'UX%'));

 4) Not Like - nlike

 $_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));

 5) In - in

 $_products->addAttributeToFilter('id', array('in' => array(1,4,98)));

 6) Not In - nin

 $_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));

 7) NULL - null

 $_products->addAttributeToFilter('description', 'null');

 8) Not NULL - notnull

 $_products->addAttributeToFilter('description', 'notnull');

 9) Greater Than - gt

 $_products->addAttributeToFilter('id', array('gt' => 5));

 10) Less Than - lt

 $_products->addAttributeToFilter('id', array('lt' => 5));

 11) Greater Than or Equals To- gteq

 $_products->addAttributeToFilter('id', array('gteq' => 5));

 12) Less Than or Equals To - lteq

 $_products->addAttributeToFilter('id', array('lteq' => 5));

Magento Arrange Collection in Specific Range Like A-D, 0-9 etc.

Add this code after any collection.

<?php $collection_AtoD->getSelect()->where("name like 'A%' or name like 'B%' or name  like 'C%' or name  like 'D%'"); ?>

Magento Show Discount in Special Price.

Add this code in template > catalog > product > price.phtml

<?php if($_finalPrice < $_price): ?>    
<?php $_savePercent = 100 - round(($_finalPrice / $_price) * 100); $_saveAmount = number_format(($_price - $_finalPrice), 2); ?>        
<p class="yousave">           
    <span class="price-label label">You Save: </span>           
    <span class="price">
        <strong class="save-amount">$<?php echo $_saveAmount; ?></strong> (<?php echo $_savePercent; ?>%)
    </span>
</p>   
<?php endif; ?>

Friday, 22 November 2013

Add Quantity Increment Decrement Buttons In Magento

app\design\frontend\default\your theme\template\catalog\product\view\addtocart.phtml

<?php $_product = $this->getProduct(); ?>
<?php $buttonTitle = $this->__(‘Add to Cart’); ?>
<?php if($_product->isSaleable()): ?>
<div>
<?php if(!$_product->isGrouped()): ?>
<div>
<label for=”qty”><?php echo $this->__(‘Qty:’) ?></label>
<span> <a href=”javascript:void(0)” onclick=”decrementQty(‘qty’);”><img src=”<?php echo $this->getSkinUrl(‘images/qty_mns_btn.jpg’) ?>” alt=”minus” /></a></span>
<span> <input type=”text” name=”qty” id=”qty” maxlength=”6″ value=”1″ title=”Antal” /></span>
<?php endif; ?>
<span> <a href=”javascript:void(0);” onclick=”incrementQty(‘qty’);”><img src=”<?php echo $this->getSkinUrl(‘images/qty_plus_btn.jpg’) ?>” alt=”minus” /></a></span>
</div>
<button title=”<?php echo $buttonTitle ?>” onclick=”productAddToCartForm.submit(this)”><span><span><?php echo $buttonTitle ?></span></span></button>
<?php echo $this->getChildHtml(”, true, true) ?>
</div>
<?php endif; ?>
<script type=”text/javascript”>
//<![CDATA[
function decrementQty(id) {
var val = parseInt($(id).getValue());
if(val != 1)
document.getElementById(id).value = val - 1;
}
function incrementQty(id) {
var val = parseInt($(id).getValue());
document.getElementById(id).value = val + 1;
}
//]]>
</script>

Monday, 21 October 2013

Magento change product image on hover in catalog page

If you want to show thumbnail image on mouse over & after mouse out show small image then go through

template->catalog->product->list.pthml

in this Search this code:

<a href=”<?php echo $_product->getProductUrl() ?>” title=”<?php echo $this->stripTags($this->getImageLabel($_product, ‘small_image’), null, true) ?>”><img src=”<?php echo $this->helper(‘catalog/image’)->init($_product, ‘small_image’)->resize(135); ?>” width=”135″ height=”135″ alt=”<?php echo $this->stripTags($this->getImageLabel($_product, ‘small_image’), null, true) ?>” /></a>

in place of that put this code :

<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(217); ?>" width="147" height="217" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" onmouseover="this.src='<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(217) ?>';" onmouseout="this.src='<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(217) ?>';" />

Display Configure Product Option in Product List Page in Magento.

Add following script in product listing page catalog/product/list.phtml

<?php echo $this->helper(‘checkout’)->formatPrice($_product->getFinalPrice()) ?>
<form action=”<?php echo Mage::getBaseUrl(); ?>checkout/cart/add/” method=”post” style=”display:block; clear:both;”>

<?php
$product = Mage::getModel(‘catalog/product’);
$product->load($_product->getId());
$xml = “”;
$hasAtts = 0;

// configurable products
$productType = $product->getTypeId();
if($productType == “configurable”) {

// configurable products
$attValConfig = $product->getTypeInstance()->getConfigurableAttributesAsArray();

if(sizeof($attValConfig)) {

$hasAtts++;

foreach($attValConfig as $attValConfigSingle) {

$xml .= “<div class=’floatoption1′>”.$attValConfigSingle['frontend_label'].”: “;

$xml .= “<select style=’display:block; clear:both; margin-bottom:10px;’ name=’super_attribute[".$attValConfigSingle['attribute_id'].”]’><option selected=’selected’>Choose an option</option>”;

foreach($attValConfigSingle['values'] as $attValConfigSingleVal) {

$baseprice = $this->getPriceHtml($product, true);
$strippedprice = str_replace(“£”, “”, $baseprice);
$numprice = strip_tags($strippedprice);
$numpricea = (float)$numprice;
$optionprice = $attValConfigSingleVal['pricing_value'];
$numpriceb = (float)$optionprice;
$catprice = $numpricea + $numpriceb;
$catpricea = number_format($catprice,2);

if (is_numeric($numpriceb)) {
$xml .= “<option value=’”.$attValConfigSingleVal['value_index'].”‘>”.$attValConfigSingleVal['label'].” £”.$catpricea.”</option>”;
} else { $xml .= “<option value=’”.$attValConfigSingleVal['value_index'].”‘>Not Mumeric</option>”; }
}
$xml .= “</select></div>”;

}

}

}

// end configurable products
$attVal = $product->getOptions();

if(sizeof($attVal)) {

$hasAtts++;

foreach($attVal as $optionVal) {

$xml .= “<div class=’floatoption1′>Size:<select style=’display:block; clear:both; margin-bottom:10px;’ name=’options[".$optionVal->getId()."]‘><option selected=’selected’>Choose an option</option>”;

foreach($optionVal->getValues() as $valuesKey => $valuesVal) {
$xml .= “<option value=’”.$valuesVal->getId().”‘>”.$valuesVal->getTitle().” +”.Mage::helper(‘core’)->currency($valuesVal->getPrice()).”</option>”;
}

$xml .= “</select></div>”;

}

}

$xml .= “<div class=’floatoption2′>Qty: <input style=’display:block; clear:both; margin-bottom:20px;’ id=’qty’ class=’input-text qty’ type=’text’ value=’1′ maxlength=’12′ name=’qty’/></div>”;

echo($xml);

?>

<input type=”hidden” name=”product” value=”<?php echo($_product->getId()); ?>” />
<div>
<button type=”submit”><span><span><?php echo $this->__(‘Add to Cart’) ?></span></span></button></div>

</form>
<script type=”text/javascript”>
var productAddToCartForm_<?php echo $_product->getId(); ?> = new VarienForm(‘product_addtocart_form_<?php echo $_product->getId(); ?>’);
productAddToCartForm_<?php echo $_product->getId(); ?>.submit = function(){
if (this.validator.validate()) {
this.form.submit();
}
}.bind(productAddToCartForm_<?php echo $_product->getId(); ?>);
</script>

// <![CDATA[
var productAddToCartForm_getId(); ?> = new VarienForm('product_addtocart_form_getId(); ?>');
productAddToCartForm_getId(); ?>.submit = function(){
if (this.validator.validate()) {
this.form.submit();
}
}.bind(productAddToCartForm_getId(); ?>);
// ]]>

Adding product from fornt-end with image in mangento programmatically In Magento

We can add a product from frond end in magneto Paste this code in file and place the file in root folder of project run this file in browser product will created and also give image correct path the image also uploaded

<?php
//$product = Mage::getModel('catalog/product');
$product = new Mage_Catalog_Model_Product();
echo time();
// Build the product
$product->setAttributeSetId(4);// #4 is for default
$product->setTypeId('simple');
$product->setName('Some cool product name');
$product->setDescription('Full description here');
$product->setShortDescription('Short description here');
$product->setSku(time());
$product->setWeight(4.0000);
$product->setStatus(1);
$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);//4
print_r(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
$product->setPrice(39.99);// # Set some price
$product->setTaxClassId(0);// # default tax class
$product->setStockData(array(
'is_in_stock' => 1,
'qty' => 100
));

//$product->setCategoryIds(array(27));// # some cat id's,
$product->setWebsiteIDs(array(1));// # Website id, 1 is default

//Default Magento attribute
$product->setCreatedAt(strtotime('now'));

try {
    $product->save();
    echo "Product Created";
}
catch (Exception $ex) {
    echo "Product Creation Failed";
}
?>