【问题标题】:Magento AJAX Table reload on Product Page产品页面上的 Magento AJAX 表重新加载
【发布时间】:2023-03-17 20:03:01
【问题描述】:

我已经为此奋斗了一段时间。我有一个可配置的产品,它设置正确且成功。我还有一张桌子,它是根据儿童的每个版本的可用尺寸和数量制作的。请参阅此处:dev4.printpartnerships.com/flyer-printing

此表当前在页面加载时呈现,然后根据下拉框选择使用 jQuery 交换它。问题是它有点慢,我需要根据下拉列表动态重新加载表。例如,选择下拉纸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 来显示/隐藏。

干杯。

【问题讨论】:

  • 我不知道你的目标,但你可以使用 jQuery 数据表,它可以提供你想要的大多数东西。 jQuery DataTables
  • 嗨 Oguz,看起来不错,但我的问题不在于数据表本身,因为它工作得很好(请参阅我的示例页面)它正在与 Magento 一起发送 AJAX 请求。正如我所说,我之前在 Magento 中尝试过此操作,只是调用简单变量并获取方法错误等,通常指向诸如未加载必要类之类的事情。不过这是之前的尝试。
  • 如何加载所有必要的值,然后按选择过滤以消除性能问题。
  • 嗨 Oguz,我担心有很多必要的值,因此会出现性能问题。你能更详细地解释你的意思吗?我需要将它作为我的示例页面上的矩阵表,该表结构无法更改。
  • 我的意思是,在我给你的示例中 (DataTables) 有即时过滤器功能。因此,您可以查询产品的所有选项,然后通过 jQuery(选择框)进行过滤。

标签: ajax magento


【解决方案1】:

您需要创建一个控制器和一个动作,并让 ajax 请求通过那里。并根据需要创建块和布局。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多