Question

I need to preset the product quantity on the product page.

NB1: my products have new yes/no attribute defined in backend called formula_price-active. NB2: for formula_price_active product, I pass parameters via product url to pre-configure my product page

I then extended the view.php class to add/overload custom functions :

    <?php
    class Mine_Quotemodule_Block_Catalog_Product_View extends Mage_Catalog_Block_Product_View
    {
        /**
         * Get default qty - either as preconfigured, or as 1.
         * Also restricts it by minimal qty.
         *
         * @param null|Mage_Catalog_Model_Product $product
         * @return int|float
         */
        public function getProductDefaultQty($product = null)
        {
            if (!$product) {
                $product = $this->getProduct();
            }

            $FPA_att_value = $product->getAttributeText('formula_price_active');

            if (isset($FPA_att_value) && (($FPA_att_value == 'yes') || ($FPA_att_value == 'oui')))
            {
                $params = Mage::app()->getRequest()->getParams();
                $urlParams = array();

                foreach ($params as $name=>$value)
                {
                    if ($name!="id") 
                    {
                    $urlParams[$name] = base64_decode($value);
                    }
                }

                if (isset($urlParams['Devis_Qty']))
                {
                    $qty = $urlParams['Devis_Qty'];
                }   
                else 
                {               
                    $qty = $this->getMinimalQty($product);
                    $config = $product->getPreconfiguredValues();
                    $configQty = $config->getQty();
                    if ($configQty > $qty) {
                        $qty = $configQty;
                    }
                }
            }
            else
            {
                $qty = $this->getMinimalQty($product);
                Mage::log('Qty'.$qty);

                $config = $product->getPreconfiguredValues();
                $configQty = $config->getQty();
                Mage::log('configQty '.$configQty);
                if ($configQty > $qty) {
                    $qty = $configQty;
                }

            }
            Mage::log('Final Qty'.$qty);
            return $qty;

        }

        public function testProductFP($product = null)
        {
             if (!$product) {
                $product = $this->getProduct();
            }

            $FPA_att_value = $product->getAttributeText('formula_price_active');

            if (isset($FPA_att_value) && (($FPA_att_value == 'yes') || ($FPA_att_value == 'oui')))
            {
                $params = Mage::app()->getRequest()->getParams();
                $urlParams = array();

                foreach ($params as $name=>$value)
                {
                    if ($name!="id") 
                    {
                    $urlParams[$name] = base64_decode($value);
                    }
                }

                if (isset($urlParams['Devis_Qty']))
                {
                    $result = true;
                }   
                else 
                {               
                    $result = false;
                }
            }
            else
            {
                $result = false;
            }

            return $result;
        }
...

and the view.phtml to add custom stuffs (but theorically has nothing to do with the current problem):

…

                <div class="extra-info">
                    <?php echo $this->getReviewsSummaryHtml($_product, 'default', false)?>

                    <!-- Modifs Perso -->
                    <!--<?php echo $this->getChildHtml('product_type_availability'); ?>-->
                    <?php echo $this->getDimHtml() ?>
                    <!-- ******************** -->

                </div>
…

This seems to work well: - case 1: If formula_price_active is set to No or if it is set to yes but the needed parameters are not present, the basic getDefaultQuantity method is used -case 2: when the formula_price_active is set to Yes and parameters are OK, it sets the wanted qty.

I then added a custom addtocart.phtml in order to disable the quantity input in case 2.

BUT, when I add it to my layout.xml the problem appears :

in case 2 it is working, but in case 1, the quantity is set to 0 ?

What is strange,is that the qty layout changes as defined…

Here is my layout.xml

  <?xml version="1.0"?>
    <layout version="0.1.0">
        <default>
            <reference name="content">
            </reference>
            <reference name="head">
                <action method="addItem"><type>skin_css</type><name>css/quote_form.css</name></action>
            </reference>
        </default>

        <routeurfrontend_index_index>
            <reference name="content">
                <block type="quotemodule/quoteformblock"  name="quoteform_quoteformblock"
                              template="quotemodule/quoteform.phtml" />
            </reference>
        </routeurfrontend_index_index>

        <catalog_product_view>
            <reference name="product.info">
                <action method="setTemplate">
                    <template>quotemodule/catalog/product/view.phtml</template>
                </action>
            </reference>

            <reference name="product.info.addtocart">
                <action method="setTemplate">
                    <template>quotemodule/catalog/product/view/addtocart.phtml</template>
                </action>
            </reference>
        </catalog_product_view>

    </layout>

Thank you for your help,

Was it helpful?

Solution

That was a stupid mistake…Sorry…

I based my addtocart.phtml on the wrong template (base/default instead of rwd/default). The base/default one does not replace the qty value by 1 if getProductDefaultQty returns null whereas the rwd/default does :

base/default addtocart.phtml

<input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />

rwd/default addtocart.phtml

<input type="text" pattern="\d*" name="qty" id="qty" maxlength="12" value="<?php echo max($this->getProductDefaultQty() * 1, 1) ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top