Archive for : June, 2011

Javascript – limit the number of characters in a textarea

Javascript – limit the number of characters in a textarea:


<script language="javascript" type="text/javascript">
function limitText(fieldLimit, countLimit, limitNum) {
if (fieldLimit.value.length > limitNum) {
fieldLimit.value = fieldLimit.value.substring(0, limitNum);
} else {
countLimit.value = limitNum - fieldLimit.value.length;
}
}
</script>
<form name="form_name">
<textarea name="sometext" onKeyDown="limitText(this.form.sometext,this.form.counter,100);"
onKeyUp="limitText(this.form.sometext,this.form.counter,100);">
</textarea><br />
(Maximum characters: 100)<br />
You have <input readonly type="text" name="counter" size="3" value="100"> characters left.
</form>

Read original article

Javascript – addslashes and stripslashes

A Javascript function for add slashes:


function addslashes(str) {
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\0/g,'\\0');
return str;
}

A Javascript function for strip slashes:


function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\0/g,'\0');
str=str.replace(/\\\\/g,'\\');
return str;
}

Magento get child products for a configurable product

Magento get products childs for a configurable product


// $currentProduct = $this->getProduct();
$configurable_products = Mage::getModel('catalog/product_type_configurable')->setProduct($currentProduct);
$products_collection = $configurable_products->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
foreach($products_collection as $_product){
echo $_product->getId() . ": " . $_product->getName();
}

Magento – Show Quantity Box in Products List

In file ‘list.phtml’ file change the <button> line near line 108 from this:


<?php if($_product->isSaleable()): ?>
<button class="form-button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?<')">>span>
<?php echo $this->__('Add to Cart') ?></span></button>

with:


<form action="
<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId(); ?>">
<input name="qty" type="text" class="input-text qty" id="qty" maxlength="12" value="
<?php echo $this->getMinimalQty($_product) ?>" />
<button class=form-button" onclick="productAddToCartForm_
<?php echo $_product->getId(); ?>.submit()"><span>
<?php echo $this->__('Add to Cart') ?></span></button>
</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>

Read full article