2012-03-19 72 views
2

我一直在与此战斗了一段时间。我有一个可以正确设置并成功完成的可配置产品。我还有一张桌子,根据儿童的基础上产生的每个版本的产品的大小和数量。看到这里:dev4.printpartnerships.com/flyer-printingMagento AJAX在产品页面上重新加载

这张表当前呈现在页面加载然后使用jQuery来交换它取决于下拉框选择。问题是它有点慢,我需要根据下拉列表动态重新加载JUST表。例如,当选择下拉式3,纸张时,它可能会重新加载表格。

<div id="matrix-container"> 
    <?php $attributeSetName = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName(); ?> 
    <?php $stock = $_product->getResource()->getAttribute('stock'); ?> 
    <?php if($stock->usesSource()):?> 
    <?php $options = $stock->getSource()->getAllOptions(false); ?> 
     <?php foreach ($options as $k => $v):?> 
      <?php $options[$k] = $v['label']; ?> 
      <?php $stockQuery = $options[$k].' '.$attributeSetName; ?> 
      <?php echo Mage::getModel('catalog/product_type_configurable')->getTable($_product, $stockQuery); ?> 
     <?php endforeach;?> 
    <?php endif;?> 
</div> 

这里的功能:这是由这两个函数,这调用来完成

public function getMatrixData($requiredAttributeIds = null, $product = null, $stock) 
{ 
    Varien_Profiler::start('CONFIGURABLE:'.__METHOD__); 
    $this->_usedProducts = '_cache_instance_products'; 
    if ($this->getProduct($product)->hasData($this->_usedProducts)) { 
     if (is_null($requiredAttributeIds) 
      and is_null($this->getProduct($product)->getData($this->_configurableAttributes))) { 
      $this->getConfigurableAttributes($product); 
      Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__); 
      return $this->getProduct($product)->getData($this->_usedProducts); 
     } 

     $usedProducts = array(); 
     $collection = $this->getUsedProductCollection($product) 
      ->addAttributeToSelect('*') 
      ->addFieldToFilter('name', array('like' => '%'.$stock.'%')); 

     if (is_array($requiredAttributeIds)) { 
      foreach ($requiredAttributeIds as $attributeId) { 
       $attribute = $this->getAttributeById($attributeId, $product); 
       if (!is_null($attribute)) 
        $collection->addAttributeToFilter($attribute->getAttributeCode(), array('notnull'=>1)); 
      } 
     } 

     foreach ($collection as $item) { 
      $usedProducts[] = $item; 
     } 

     $this->getProduct($product)->setData($this->_usedProducts, $usedProducts); 
    } 
    Varien_Profiler::stop('CONFIGURABLE:'.__METHOD__); 
    return $this->getProduct($product)->getData($this->_usedProducts); 
} 

public function getTable($product = false, $stock) 
{ 
    if (!$product) return false; 
     $childProducts = $this->getMatrixData(null, $product, $stock); 
     $x = array(); 
     $r = ''; 

      foreach ($childProducts as $children){ 
       $x[$children->getAttributeText('quantity')][$children->getAttributeText('size')] = array('id'=>$children->getId(), 'price'=>number_format($children->getPrice(),'2'), 'name'=>$children->getName()); 
      } 
     ksort($x); 
     $r .= '<table id="'.strtolower(str_replace(' ','-',$stock)).'" class="matrix"><tr><th></th>'; 

      foreach(array_keys(current($x)) as $size){ 
       $r .= '<th>'.$size.'</th>'; 
      } 
     $r .= '</tr>'; 

      foreach($x as $quantity => $data){ 
       $r .= '<tr><th>'.$quantity.'</th>'; 
        foreach($data as $item){ 
         $r .= '<td><a href="/checkout/cart/add?product='.$item[id].'" title="Add '.$item[name].' to basket">£'.$item[price].'</a></td>'; 
        } 
       $r .= '</tr>'; 
      } 
     $r .= '</table>'; 
     return $r; 
    } 

这工作得很好,因为它是和表加载完美的,但是我想AJAX的他们,由将下拉值作为数据发送到AJAX脚本并重新加载表格,而不是加载页面加载和显示/隐藏的所有表格。我之前在Magento尝试过使用AJAX,除了问题之外,我想知道是否有人可以给我一个我可以编辑的真实世界的例子或任何可以解决我的问题的方法。

正如你所看到的,我拥有所有的代码和逻辑,它只是让它重新加载而不是jQuery来显示/隐藏。

干杯。

+0

我不知道你的目标,但你可以使用jQuery的数据表,是可用的大多数东西,你想要的。 [jQuery DataTables](http://www.datatables.net/) – 2012-03-19 11:05:18

+0

嗨Oguz,看起来不错,但我的问题不是数据表本身,因为它工作得很好(请参阅我的示例页面)它正在与Magento发送一个AJAX请求。正如我所说我以前在Magento中尝试过,只是调用简单变量并获取方法错误等,通常指向类似的东西,如必要的类不加载。虽然这是以前的尝试。 – 2012-03-19 11:14:49

+0

如何加载所有必要的值,然后通过选择进行过滤以消除性能问题。 – 2012-03-19 11:23:17

回答

相关问题