2

I am in need to get the Json Config by product id or sku in the product listing page for a particular category.

I can see that getJsonConfig is decalred in Product options block where only one particular product is showing thats why it can show Json Config without saying the product id. But I am in product list page.

Is there any way to get it like below?

$this->getJsonConfig($productId);
4

2 回答 2

5
$this->getJsonConfig()

该方法是一部分

class Mage_Catalog_Block_Product_View

它调用

 /* @var $product Mage_Catalog_Model_Product */
 $product = $this->getProduct();

public function getProduct()
{
    if (!Mage::registry('product') && $this->getProductId()) {
        $product = Mage::getModel('catalog/product')->load($this->getProductId());
        Mage::register('product', $product);
    }
    return Mage::registry('product');
}

因此,要使用它(在任何其他页面中),您必须在块实例中设置产品,如下所示:

Mage::register('product', $_product); // add the product object in the registry
$block = Mage::getBlockSingleton('Mage_Catalog_Block_Product_View'); // Instantiate the product view block
echo $block->getJsonConfig(); 
于 2013-05-24T21:23:10.137 回答
4

我为可配置的产品视图页面找到了一个简单的解决方案。

希望这会有所帮助。

    if ($_product->isConfigurable())
      {  $block1 = Mage::app()->getLayout()->createBlock('catalog/product_view');
         $block1->setProduct($_product);
         $configPrice = $block1->getJsonConfig();   //var_dump( $config = $block->getJsonConfig`enter code here`()  );

         $block2 = Mage::app()->getLayout()->createBlock('catalog/product_view_type_configurable');
         $block2->setProduct($_product);
         $config = $block2->getJsonConfig();  
      }

<?php if ($_product->isConfigurable()): ?>
   <script type="text/javascript">
       var optionsPrice = new Product.OptionsPrice(<?php echo $configPrice ?>);
   </script>

<script type="text/javascript">
               var spConfig = new Product.Config(<?php echo $config ?>);
            </script>
<?php endif; ?>
于 2014-07-22T14:19:54.953 回答