Magento - add product to cart with fixed quantity - magento

I'm adding a product programmatically in an action to the cart.
Is it possible to set a fixed quantity in this step, which the user can't change afterwards?

You cannot set a fixed quantity that can't be manipulated by the user in some way or another, however you can mask it from the users view.
There are two ways to achieve this, first option is the non coding way around it but won't be as user friendly as the second:
First option:
Goto products backend -> Inventory and set 'Maximum Qty Allowed in Shopping Cart' to the fixed quantity. You can use the above answer to set the fixed quantity.
Second option:
If not, then you can modify the default.phtml (cart item render) to prevent the quantity adjustment field being rendered. You could use anything here to define the product, you could attach some custom options to identify the product.
Your looking for the line with the following:
<input name="cart[<?php echo $_item->getId() ?>][qty]" value="<?php echo $this->getQty() ?>" size="4" title="<?php echo $this->__('Qty') ?>" class="input-text qty" maxlength="12" />
Wrap it in an if else statement to differentiate the product you are adding programatically (you could use for example, Sku, product ID or a custom option). Instead of rendering the input field, just render a static 1 instead with no option to modify the quantity.
You could also add an option to the quote item and then pull via getOptionByCode() for the differentiation.
Doing both options would be a complete solution to your problem.

Yes.
public function addAction()
{
if (!$this->_validateFormKey()) {
$this->_goBack();
return;
}
$cart = $this->_getCart();
$params = $this->getRequest()->getParams();
try {
if (isset($params['qty'])) {
$filter = new Zend_Filter_LocalizedToNormalized(
array('locale' => Mage::app()->getLocale()->getLocaleCode())
);
$params['qty'] = $filter->filter($params['qty']);
}
As you can see, in your CartController you have add action.
The code above shows that this controller expects a param called "qty", that you can use.
I'm using magento1.8

Related

adding a different customer example:seller through a different regestration page

I am using magento 1.9. I want to add a customer group as seller from the front end using a different registration form keeping the registration form for registration of general category separate.
Add in your registration the following field,
<?php
$groups = Mage::helper('customer')->getGroups()->toOptionArray();
foreach ($groups as $group){
echo '<input type="radio" name="group_id" value="'.$group['value'].'" class="validate-radio" >'.$group['label'].'</input><br/>';
}
?>
And save the fields as following in your controller file.
$customer->setGroupId($this->getRequest()->getPost(‘group_id’));

Magento: Inventory increment qty attribute per store

I have one simple question.
My problem is:
Is there are any free extensions that could turn "Enable Qty Increments" and "Qty Increments" from global scope to store view?
Also I have found this question inventory settings
It's have some kind of answer, but I need to confirm this.
If there are no free extension that could fulfill my needs, do I need to write my own extension (as answer in previous link says) or there is an easy way to change scope from global to store view. ?
My Magento version is CE 1.9.1.0
What you could do to achieve the same thing is create a new product text attribute called pack_size, give it a per store view scope, then set the order quantity against it per product, per store view.
Then, in your addtocart.phtml file, here;
app/design/frontend/XXX/YYY/template/catalog/product/view/addtocart.phtml
Where XXX YYY is the name of your theme, and replace the quantity input box with;
<?php $pack = $_product->getData('pack_size'); ?>
<?php if(($pack == 1) || (!$pack)) { ?>
<input type="text" name="qty" id="qty" maxlength="4" value="1" />
<?php } else { ?>
<select name="qty" id="qty" maxlength="12">
<?php
$countme = 1;
while ($countme < 101) {
echo '<option value="'.($pack*$countme).'">'.($pack*$countme).'</option>';
$countme++; } ?>
</select>
Now if the value of pack_offer is set and greater than 1, the user will only be able to choose a multiple of that qty.
Depending on your theme, you may also need to implement this in the cart page.

Magento: Limit Product Max Quantity Per Customer (NOT per Order)

I know we can easily limit the max quantity of a given product a customer can purchase per order, but is it possible (natively or even with a plugin) to limit max quantity of a given product per CUSTOMER ??
I don't want to use a coupon nor modify the code: it needs to be a sale price with the help of native or extension functionality.
Magento 1.5.1
It is not possible native, but you can make a module that will perform such restrictions.
You need to create a resource model, that will retrieve not canceled and not refunded orders for product(s) with particular product id. Actually it just a simple select to sales/order and sales/order_item table. Method of resource model might look like the following:
public function getPurchasedProductQty(array $productIds, $customerId)
{
$select = $this->_getReadAdapter()->select();
$select
->from(array('order_item' => $this->getTable('sales/order_item')),
array(
'qty' => new Zend_Db_Expr('order_item.ordered_qty - order_item.canceled_qty - order_item.refunded_qty'),
'product_id'))
// Joining order to retrieve info about item and filter out canceled or refunded orders
->join(array('order' => $this->getTable('sales/order')),
'order.entity_id = order_item.order_id',
array())
// Limit it to the current customer
->where('order.customer_id = ?', $customerId)
// Filter out refunded and canceled orders
->where('order.state NOT IN(?)', array(
Mage_Sales_Model_Order::STATE_CLOSED,
Mage_Sales_Model_Order::STATE_CANCELED
))
// Add Product Id Condition
->where('order_item.product_id IN(?)', $productIds);
return $this->_getReadAdapter()->fetchCol($select);
}
Then when you observe sales_quote_item_collection_products_after_load event you just can place your custom logic with checking the restrictions on products that are going to be used in the cart and remove that ones from loaded collection. This logic you should implement yourself.
Assuming that you are trying limit the product qty a registered customer who is currently log in can add to their cart.
(This is a one to one relationship, but could easily modify to accomodate many different products and qty per customer)
Create a custom module that will add a field in customer entity, so admin can set the appropriate qty for each customer.
Field name: [ModuleName]_product_id (see Adding attributes to customer entity)
Field name: [ModuleName]_max_cart_qty (see Adding attributes to customer entity)
In (copy files below to your local template folder) and update the qty input field.
/app/design/frontend/base/default/template/catalog/product/view/addtocart.phtml
/app/design/frontend/base/default/template/checkout/cart/item/default.phtml
Change
<input type="text" class="input-text qty" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" />
to (Add a validation class to make sure the qty is less than or equal )
$addValidationClass = '';
if( Customer is login && ModuleName_product_id == $_product->getId() && [ModuleName]_max_cart_qty > 0){
$addValidationClass = ' validate-digits-range-1-' . [ModuleName]_max_cart_qty
}
<input type="text" class="input-text qty<?php echo $addValidationClass; ?>" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" />
If you want to do server-side validation then create a observer for add to cart event, that compare the above logic to the item currently been add to cart
Below Extension Will Be help to achieve this
https://www.magentocommerce.com/magento-connect/maximum-order-quantity.html
Edit
In this Extension give
Quite often store owners need to restrict the order product quantity with custom message at cart page. This is not possible while using the default admin settings. However, by using this extension you can set the limit for product quantity with custom error message. If maximum quantity limit exceeds the limit then error message will be shown at cart page.
You can set the maximum quantity for each product with custom error message. You can enable/disable globally them using the backend options.

Magento 1.6.2 Adding Configurable Product in Wishlist : How to check whether or not all required fields are filled before adding to wishlist

6.2 community edition to develop an apparel site where each product is available in various sizes, hence all products are added as configurable products.
Tshirt size is a required attribute and is set as a dropdown with its first value as Select A Size.
When add to cart is clicked from the product page, whether this attribute is selected or not is rightly checked.
I have also added a ADD TO WISHLIST button to the product view page. But add to wishlist does not check the required field - attribute set.
It just directly adds the configurable product to wishlist without its required attribute size.
QS: How do i make sure that like ADD TO CART, the add to wishlist button first checks whether or not the Tshirt size is selected, and if selected only then go and add it to wishlist. Otherwise, give an error .... like in the case of Add To Cart that the Tshirt Size is required..
thanks
Moody
Navigate to /app/design/frontend/<YOUR_PACKAGE>/<YOUR_TEMPLATE>/catalog/product/view/addto.phtml and change productAddToCartForm.submitLight to productAddToCartForm.submit. The full line is:
<li><?php echo $this->__('Add to Wishlist') ?></li>
Change it to:
<li><?php echo $this->__('Add to Wishlist') ?></li>
Open app/design/frontend/YOUR_PACKAGE/YOUR_THEME/template/catalog/product/view.phtml
Comment all delete Validation.methods lines from the following Javascript code present in that file:
productAddToCartForm.submitLight = function(button, url){
if(this.validator) {
var nv = Validation.methods;
//delete Validation.methods['required-entry'];
//delete Validation.methods['validate-one-required'];
//delete Validation.methods['validate-one-required-by-name'];
// Remove custom datetime validators
for (var methodName in Validation.methods) {
if (methodName.match(/^validate-datetime-.*/i)) {
//delete Validation.methods[methodName];
}
}
if (this.validator.validate()) {
if (url) {
this.form.action = url;
}
this.form.submit();
}
Object.extend(Validation.methods, nv);
}
}.bind(productAddToCartForm);

How to display Magento Custom Options Individually

My Magento site has a product which has a few Custom Options, one text, one file upload and four drop down lists.
The design of the site dictates that I need to show these options throughout the product view page and not all in one group.
Is there a function that I can call to return the HTML of a single Custom Option?
There are ways to do this that are tantamount to cheating.
Your shop requires javascript to operate and there is a lot you can do with Prototype before the page renders, by using the on dom:loaded event. You can attach your custom options to wherever you want in the DOM, or you can hide them and put something else where you want it on the page that updates the form element. You may want to do this if you have to capture a colour name but don't want to put oodles of colours on every product - a textbox can go on the product and your control can write to it.
The benefit of some $$('cheating') is that you don't have to go too deep into Magento code for what is a 'design consideration'.
I didn't understand correctly about group. If you mean category then ;
create a new attribute set which this attribute set should contain attributes that you want to show. After that, when you create a product, select this attribute set instead of default. So that, only this attributes will be available in the specified products.
Try the following code snippets ( don't forget to change "attribute_code")
Let say, you want to show Multi Select list in your product page, in that case :
$selectArray = $this->getProduct()->getAttributeText('YOUR_ATTRIBUTE_CODE');
$endOfArray = end($selectArray);
echo "<ul class='set-some-class'>";
foreach($selectArray as $selectionItem) {
echo "<li> . $selectionItem";
if($selectionItem != $endOfArray) {
echo "</li>\n";
} else {
echo "</ul>";
}
}
For page other than product view page, in that case:
$attribute = Mage::getModel('catalog/product')->getAttribute('catalog_product', 'YOUR_ATTRIBUTE_CODE');
$options = $attribute->getSource()->getAllOptions(true, true);
$lastOption = end($options);
echo "<ul class='set-some-class'";
foreach($options as $option) {
echo $option['label'];
if($option != $lastOption) {
echo "<li>\n";
} else {
echo "</ul>";
}
}

Resources