So I came up with a solution that works just awesome. I added another controller action and a model to do the Magento Interactions during my ajax calls. So let me show you how it's done, I hope somebody can profit from this sooner or later :)
My new Action:
public function updateAction ()
{
//Instantiate Product Model
$productModel = Mage::getModel('doorconfig/product');
//Get Updated Values from the Model
$currentProduct = $productModel->getProduct($_POST);
$currentProductId = $currentProduct->getId();
$currentProductUrl = $currentProduct->getProductUrl();
$currentPrice = $productModel->getPrice($currentProductId);
$currentType = $this->getRequest()->getPost('doorconfig_type');
$currentSize = $this->getRequest()->getPost('doorconfig_size');
$currentProductBaseImgUrl = $productModel->getDoorBaseImgUrl($currentType,$currentSize);
//Populate Resultarray
$result = array("currentProductId"=>$currentProductId,"currentPrice"=>$currentPrice,"currentProductUrl"=>$currentProductUrl,"currentProductBaseImgUrl"=>$currentProductBaseImgUrl);
//Encode Result in JSON
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
return $result;
}
My model just got most of the business logic from my block, so nothing special to point out about that.
And finally the updated Ajax section which now triggers my new controller action, receives the result as a JSON encode and changes the values in the DOM:
<script type="text/javascript">
var $price = "";
var $baseImgUrl = "";
var $productUrl = "";
var $productId = "";
var $f = $j("#attributeform");
var $formData;
var $currentStoreUrl = "<?php echo $currentStoreUrl ?>";
function ajaxUpdate()
{
$j.ajax({
url: "/doorconfig/index/update",
type: "POST",
data: $formData,
dataType: "json",
success: function(data)
{
$productId = data.currentProductId;
$price = data.currentPrice;
$baseImgUrl = data.currentProductBaseImgUrl;
$productUrl = data.currentProductUrl;
$j("#result").text($price);
$j("#addtocart").attr('href', $currentStoreUrl + "checkout/cart/add?product=" + $productId + "&qty=1");
$j("#productimg").attr('src', $baseImgUrl);
console.log(data);
},
error: function(error)
{
console.log("Error:");
console.log(error);
alert("ERROR");
}
});
};
$j(document).ready(function()
{
$j("#result").text('<?php echo $defaultProductPrice; ?>');
$j("#addtocart").attr('href', '<?php echo $currentStoreUrl . "checkout/cart/add?product=" . $defaultProductId . "&qty=1" ?>');
$j("#moreinfo").attr('href', '<?php echo $defaultProductUrl; ?>');
$j("#productimg").attr('src', '<?php echo $defaultProductImgUrl; ?>');
$j("#attributeform")[0].reset();
$j("form[name=attributeform]").change(function ()
{
$formData = $f.serialize();
ajaxUpdate();
})
});
</script>
If you need any further explanation or wanna improve something please comment :)
I managed to add a field to the registration page and have it saved to the database. thanks to the help of this tutorial. I have added my code which has done the job.
Sorry there is no explanation to the code or an answer to what is customer_account_edit
and customer_account_create
and how is setData() parameter decided. But I thought this code may be helpful for anyone else adding new fields. Hopefully someone with more knowledge will expand on that part
config.xml
<global>
<fieldsets>
<customer_account>
<username>
<create>1</create>
<update>1</update>
<name>1</name>
</username>
</customer_account>
</fieldsets>
<resources>
<customforms_reginfo_setup>
<setup>
<module>Customforms_Reginfo</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customforms_reginfo_setup>
</resources>
</global>
setup-0.1.0.php
$installer = $this;
$installer->startSetup();
$this->addAttribute('customer', 'username', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Username',
'global' => 1,
'visible' => 1,
'required' => 1,
'user_defined' => 1,
'visible_on_front' => 1
));
Mage::getSingleton('eav/config')
->getAttribute('customer', 'username')
->setData('used_in_forms', array('adminhtml_customer', 'customer_account_edit','customer_account_create'))
->save();
$installer->endSetup();
Best Answer
EDIT: I managed to do it. Here is the solution if it can help :
First the .phtml form:
Th JS/ajax script (which send data to php controller and treat the answer):
and the controller.php file: