Skip to content

Instantly share code, notes, and snippets.

@devjosh12
Created January 28, 2015 15:22
Show Gist options
  • Save devjosh12/ae3a4f481e447594dcde to your computer and use it in GitHub Desktop.
Save devjosh12/ae3a4f481e447594dcde to your computer and use it in GitHub Desktop.
Update custom options magento in cart page
use the onchange() function and pass parameters like this in default.phtml
Just place below code:
<?php if ($_options = $this->getOptionList()):
$_customOptions = $_item->getProduct()->getOptions();
if(!empty($_customOptions)){
foreach ($_options as $_option) {
$row['label'][]=$_option['label'];
$myrow[$_option['label']][]=$_option['value'];
}
$optStr='';
foreach($_customOptions as $optionKey =>$optionVal){
$optStr.= "<label class='required' style='width: 45%;'>".$optionVal->getDefaultTitle()."</label>";
$optStr.= "<select style='width: 45%; min-width: 70px;' id='option_".$optionVal->getOptionId() .'_'.$_item->getId()."' onchange='updateproduct(".$_item->getId().",".$optionVal->getOptionId() .",this); 'name='options[".$optionVal->getOptionId() ."]'>";
$optStr.= "<option value=''>--Please Select--</option>";
foreach($optionVal->getValues() as $valuesKey => $valuesVal) {
$selected='';
if(in_array($optionVal->getDefaultTitle(),$row['label']))
{
if(in_array($valuesVal->getTitle(),$myrow[$optionVal->getDefaultTitle()]))
$selected='selected';
}
$optStr.= "<option value='".$valuesVal->getId()."' $selected >".$valuesVal->getTitle()."</option>";
}
$optStr.= "</select><br />";
}
echo($optStr);
}else{
?>
<dl class="item-options">
<?php foreach ($_options as $_option) : ?>
<?php $_formatedOptionValue = $this->getFormatedOptionValue($_option) ?>
<dt><?php echo $this->escapeHtml($_option['label']) ?></dt>
<dd<?php if (isset($_formatedOptionValue['full_view'])): ?> class="truncated"<?php endif; ?>><?php echo $_formatedOptionValue['value'] ?>
<?php if (isset($_formatedOptionValue['full_view'])): ?>
<div class="truncated_full_value">
<dl class="item-options">
<dt><?php echo $this->escapeHtml($_option['label']) ?></dt>
<dd><?php echo $_formatedOptionValue['full_view'] ?></dd>
</dl>
</div>
<?php endif; ?>
</dd>
<?php endforeach; ?>
</dl>
<?php }
endif;?>
Then in updateproduct script function, paste below code :
<script>
function updateproduct(itemid,optionid,obj)
{
var value=jQuery(obj).val();
/*var value= jQuery('#option_'+optionid+'_'+itemid+':option:selected').val();
alert(value);*/
postdate=$(obj).serialize()
//var postdata='{option['+optionid+']:' +value+'}';
jQuery.ajax({
url :'<?php echo $this->getUrl('') ?>checkout/cart/myUpdate/id/'+itemid ,
type: "POST",
data: postdate,
});
}
</script>
Then in Checkout/controllers/CartController.php paste below function :
public function myUpdateAction()
{
$params = $this->getRequest()->getParams();
$itemID = $params['id'];
$item = Mage::getSingleton('checkout/session')->getQuote()->getItemById($itemID);
$options = $item->getOptions();
foreach ($params['options'] as $key=> $value) {
$key=$key;
$value=$value;
}
foreach ($options as $option) {
$str= $option->getCode();
if($str == 'option_'.$key)
{
$option->setValue($value);
}
}
$item->setOptions($options)->save();
Mage::getSingleton('checkout/cart')->save();
echo 'update';
}
}
@kalpeshsassyinfotech
Copy link

hello devjosh12

ajax response not working error return 404 status

@MaxsHub
Copy link

MaxsHub commented Jul 19, 2018

code not working on page checkout/cart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment