dropdown attribute is not saving in database
-
20-10-2020 - |
题
we are using magento multi vendor site.
In frontend we gave an option for vendor update product attributes
we are using following code to update custom attribute "local" .its working fine.
Catalog Input Type for Store Owner : Textfield
phtml
<?php $attribute = $products->getResource()->getAttribute('local');?>
<?php if($attribute):?>
<?php $attribute_value = $attribute ->getFrontend()->getValue($products); ?>
<input class="ama1" type = "text" id = "local_<?php echo $products->getId(); ?>" onkeydown="validateNumbers(event)" name= "local[]" value = "<?php echo $attribute_value; ?>" style = ""/>
<?php endif; ?>
<input type="hidden" name="localcurr_<?php echo $products->getId(); ?>" id="localcurr_<?php echo $products->getId(); ?>" value="<?php echo $products->getLocal(); ?>" />
<p id="updatedlocal_<?php echo $products->getId(); ?>" style = "display:none;color:red; position:relative; top:16px;">Updated</p>
<br/>
<button id="local_update_button_<?php echo $products->getId(); ?>" class="update" onclick="updateFieldLocal('<?php echo $products->getId(); ?>'); return false;" >
<span><span style="font-size:12px;"><?php echo $helper->__('Update') ?></span></span>
</button>
<button id="local_reset_button_<?php echo $products->getId(); ?>" type="reset" class="cancel" onclick="hideResetLocal('<?php echo $products->getId(); ?>','<?php echo $products->getPrice(); ?>'); return false;">
<span><span><?php echo $helper->__('Cancel') ?></span></span>
</button>
</span>
JS
function hideResetLocal(product_id,localold) {
var qtyId='#local_'+ product_id;
var currlocal='#localcurr_'+ product_id;
var editLink="#local_edit_link_"+ product_id;
var updateButton="#local_update_button_"+ product_id;
var valuelocal="#valuelocal_"+ product_id;
var resetButton="#local_reset_button_"+ product_id;
$wk_jq(valuelocal).show();
$wk_jq(qtyId).val( $wk_jq(currlocal).val());
$wk_jq(editLink).show();
}
function showFieldLocal(product_id)
{
var qtyId='#local_'+ product_id;
var editLink="#local_edit_link_"+ product_id;
var valuelocal="#valuelocal_"+ product_id;
var updateButton="#local_update_button_"+ product_id;
var resetButton="#local_reset_button_"+ product_id;
$wk_jq(qtyId).show();
$wk_jq(valuelocal).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 updateFieldLocal(product_id)
{
var localId = '#local_'+ product_id;
var currlocal='#localcurr_'+ product_id;
var updatedqty = '#updatedlocal_'+ product_id;
var url ='<?php echo Mage::getUrl('marketplace/marketplaceaccount/updateFieldLocal/')?>';
$local = $wk_jq(localId).val();
$wk_jq(currlocal).val($local);
new Ajax.Request(url, {
method: 'post',
parameters: {id: product_id, local: $local},
//parameters: {id: product_id, local: $local},
onComplete: function (transport) {
//alert(transport.responseText);
jQuery(updatedqty).show().delay(2000).fadeOut();
}
});
}
Controller.php
public function updateFieldLocalAction(){
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));
try{
$upd_local = $this->getRequest()->getParam('local');
$product = Mage::getModel('catalog/product')->load($id);
$product->setLocal($upd_local);
$product->save();
echo $local = $product->getLocal();
echo $name = $product->getName();
$response['message'] = 'Your Product Is Been Sucessfully Updated';
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}catch(Exception $e){
echo "Not Saving"; exit;
Mage::log($e->getMessage());
}
}
In the same way, we are using following code to update custom attribute : "cod". but its not saving in database.
Catalog Input Type for Store Owner : Dropdown
phtml
<?php $attribute = $products->getResource()->getAttribute('cod');?>
<?php if($attribute):?>
<?php $attribute_value = $attribute ->getFrontend()->getValue($products); ?>
<input class="ama1" type = "text" id = "cod_<?php echo $products->getId(); ?>" name= "cod[]" value = "<?php echo $attribute_value; ?>" style = ""/>
<?php endif; ?>
<input type="hidden" name="codcurr_<?php echo $products->getId(); ?>" id="codcurr_<?php echo $products->getId(); ?>" value="<?php echo $products->getCod(); ?>" />
<p id="updatedcod_<?php echo $products->getId(); ?>" style = "display:none;color:red; position:relative; top:16px;">Updated</p>
<br/>
<button id="cod_update_button_<?php echo $products->getId(); ?>" class="update" onclick="updateFieldCod('<?php echo $products->getId(); ?>'); return false;" >
<span><span style="font-size:12px;"><?php echo $helper->__('Update') ?></span></span>
</button>
<button id="cod_reset_button_<?php echo $products->getId(); ?>" type="reset" class="cancel" onclick="hideResetCod('<?php echo $products->getId(); ?>','<?php echo $products->getPrice(); ?>'); return false;">
<span><span><?php echo $helper->__('Cancel') ?></span></span>
</button>
</span>
JS
function hideResetCod(product_id,codold) {
var qtyId='#cod_'+ product_id;
var currcod='#codcurr_'+ product_id;
var editLink="#cod_edit_link_"+ product_id;
var updateButton="#cod_update_button_"+ product_id;
var valuecod="#valuecod_"+ product_id;
var resetButton="#cod_reset_button_"+ product_id;
$wk_jq(valuecod).show();
$wk_jq(qtyId).val( $wk_jq(currcod).val());
$wk_jq(editLink).show();
}
function showFieldCod(product_id)
{
var qtyId='#cod_'+ product_id;
var editLink="#cod_edit_link_"+ product_id;
var valuecod="#valuecod_"+ product_id;
var updateButton="#cod_update_button_"+ product_id;
var resetButton="#cod_reset_button_"+ product_id;
$wk_jq(qtyId).show();
$wk_jq(valuecod).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 updateFieldCod(product_id)
{
var codId = '#cod_'+ product_id;
var currcod='#codcurr_'+ product_id;
var updatedqty = '#updatedcod_'+ product_id;
var url ='<?php echo Mage::getUrl('marketplace/marketplaceaccount/updateFieldCod/')?>';
$cod = $wk_jq(codId).val();
$wk_jq(currcod).val($cod);
new Ajax.Request(url, {
method: 'post',
parameters: {id: product_id, cod: $cod},
//parameters: {id: product_id, cod: $cod},
onComplete: function (transport) {
//alert(transport.responseText);
jQuery(updatedqty).show().delay(2000).fadeOut();
}
});
}
Controller.php
public function updateFieldCodAction(){
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));
try{
$upd_cod = $this->getRequest()->getParam('cod');
$product = Mage::getModel('catalog/product')->load($id);
$product->setCod($upd_cod);
$product->save();
echo $cod = $product->getCod();
echo $name = $product->getName();
$response['message'] = 'Your Product Is Been Sucessfully Updated';
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
}catch(Exception $e){
echo "Not Saving"; exit;
Mage::log($e->getMessage());
}
}
解决方案
replace
<input class="ama1" type = "text" id = "cod_<?php echo $products->getId(); ?>" name= "cod[]" value = "<?php echo $attribute_value; ?>" style = ""/>
with
<select id = "cod_<?php echo $products->getId(); ?>" name= "cod[]" ">
<option value="0" <?php if($products->getCod()==0) { echo "selected";} ?>>No</option>
<option value="1"<?php if($products->getCod()==1) { echo "selected";} ?>>Yes</option>
</select>