How To show product quantity in dropdown Magento - magento

I want to show Product quantity in dropdown like 10,20,30 for according to product in magento please help what are i do
please help
thanks

I would recommend you create a new numeric custom attribute for your products so you can define the packsize/order multiple for each product. Then go to this file;
app/design/frontend/YOURFOLDER/YOURTHEME/template/catalog/product/view/addtocart.phtml
In this file, find this code;
<?php if(!$_product->isGrouped()): ?>
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<input type="text" name="qty" id="qty" maxlength="12" value="1<?php //echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
<?php endif; ?>
And replace it with this;
<?php if(!$_product->isGrouped()): ?>
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<?php
$qtymultiple = $_product->getData('your_attribute_code'); // Add your attribute_id here
if(($qtymultiple == 1) || (!$qtymultiple)) { // No need to anything ?>
<input type="text" name="qty" id="qty" maxlength="12" value="1<?php //echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
<?php } else { //qty multiple requirement ?>
<select name="qty" id="qty" maxlength="12">
<?php
$countme = 1;
while ($countme < 101) { // Define how many multiples of the number to offer
echo '<option value="'.($qtymultiple*$countme).'">'.($qtymultiple*$countme).'</option>';
$countme++;
} ?>
</select>
<?php } ?>
<?php endif; ?>
This will show a select box with multiples instead of the free type qty box for products that have a value the custom attribute.

Related

remove quantity box for specific product

Good morning I installed Magento ver. 1.9.2.1 and I need to delete the box quantity only on some products. I was thinking, for example, when max quantity is set to 1, instead for other products leave the box. Thank you, do you have any suggestions?
Nicola
I tried to modify the file addtochar.phtml but without success. I attach the code
?>getProduct(); ?> ('Add to Cart'); ?> isSaleable()): ?> isGrouped()): ?> __('Qty:') ?> getProductDefaultQty() * 1 ?>" title="quoteEscape($this->('Qty')) ?>" class="input-text qty" /> " id="product-addtocart-button" class="button btn-cart" onclick="productAddToCartForm.submit(this)"> getChildHtml('', true, true) ?> showButtonDesign(); ?>
I hope this code will help you.
<div class="add-to-cart">
<?php if(!$_product->isGrouped()): ?>
<?php $hiddenFlag = in_array($_product->getId(), array(10,20,30,40)) ? true : false; ?>
<?php if (!$hiddenFlag): ?>
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<?php endif;?>
<input type="<?php echo $hiddenFlag ? 'hidden' : 'text' ?>" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Qty')) ?>" class="input-text qty" />
<?php endif; ?>
<button type="button" title="<?php echo $buttonTitle ?>" id="product-addtocart-button" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span><?php echo $buttonTitle ?></span></span></button>
<?php echo $this->getChildHtml('', true, true) ?>
</div>
Please pay attention to this line: <?php $hiddenFlag = in_array($_product->getId(), array(10,20,30,40)) ? true : false; ?> it will check if product eligible for a qty visibility.

How to sort products by Custom Attributes?

Is there a way to sort products by any custom attribute? For example, sort by category, brand or colour?
I assume that I should create the attribute and set it to be "Used for Sorting in Product Listing".
I would like to sort products by the following attributes:
gender first, then brand, category and lastly colour.
The question is how do I use these attributes to sort the products in the backend so that they are visible in the frontend in that order?
You can use this code
Create a phtml file (sort.phtml) under catalog\product\list.
Contain of sort.phtml is
<div class="sort-by">
<label><?php echo $this->__('Sort By') ?></label>
<select onchange="setLocation(this.value)">
<?php foreach($this->getAvailableOrders() as $_key=>$_order): ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
<?php echo $this->__($_order) ?>
</option>
<?php endforeach; ?>
</select>
<?php if($this->getCurrentDirection() == 'desc'): ?>
<img src="<?php echo $this->getSkinUrl('images/i_desc_arrow.gif') ?>" alt="<?php echo $this->__('Set Ascending Direction') ?>" class="v-middle" />
<?php else: ?>
<img src="<?php echo $this->getSkinUrl('images/i_asc_arrow.gif') ?>" alt="<?php echo $this->__('Set Descending Direction') ?>" class="v-middle" />
<?php endif; ?>
</div>
After that call this file in list.phtml using below codes-
echo $this->getLayout()->createBlock("catalog/product_list_toolbar")->setTemplate('catalog/product/list/sort.phtml')->toHtml();
Hope this code will be working

Add field to Edit Account form in Magento

I'm adding a field to the template customer/form/edit.phtml. I used this code :
<li>
<?php
$attribute = Mage::getModel('eav/config')->getAttribute('customer','code_magique');
?>
<label for="code_magique" class="<?php if($attribute->getIsRequired() == true){?>required<?php } ?>"><?php if($attribute->getIsRequired() == true){?><em>*</em><?php } ?><?php echo $this->__('Code') ?></label>
<div class="input-box">
<input type="text" name="code_magique" id="code_magique" value="<?php echo $this->htmlEscape($this->getCustomer()->getData("code_magique")) ?>" title="<?php echo $this->__('Code') ?>" class="input-text" />
</div>
</li>
This display a field with my "code_magique" attribute inside but when i try to modify this attribute, it doesn't work, did I forget something?
I did a quick look at AccountController.php, and it seems there is some postprocessing of form data, not the simple $model->addData($post);
Try hooking to customer_save_before event and adding your data manually.
Hopefully you know how to create anobserver and add data to model object?

Magento - Add to cart with quantity field on Upsell products

I'm trying to get my Upsell products, which I use instead of related products on the view.phtml, to work with a Quantity field in front of the Add to cart button. I'am also using AheadWorks Ajax AddToCart Pro 2.5 extension.
Right now, I'am adding the products without quantity fields with this snippet:
<form action="<?php echo $this->getAddToCartUrl($_link) ?>" method="post" id="view_addtocart_form_<?php echo $_link->getId(); ?>"><button onclick="setLocation('<?php echo $this->getAddToCartUrl($_link) ?>')" class="greenbutton" title="Add to Cart" type="button"><span><span>Add to Cart</span></span></button></form>
This works great, but I cannot change the quantity due to the lacking quantity field. Then I try to use this from my list.phtml which works fine, in the category view:
<script type="text/javascript">
function setQty(id, url) {
var qty = document.getElementById('qty_' + id).value;
document.getElementById('cart_button_' + id).innerHTML = '<button type="button" class="greenbutton-small" onclick="setLocation(\'' + url + 'qty/' + qty + '/\')"><span><span>Læg i kurv</span></span></button>';
}
</script>
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<input type="text" name="qty_<?php echo $_product->getId(); ?>" id="qty_<?php echo $_product->getId(); ?>" maxlength="12" value="1" onkeyup="setQty(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product) ?>');" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
<span id="cart_button_<?php echo $_product->getId(); ?>">
<button type="button" class="greenbutton-small" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></span>
Now, the funny thing is, this does not work if I try to use it in upsell.phtml, but I cannot figure out why? This works perfect with Ajax Cart Pro from AW in the Category view's.
In upsell.phtml the product object is called $_link per the following code:
<?php if($_link=$this->getIterableItem()): ?>
If you're trying to include your code in your upsell.phtml then you'll have to change $_product to $_link like this:
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<input type="text" name="qty_<?php echo $_link->getId(); ?>" id="qty_<?php echo $_link->getId(); ?>" maxlength="12" value="1" onkeyup="setQty(<?php echo $_link->getId(); ?>, '<?php echo $this->getAddToCartUrl($_link) ?>');" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
<span id="cart_button_<?php echo $_link->getId(); ?>">
<button type="button" class="greenbutton-small" onclick="setLocation('<?php echo $this->getAddToCartUrl($_link) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></span>
It should go after the following line:
<?php if($_link=$this->getIterableItem()): ?>

Change gender dropdown to radio button in magento

Today i started working in registration form in magento site. As you know by default it's having gender drop down. I need to change that to checkbox.
So far i went to register.phtml file, and tried to add <input type="radio" ...../> select files, but this didn't worked.
Did anyone know how to solve this !
please give me some suggestions to so this....
Do not forget the validation!
<div class="input-box">
<?php
$options = Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();
$value = $this->getGender();
$_last = count($options);
$_index = 0;
?>
<?php foreach ($options as $option):?>
<?php if($option['value'] != ''): ?>
<input id="<?php echo $this->getFieldId('gender')?>-<?php echo $option['value'] ?>"
class="radio<?php if ($this->isRequired() && $_index == $_last - 1):?> validate-one-required<?php endif; ?>"
type="radio" title="<?php echo $option['label'] ?>"
value="<?php echo $option['value'] ?>" name="<?php echo $this->getFieldName('gender')?>"
<?php if ($option['value'] == $value) echo ' checked="checked"' ?>>
<label for="<?php echo $this->getFieldId('gender')?>-<?php echo $option['value'] ?>">
<?php echo $option['label'] ?>
</label>
<?php endif; ?>
<?php $_index++; ?>
<?php endforeach;?>
</div>
Magento uses widgets on registration form. In fact in template register.phtml you can see lines:
<?php $_gender = $this->getLayout()->createBlock('customer/widget_gender') ?>
<?php if ($_gender->isEnabled()): ?>
<li><?php echo $_gender->setGender($this->getFormData()->getGender())->toHtml() ?></li>
<?php endif ?>
This particular widget can be found in template/customer/widget directory. So in order to change select into radio buttons, copy it (template) to your theme and modify, e.g.:
<div class="input-box">
<label><?php echo $this->__('Gender'); ?></label>
<?php $options = Mage::getResourceSingleton('customer/customer')->getAttribute('gender')->getSource()->getAllOptions();?>
<?php $value = $this->getGender();?>
<?php foreach ($options as $option):?>
<input type="radio" name="<?php echo $this->getFieldName('gender')?>" value="<?php echo $option['value'] ?>"<?php if ($option['value'] == $value) echo ' selected="selected"' ?> /><?php echo $option['label'] ?>
<br />
<?php endforeach;?>
</div>
Hope didn't make any typo.

Resources