【问题标题】:Magento Reviews Form on custom page自定义页面上的 Magento 评论表单
【发布时间】:2015-01-29 18:55:31
【问题描述】:

我已使用自定义页面的 xml 布局将评论表单添加到自定义页面上。自定义页面位于我的帐户部分,显示客户订购的所有产品。

<reference name="content">
        <block type="review/form" name="product.info.review_form" as="review_form" template="review/form.phtml"/>
</reference>

这会将表单放在我的自定义页面的底部,但是我想在显示每个产品后调用评论表单。我尝试了以下方法:

 echo $this->getChild('review_form'); 

但它不起作用,我将遇到的另一个问题是将产品 ID 插入到表单操作中。我是不是走错路了?

更新完整代码:

<?php
if (Mage::getSingleton('customer/session')->isLoggedIn()) {

/* Get the customer data */
$customer       = Mage::getSingleton('customer/session')->getCustomer();
/* Get the customer's email address */
$customer_email = $customer->getEmail();

}

$collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', array(
'like' => $customer_email
));



$uniuqProductSkus = array();

foreach ($collection as $order) { 

    $order_id = $order->getId(); 
    $order = Mage::getModel("sales/order")->load($order_id); 
    $ordered_items = $order->getAllItems(); 
        foreach ($ordered_items as $item) 
        { 
        if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) { 
        continue; 
        } else { 
            array_push($uniuqProductSkus, $item->getProduct()->getSku()); 

            $_product                 = Mage::getModel('catalog/product')->load($item->getProductId());
            $product_small_image_path = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(200);
            $product_thumbnail_path   = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(150);
            $summaryData              = Mage::getModel('review/review_summary')->load($item->getProductId());


            echo "<li>";

            echo "<div class='previous-name'><p><a  style='color:black; font-weight:bold; font-size:14px;' href='" . $_product->getProductUrl() . "'>";
            echo $item->getName() . "</a></p></div>";

            echo "<div class='previous-image'><a href='" . $_product->getProductUrl() . "'>";
            echo "<img src='" . $product_small_image_path . "' />";
            echo "</a></div>";

            echo "<div class='previous-rating'>";
            echo "<p><a  style='color:black; font-weight:bold; font-size:14px;' href='" . $_product->getProductUrl() . "#product_tabs_review_tabbed'>Review this beer now</a></p>";

            echo $summaryData->getRatingSummary() . '% Would buy again <br/>';

            echo "<div class='rating-box' style='float:left;'>";
            echo "<div class='rating' style='width:" . $summaryData->getRatingSummary() . "%'></div></div>";
            echo "<div class='previous-clear'></div></div>";



            $productsreviews = Mage::getModel('review/review')->getProductCollection()->addCustomerFilter(Mage::getSingleton('customer/session')->getCustomerId())->setDateOrder();
            foreach ($productsreviews as $productsreview)
            {
                $product = Mage::getModel('catalog/product')->load($productsreview->getData('entity_pk_value')); 
            }

            echo $product->getRating();

            ?>

            <script type="text/javascript">
                    function toggle_visibility(id) {
                       var e = document.getElementById(id);
                       if(e.style.display == 'block')
                          e.style.display = 'none';
                       else
                          e.style.display = 'block';
                    }
                </script>



            <?php

            echo '<a href="#" onclick="toggle_visibility(\'ajaxpro-notice-form' . $_product->getId().'\');" >Review Now</a>';

            echo "<div id='ajaxpro-notice-form" . $_product->getId()."' class='ajaxpro-form' style='display:none'>";
            echo '<a href="#" onclick="toggle_visibility(\'ajaxpro-notice-form' . $_product->getId().'\');" class="btn-close ajaxpro-button" title="Remove This Item">Remove This Item</a>';
            $productId ='43';
            $this->getChild('review_form')->setData('productId', $productId);
            echo $this->getChildHtml('review_form', false);
            echo "</div>";

            /**echo "<div class='previous-button'>";
            echo '<button type="button" title="Add to Cart" class="button btn-cart" onclick="setLocation(\'';
            echo $this->helper('checkout/cart')->getAddUrl($_product);
            echo '\')"><span><span>Order Again</span></span></button>';
            echo "</div>";**/

            ?>

            <?php $i = $_product->getId();?>

            <div class='previous-button'>

                <form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>


                <?php if($_product->isSaleable()): ?>

                    <input type="text" name="qty" id="qty<?php echo $i;?>" maxlength="12" value="1" title="Qty" class="cartnum" />

                    <?php $qtyforaction = 'qty'.$i; ?>

                    <script type="text/javascript">
                    var qty = document.getElementById('qty').value;
                    document.getElementById('buttonaddcart').innerHTML = '<button type="button" class="addtocartbutton" onclick="setLocation(\'' + url + 'qty/' + qty + '/\')"></button>';
                    </script>


                    <div id="buttonaddcart">

                        <button type="button" class="addtocartbutton button btn-cart" onclick="setLocation('<?php echo $this->helper('checkout/cart')->getAddUrl($_product); ?>qty/')" >
                            <span><span>Order Again</span></span>
                        </button>

                    </div>          

                <?php else: ?>

                    <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>

                <?php endif; ?>

                </form>

            </div>


            <?php

            echo "<div class='previous-clear'></div>";

            echo "</li>";

    }
}
}
?> 

【问题讨论】:

    标签: php magento


    【解决方案1】:

    试试这个:

    echo $this->getChildHtml('review_form'); 
    

    此外,如果您想检索产品 ID,这完全取决于您的自定义页面的加载方式。我假设您使用的是自定义控制器,这是使用寄存器的最佳位置:

    Mage::register('current_product', $product);
    

    然后你可以在你的模板文件中调用Mage::registry('current_product')

    【讨论】:

    • 我试过 echo $this->getChildHtml('review_form');这也不起作用?
    • 请注意,您可以删除您引用的 XML 中的 template 定义。它是在块级别定义的。你也应该有 &lt;reference name="content"&gt;&lt;block type="mymodule/myblock" name="mymodule.myblock" template="mymodule/myblock.phtml"&gt;&lt;block type="review/form" name="product.info.review_form" as="review_form" /&gt;&lt;/block&gt;&lt;/reference&gt; 并在 mymodule/myblock.phtml 中调用 getChildHtml - 希望它有意义
    • 非常感谢!表单通过并填写
      除了产品 ID 之外的所有内容?
    • 确保您的自定义 URL 具有在“id”参数下定义的产品 ID。所以yourcontroller/youraction/id/productidhere。或将$this-&gt;getAction() 替换为Mage::getUrl('review/product/post', array('id' =&gt; $productId));
    • 我在 php 循环中有这段代码,所以我现在不确定如何每次都获得唯一的产品 ID?目前是空白吗?
    猜你喜欢
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 2012-02-01
    • 1970-01-01
    • 2013-11-27
    • 2013-01-14
    相关资源
    最近更新 更多