产品页面上的Magento AJAX表重新加载

时间:2012-03-19 10:28:01

标签: ajax magento

我一直在争论这一段时间。我有一个可配置的产品,设置正确,成功。我还有一张桌子,根据孩子的每个版本的尺寸和数量生成。请参见:dev4.printpartnerships.com/flyer-printing

此表当前在页面加载时呈现,然后使用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并且只有问题,我想知道是否有人可以给我一个真实世界的例子,我可以编辑或解决我的问题。

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

干杯。

1 个答案:

答案 0 :(得分:0)

您需要创建一个控制器和一个动作,并让ajax请求通过那里。如果你愿意,可以创建块和布局。

以下是一个非常简单的示例:http://subesh.com.np/2009/11/working-with-ajax-in-magento/

它链接到您可以用作基础的扩展程序:http://www.magentocommerce.com/magento-connect/ajaxify-8411.html