CarComp
CarComp

Reputation: 2016

Whats a good way to limit the purchase qty of downloadable products in magento

Firstly: I get the mvc and php of magento, but I'm not fully versed on what its 'built in features' can possibly do.

I am working on a way to limit the QTY to 1 when a user selects one of my BOOK configurable products as the epub or pdf version. I'm getting ready to start tackling it with some jQuery voodoo in the theme to hide the QTY option if the selection is not "Physical". I was hoping someone might know of a way to do this or have experience doing this before.

Feel free to answer with "do this in admin" or "code something like this"

Thanks

Upvotes: 2

Views: 882

Answers (2)

CarComp
CarComp

Reputation: 2016

Heres how I implemented it in the import system to follow your answer. All future products will have the configuration like you said, and current ones are handled via jQuery

catalogInventoryStockItemUpdateEntity stock;

if (this.deliveryType != DeliveryTypes.Simple)
{
    stock = new catalogInventoryStockItemUpdateEntity
                {
                    use_config_manage_stockSpecified = true,
                    use_config_manage_stock = 0,
                    manage_stockSpecified = true,
                    manage_stock = 0,
                    backorders = 0,
                    is_in_stockSpecified = true,
                    is_in_stock = 1,
                    use_config_max_sale_qtySpecified = true,
                    use_config_max_sale_qty = 0,
                    max_sale_qtySpecified = true,
                    max_sale_qty = 1
                };
}
else
{

    stock = new catalogInventoryStockItemUpdateEntity
                {
                    manage_stockSpecified = true,
                    is_qty_decimal = 1,
                    is_qty_decimalSpecified = true,
                    qty = this.InventoryQuantity.ToString(CultureInfo.InvariantCulture),
                    backorders = 0,
                    is_in_stockSpecified = true,
                    is_in_stock = 1,
                    manage_stock = 1
                };
}

return stock;

Heres how i made the front end look a little nicer. I added a small fade effect, and the amount changes and explains why it is changing. I inserted the following into my theme /var/www/app/design/frontend/NKI/default/template/catalog/product/view.phtml

<script type="text/javascript">

jQuery(document).ready(function() {
    jQuery('#attribute501').change(function() {
            var x = jQuery(this).val();
            // If its not a physical book
            var qtyInput = jQuery('#theQty').find('#qty');
            jQuery(qtyInput).val(1);
            var qtyExplain = jQuery('#qtyExplain');

            if (x) {

                    if (x != 3) {
                            jQuery(qtyExplain).fadeIn('slow');
                            jQuery(qtyInput).attr("disabled",true);
                    } else {
                            jQuery(qtyExplain).fadeOut('slow');
                            jQuery(qtyInput).attr("disabled",false);
                    }
            } else {
                    jQuery(qtyExplain).fadeOut('slow');
                    jQuery(qtyInput).attr("disabled",false);

            }
    });

});

</script>

Also in /var/www/app/design/frontend/NKI/default/template/catalog/product/view/addtocart.phtml I changed it to this

<?php $_product = $this->getProduct() ?>

<?php if($_product->isSaleable()): ?>
    <div class="add-to-cart" style="width: 365px">
        <?php if(!$_product->isGrouped()): ?>
        <div id="qtyExplain" style="display:none">
                <p>Downloads are unlimited. Quantity is limited to one item.</p>
        </div>

        <div id="theQty" style="display: inline" >
        <label for="qty"><?php echo $this->__('Qty:') ?></label>
        <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
        </div>
        <?php endif; ?>
        <button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="productAddToCartForm.submit()"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
        <?php echo $this->getChildHtml('', true, true) ?>
    </div>
<?php endif; ?>

Also, for the shopping cart, i added the following code to the following file around line 154 /var/www/app/design/frontend/NKI/default/template/checkout/cart/item/default.phtml

 <?php
        if ($_item->getIsVirtual()): ?>
                <span><?php echo $_item->getQty();?></span>
        <?php else: ?>

        <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" />
        <?php endif; ?>

Upvotes: 0

Ian
Ian

Reputation: 1761

You can limit the number of items allowed in cart for a particular item by editing it and going to the "Inventory" tab. There are two settings: "Minimum qty allowed in shopping cart" and "Maximum qty allowed in shopping cart". Uncheck "Use config settings" for "maximum qty allowed" and set this as one.

By default both of these are "use config" which means it is also editable in the System -> Configuration -> Inventory tab as well.

Upvotes: 4

Related Questions