Magento Forum

   
Ajax call from custom product view page returns error, but browsing the ajax page directly with $_GET data in url works? 
 
aaspey
Jr. Member
 
Total Posts:  1
Joined:  2010-05-18
 

app/code/local/Newport/Customajaxmain/controllers/AjaxController.php:

class Newport_Customajaxmain_AjaxController extends Mage_Core_Controller_Front_Action {

    
public function indexAction() {
        $this
->loadLayout();
        
$this->renderLayout();
    
}
}

app/code/local/Newport/Customajaxmain/etc/config.xml:

<?xml version="1.0"?>
<config>
  <
modules>
    <
Newport_Customajaxmain>
      <
version>0.1.0</version>
    </
Newport_Customajaxmain>
  </
modules>
  <
frontend>
    <
routers>
      <
customajaxmain>
        <use>
standard</use>
        <
args>
          <
module>Newport_Customajaxmain</module>
          <
frontName>customajaxmain</frontName>
        </
args>
      </
customajaxmain>
    </
routers>
    <
layout>
      <
updates>
        <
customajaxmain>
          <
file>customajaxmain.xml</file>
        </
customajaxmain>
      </
updates>
    </
layout>
  </
frontend>
</
config>

app/etc/modules/Newport_Customajaxmain.xml:

<?xml version="1.0"?>
<config>
    <
modules>
        <
Newport_Customajaxmain>
            <
active>true</active>
            <
codePool>local</codePool>
        </
Newport_Customajaxmain>
    </
modules>
</
config>

app/design/frontend/default/kitchendoors/template/customajaxmain/customajaxmain.phtml:

if (isset($_GET["pricetype"]) || ($_GET["height"]) || ($_GET["width"]) || ($_GET["prodid"])) {
    
    $loadid 
$_GET["prodid"];
    
$pricetype $_GET["pricetype"]
    
$height $_GET["height"];
    
$width $_GET["width"];
    
    
$_product Mage::getModel('catalog/product')->load($loadid);
    
    
$productAttributeOptions $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);
    
    
$attributeOptions = array();
    
$attributeOptionsKey = array();
    
    foreach (
$productAttributeOptions as $productAttribute{
        
foreach ($productAttribute['values'as $attribute{
            $attributeOptions[] 
$attribute['store_label'];
            
$attributeOptionsKey[] $attribute['value_index'];
        
}
    }
    
    $splitFirst 
= array();
    
$splitHeight = array();
    
$splitWidth = array();
    
$productTable = array();           
    
$minHeight = array();
    
$maxHeight = array();
    
$minWidth = array();
    
$maxWidth = array();
    
    
$x 0;
    foreach(
$attributeOptions as $attributeOption){
        $splitFirst 
explode("&"$attributeOption);
        
$splitHeight explode("=>"$splitFirst[1]);
        
$splitWidth explode("=>"$splitFirst[2]);
        
$productTable[$x]['full_value'$attributeOption;
        
$productTable[$x]['price_type'$splitFirst[0];
        
$productTable[$x]['min_height'$splitHeight[0];
        
$productTable[$x]['max_height'$splitHeight[1];
        
$productTable[$x]['min_width'$splitWidth[0];
        
$productTable[$x]['max_width'$splitWidth[1];
        
$minHeight[] $splitHeight[0];
        
$maxHeight[] $splitHeight[1];
        
$minWidth[] $splitWidth[0];
        
$maxWidth[] $splitWidth[1];
        unset(
$splitFirst);
        unset(
$splitHeight);
        unset(
$splitWidth);
        
$x++;
    
}

    $i 
0;
    foreach(
$attributeOptionsKey as $attributeOptionKey){
        $productTable[$i][
'key'$attributeOptionKey;
        
$i++;
    
}

    
function searchForId($price_type$height$width$array{
        
foreach ($array as $key => $val{
            
if ($val['price_type'=== $price_type && $val['min_height'<= $height && $val['max_height'>= $height && $val['min_width'<= $width && $val['max_width'>= $width{
                $rangevalue 
$array[$key]['key'];
                echo 
$rangevalue;
            
}
        }
        
return null;
    
}
    
    
echo '<p>';
    
searchForId($pricetype$height$width$productTable);
    echo 
'</p>';
    
else {
    
echo 'No Data!!!';
}

app/design/frontend/default/kitchendoors/template/catalog/product/view_custom_drawer.phtml:

function finalMasterChange(price_typeheightwidthprodid
                        
var price_type price_type;
                        var 
height height;
                        var 
width width;
                        var 
prodid prodid;
                        
jQuery.ajax({                           
                            url
"/customajaxmain/ajax/index",
                            
type"GET",                            
                            
data{ pricetypeprice_typeheightheightwidthwidthprodidprodid },
                            
success: function(data{
                              
var content jQuery(data).text(); 
                              
alert(content);
                              
jQuery('#price_type_drop').val(content);  
                            
},
                            
error: function() {
                                alert(
'Error occured');
                            
}
                        }
);    
                    
}

I will attach all relevant full copies of files.

Basically when the finalMasterChange function runs on the view_custom_drawer.phtml page I recieve the following error:

Fatal Error: Call to undefined method:
Mage_Catalog_Model_Product_Type_Simple::getConfigurableAttributesAsArray() in customajaxmain.phtml on line 12 which is this line:
$productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);

For some reason this seems to be returning the product type as simple even though the product id used is for a configurable product?

Also if you run the custom page directly including the $_GET data in the URL there is no error received and the page returns the desired value:

http://kitchendoors.newportwebdesign.co.uk/customajaxmain/ajax/index?pricetype=price&height;=150&width;=300&prodid;=9038

So why does it fail when the same page is run using an ajax call from the custom product view page?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top