0

we have magento multi vendor site. we are giving option for vendor to edit all textfields [price, quantity, commision.... etc ] of single products and save all by clicking one "Update" button.

enter image description here

we are using following code for price text field. In same way we are using code for other text fields.

Phtml

<input class="ama1" type = "text" id = "price_<?php echo $products->getId(); ?>" onkeydown="validateNumbers(event)" name= "price[]" value = "<?php echo $products->getPrice(); ?>" style = ""/>

<input type="hidden" name="curr_<?php echo $products->getId(); ?>" id="curr_<?php echo $products->getId(); ?>" value="<?php echo $products->getPrice(); ?>" />

<p id="updatedprice_<?php echo $products->getId(); ?>" style = "display:none;color:red; position:relative; top:16px;">Updated</p>
<br/>

<button id="price_update_button_<?php echo $products->getId(); ?>" class="update" onclick="updateFieldPrice('<?php echo $products->getId(); ?>'); return false;" >
<span><span style="font-size:12px;"><?php echo $helper->__('Update') ?></span></span>
</button>

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

Javascript

function hideResetPrice(product_id,priceold) { 

var qtyId='#price_'+ product_id; 
var currprice='#curr_'+ product_id; 
var editLink="#price_edit_link_"+ product_id; 
var updateButton="#price_update_button_"+ product_id; 
var valueprice="#valueprice_"+ product_id; 
var resetButton="#price_reset_button_"+ product_id; 


$wk_jq(valueprice).show(); 
$wk_jq(qtyId).val( $wk_jq(currprice).val()); 
$wk_jq(editLink).show(); 

}



function showFieldPrice(product_id)
        {

            var qtyId='#price_'+ product_id;

            var editLink="#price_edit_link_"+ product_id;
            var valueprice="#valueprice_"+ product_id;
            var updateButton="#price_update_button_"+ product_id;
            var resetButton="#price_reset_button_"+ product_id;

            $wk_jq(qtyId).show();
            $wk_jq(valueprice).hide();

            $wk_jq(editLink).hide();
            $wk_jq(updateButton).show();
            $wk_jq(updateButton).prop('disabled', false);//just in case
            $wk_jq(resetButton).show();

            return false;


        }


function updateFieldPrice(product_id) 
{ 
var priceId = '#price_'+ product_id; 
var currprice='#curr_'+ product_id; 
var updatedqty = '#updatedprice_'+ product_id; 
var url ='<?php echo Mage::getUrl('marketplace/marketplaceaccount/updateFieldPrice/')?>'; 
$price = $wk_jq(priceId).val(); 
$wk_jq(currprice).val($price); 
new Ajax.Request(url, { 
method: 'post', 
parameters: {id: product_id, price: $price}, 
onComplete: function (transport) { 
//alert(transport.responseText); 

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

} 
}); 
}

Controller.php

public function updateFieldPriceAction(){
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);      
        $id= $this->getRequest()->getParam('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);

        try{
        $upd_price = $this->getRequest()->getParam('price');
        $product = Mage::getModel('catalog/product')->load($id);        
        //$product->setData('price', $upd_price);
        $product->setPrice($upd_price);

        //$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($id);
        //$stockItem->setData('manage_stock', 1);
        //$stockItem->setData('qty', $this->getRequest()->getParam('qty'));
        $product->save();

        echo $price = $product->getPrice();
        echo $name = $product->getName();

        $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'));


        //endif;
        }catch(Exception $e){
        echo "Not Saving"; exit;    
        Mage::log($e->getMessage());
        }

      }

If we edit all text fields , means once we click on update button, we will get message "Updated" below all the text fields. It's fine. But, Now if we edit single textfield also, we are getting "updated" message under all text fields.

"Updated" message should display only below the text fields we edited.

1

If you want to track every single field and display the update message only for those which were updated (correct me if i wrong) you must:

  • Track the actual values on load for each row (product).
  • Then in the Success block you can validate the new values vs the old ones to display the message and, of course, update the current values with the new ones.

Possible Approach Example

To get Initial Values:

Add a common class to each product row

<div class="product-row" id="product-row_<?php echo $productId ?>">
 <input name="updatedprice_" .../>
 <input name="updatedprice_" .../>
</div>

Then iterate it with jQuery on DOM load (just once):

var _currentValues = []

jQuery("div.product-row" ).each(function(index) {
    // get the id
    var _id = ($(this).attr('id')).split('_')[1]
    var data = {
        updateprice: jQuery("#updatedprice_" + _id ),
        updateqty: jQuery("#updatedqty_" + _id ),
        // ... other fields
    }
});

So in the success callback function (of the ajax call) you can now compare the updated data over the previous values and decide which message to show.

NOTE:

This is just a javascript workaround suggestion based on the code you already have, IMO this isn't the best way to do it, i prefer:

  • Create an array with products data in php (Block) and convert it to json (to track initial values)

  • Use array based names in form inputs instead name-id concatenation

  • Use json data in the ajax call instead paramaters.

  • Use error handles in both controller and ajax callbacks

  • Retrieve json data in the controller

But that involves a complete refactoring.

BTW: It's strongly recommended to keep all that logic in a javascript Object instead doing directly to the window object. this will give you a nice isolation level that could be useful to avoid common naming problems.

  • you are correct. I am new to JS. please give me the updated code for price and quantity , so that i will follow for other text fields. – Baby in Magento Dec 30 '15 at 20:18
  • This is complete code of that phtml page : pastebin.com/VKDR5kyZ – Baby in Magento Dec 30 '15 at 20:21
  • You could accomplish this in many ways, i can give you an idea about how to do it, but i'm not able to code the entire solution right now. – MauroNigrele Dec 30 '15 at 20:33
  • Okay, if you get free time, please give me updated code, until that time i will try using your idea. – Baby in Magento Dec 30 '15 at 20:36
  • i just updated the answer, take a look... it's all i can do for you today. – MauroNigrele Dec 30 '15 at 21:38

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.