Magento:在类别页面上获取产品可配置选项

时间:2012-01-05 10:47:04

标签: php javascript magento

在我的Magento网站的类别列表页面中,我在该右侧有一列,当用户点击产品时,该列通过AJAX加载产品的视图页面。我能够从视图页面中检索所有内容,如图像,标签等,但我无法检索产品的可配置选项,因为它们存储为javascript变量。

关于如何检索该信息的任何想法?

编辑 - 更好的解释:我试图让用户直接从类别页面配置产品,向他们显示所有选项,而无需导航到每个产品的视图页面。到目前为止,我只能获得HTML,但下拉列表是空的。这就是因为所有的选择&它们的价格在视图页面中存储为javascript变量。所以我的问题是,我如何通过ajax调用获得这些选项&加载下拉列表&让它们完全按照产品视图页面的方式工作吗?

2 个答案:

答案 0 :(得分:1)

Magento使用JS,这是事实。它使用Mage_Catalog_Block_Product_View_Type_Configurable类来呈现可配置的产品。你应该对它的getJsonConfig()方法感兴趣。正如您所说,它在app/design/frontend/base/default/template/catalog/product/view/type/options/configurable.phtml模板中用于呈现执行magick的javascript代码。

因此,在模板中看起来像:

<script type="text/javascript">
    var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
</script>

在浏览器中看起来像:

<script type="text/javascript">
    var denyProduct = {"62":["12","9"]}        
    var spConfig = new Product.Config({"attributes":{"70":{"id":"70","code":"manufacturer","label":"Manufacturer","options":[{"id":"11","label":"Fischer","price":"10","oldPrice":"10","products":["61","66"]},{"id":"12","label":"Queen mum","price":"20","oldPrice":"20","products":["62"]}]},"122":{"id":"122","code":"size","label":"Maat","options":[{"id":"9","label":"M","price":"15","oldPrice":"15","products":["61","62"]},{"id":"10","label":"S","price":"0","oldPrice":"0","products":["66"]}]}},"template":"\u20ac\u00a0#{price}","basePrice":"150","oldPrice":"150","productId":"64","chooseText":"Kies een optie...","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":19,"currentTax":19,"inclTaxTitle":"Incl. BTW"}});
</script>

创建所有javascript选项的代码是(Mage_Catalog_Block_Product_View_Type_Configurable::getJsonConfig()方法):

$attributes = array();
$options    = array();
$store      = $this->getCurrentStore();
$taxHelper  = Mage::helper('tax');
$currentProduct = $this->getProduct();

$preconfiguredFlag = $currentProduct->hasPreconfiguredValues();
if ($preconfiguredFlag) {
    $preconfiguredValues = $currentProduct->getPreconfiguredValues();
    $defaultValues       = array();
}

foreach ($this->getAllowProducts() as $product) {
    $productId  = $product->getId();

    foreach ($this->getAllowAttributes() as $attribute) {
        $productAttribute   = $attribute->getProductAttribute();
        $productAttributeId = $productAttribute->getId();
        $attributeValue     = $product->getData($productAttribute->getAttributeCode());
        if (!isset($options[$productAttributeId])) {
            $options[$productAttributeId] = array();
        }

        if (!isset($options[$productAttributeId][$attributeValue])) {
            $options[$productAttributeId][$attributeValue] = array();
        }
        $options[$productAttributeId][$attributeValue][] = $productId;
    }
}

$this->_resPrices = array(
    $this->_preparePrice($currentProduct->getFinalPrice())
);

foreach ($this->getAllowAttributes() as $attribute) {
    $productAttribute = $attribute->getProductAttribute();
    $attributeId = $productAttribute->getId();
    $info = array(
       'id'        => $productAttribute->getId(),
       'code'      => $productAttribute->getAttributeCode(),
       'label'     => $attribute->getLabel(),
       'options'   => array()
    );

    $optionPrices = array();
    $prices = $attribute->getPrices();
    if (is_array($prices)) {
        foreach ($prices as $value) {
            if(!$this->_validateAttributeValue($attributeId, $value, $options)) {
                continue;
            }
            $currentProduct->setConfigurablePrice(
                $this->_preparePrice($value['pricing_value'], $value['is_percent'])
            );
            Mage::dispatchEvent(
                'catalog_product_type_configurable_price',
                array('product' => $currentProduct)
            );
            $configurablePrice = $currentProduct->getConfigurablePrice();

            if (isset($options[$attributeId][$value['value_index']])) {
                $productsIndex = $options[$attributeId][$value['value_index']];
            } else {
                $productsIndex = array();
            }

            $info['options'][] = array(
                'id'        => $value['value_index'],
                'label'     => $value['label'],
                'price'     => $configurablePrice,
                'oldPrice'  => $this->_preparePrice($value['pricing_value'], $value['is_percent']),
                'products'  => $productsIndex,
            );
            $optionPrices[] = $configurablePrice;
            //$this->_registerAdditionalJsPrice($value['pricing_value'], $value['is_percent']);
        }
    }
    /**
     * Prepare formated values for options choose
     */
    foreach ($optionPrices as $optionPrice) {
        foreach ($optionPrices as $additional) {
            $this->_preparePrice(abs($additional-$optionPrice));
        }
    }
    if($this->_validateAttributeInfo($info)) {
       $attributes[$attributeId] = $info;
    }

    // Add attribute default value (if set)
    if ($preconfiguredFlag) {
        $configValue = $preconfiguredValues->getData('super_attribute/' . $attributeId);
        if ($configValue) {
            $defaultValues[$attributeId] = $configValue;
        }
    }
}

$taxCalculation = Mage::getSingleton('tax/calculation');
if (!$taxCalculation->getCustomer() && Mage::registry('current_customer')) {
    $taxCalculation->setCustomer(Mage::registry('current_customer'));
}

将这些值与<select>元素相关联的代码位于Product.js文件中。

如果你只想获得在某个类别中使用的所有属性,这里是解决方案:假设你有一个变量$_productCollection填充了类别产品。然后你可以执行这个性能突破的代码:

$setIds = array();

foreach ($_productCollection as $k => $product)
{
    $setIds[] = $product->getAttributeSetId();
}

/** @var $collection Mage_Catalog_Model_Resource_Product_Attribute_Collection */
$collection = Mage::getResourceModel('catalog/product_attribute_collection');
$collection
    ->setItemObjectClass('catalog/resource_eav_attribute')
    ->setAttributeSetFilter($setIds)
    ->addStoreLabel(Mage::app()->getStore()->getId())
    ->setOrder('position', 'ASC');

$collection->load();

因此,您将获得$collection变量中的所有属性(包含重复)。

希望,这会以某种方式帮助你。

答案 1 :(得分:0)

这会占用大量的服务器资源,因为每种产品都有很多选项。最好的方法是使用ajax只在需要时加载所有选项。我发现这个扩展会首先加载颜色,然后当你将鼠标移到它上面时会给你所有的产品选项。

http://www.consofas.com/plugins/options-quickview-for-configurable-products-in-magento/