0

I am using an extension, in that they wrote some code in phtml and in some controller file. I posted the code below.

But I want to know how it's linked, I guess they are using some get(), but I believe there should be common words in both the file, then only it will work. but in below code, I can't find any common words.

Also without the help of the controller file, can we update using Ajax?

<?php
$selllermpassignproduct=Mage::getModel('mpassignproduct/mpassignproduct')->getAssignProDetails($products->getId());

//Zend_Debug::dump($selllermpassignproduct,null,true);
$stock_item=Mage::getModel('cataloginventory/stock_item')->loadByProduct($products);

 $SellerQty=isset($selllermpassignproduct['sellerqty'])?$selllermpassignproduct['sellerqty']:
 $stock_item->getQty();
$assignqty=isset($selllermpassignproduct['assignqty'])?$selllermpassignproduct['assignqty']:0;
?>


<input type = "text" id = "qty_<?php echo $products->getId(); ?>" onkeydown="validateNumbers(event)" name = "stock<?php echo $j ?>" class="ama1" value = "<?php echo (int) $SellerQty; ?>" />



<span class="label wk_action" id="edit_link_<?php echo $products->getId(); ?>">
<!-- <img onclick="showField('<?php echo $products->getId(); ?>'); return false;" src="<?php echo $this->getSkinUrl('marketplace/images/icon-edit.png'); ?>"/> -->
</span>  

<p id="updatedqty_<?php echo $products->getId(); ?>" style = "display:none;color:red;">Updated</p>
<br/>


<button id="update_button_<?php echo $products->getId(); ?>" class="buttons" onclick="updateField('<?php echo $products->getId(); ?>',<?php echo $assignqty;?>); return false;" style="display:none" >
<span><span style="font-size:12px;"><?php echo $helper->__('Update') ?></span></span>
</button>

<button id="reset_button_<?php echo $products->getId(); ?>" type="reset" class="cancel" onclick="hideReset('<?php echo $products->getId(); ?>'); return false;"  >
<span><span><?php echo $helper->__('Cancel') ?></span></span>
</button>


<script>

      function hideReset(product_id) {

            var qtyId='#qty_'+ product_id;
            var editLink="#edit_link_"+ product_id;
            var updateButton="#update_button_"+ product_id;
            var resetButton="#reset_button_"+ product_id;

            $wk_jq(qtyId).hide();
            $wk_jq(editLink).show();
            $wk_jq(updateButton).hide();
            $wk_jq(resetButton).hide();
        }




             function updateField(product_id,assignqty)
        {
            var qtyId = '#qty_'+ product_id;
            var valueId = '#valueqty_'+ product_id;
            var updatedqty = '#updatedqty_'+ product_id;


            var editLink = "#edit_link_"+ product_id;
            var updateButton = "#update_button_"+ product_id;
            var resetButton = "#reset_button"+ product_id;
            var url ='<?php echo Mage::getUrl('marketplace/marketplaceaccount/updateField/')?>';

            //$wk_jq(qtyId).toggle()

            $wk_jq(editLink).hide();
            //$wk_jq(updateButton).show();
            $wk_jq(resetButton).show();

            $qty = $wk_jq(qtyId).val();
            jQuery(valueId).html($qty);
            //hideReset(product_id);

            var tmpQty=parseInt(assignqty)+ parseInt($qty) ;

            new Ajax.Request(url, {
                method: 'post',
                parameters: {id: product_id, qty: $qty},
                onComplete: function (transport) {
                    //alert(transport.responseText);
                    jQuery(priceId).val($price);
            // $wk_jq(priceId).setValue($price);

                    //jQuery(updatedqty).show().delay(2000).fadeOut();

                    $updateButton.prop('disabled', false);

                    // $wk_jq(qtyId).setValue($qty);



                }
            });
        }



        </script>

Controllers

<?php   

   public function updateFieldAction(){
        $id= $this->getRequest()->getParam('id');
        $selllermpassignproduct=Mage::getModel('mpassignproduct/mpassignproduct')->getAssignProDetails($id);
        $customerid=Mage::getSingleton('customer/session')->getCustomerId();
        $collection_product = Mage::getModel('marketplace/product')->getCollection()->addFieldToFilter('mageproductid',array('eq'=>$id))->addFieldToFilter('userid',array('eq'=>$customerid));
        //Mage::getSingleton('core/session')->setEditProductId($id);
         $assignqty=isset($selllermpassignproduct['assignqty'])?$selllermpassignproduct['assignqty']:0;
        $newqty=$this->getRequest()->getParam('qty')+$assignqty;
        $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);
        $stockItem->setData('manage_stock', 1);
        $stockItem->setData('qty', $newqty);

        $stockItem->save();

        $response['message'] = 'Your Product Is Been Sucessfully Updated';
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response)); 
        //Mage::getSingleton('core/session')->addSuccess(Mage::helper('marketplace')->__('Your Product Is Been Sucessfully Updated'));
      }
1

In your template's function function updateField(product_id,assignqty), you see

var url ='<?php echo Mage::getUrl('marketplace/marketplaceaccount/updateField/')?>';

Which generates a url like http://example.com/marketplace/marketplaceaccount/updateField where you will be sending Ajax request. It will hit updateFieldAction() method defined in your controller.

Now, in your controller, you will get data sent from template form using $id= $this->getRequest()->getParam('id');.

Next, you have $selllermpassignproduct=Mage::getModel('mpassignproduct/mpassignproduct')->getAssignProDetails($id); which is used to get instance of Model with mpassignproduct/mpassignproduct. In extension's etc/config.xml file, you will find nodes like

<global>
        <models>
            <mpassignproduct>
                <class>Namespace_Modulename_Model</class>

Which will give your model path, so Mage::getModel('mpassignproduct/mpassignproduct'); gives an instance of class Namespace_Modulename_Model_Mpassignproduct and getAssignProDetails() function will be defined in your model, which by the given name seems will take an argument product id, and return assigned product details of the product whose id you will send as function argument.

  • Nice, thanks for awesome explaination. just for experiment, i used public function updateFielAction() instead of updateFieldAction() . but still it working, after i changed the function name ? – Baby in Magento Feb 28 '16 at 7:36
  • Where you've changed it? Clear your magento/browser cache and it should work. Also, have you changed updateFileAction() to updateFielAction() or you have added new function updateFielAction() to test it ? – amitshree Feb 28 '16 at 7:39
  • I didt added, but just renamed it. cache was disabled in site before doing changes. but you are right. it''s browser problem. – Baby in Magento Feb 28 '16 at 7:45
1

You can't update field value in database without help of controller.

controllers are responsible for processing all requests made via a url.

From your code you request to update qty field.

Simply increase value of field you can use jquery but if you want to update field in model you need to call controller for save field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.