【问题标题】:Magento: Add textboxx on product details page for custom sizeMagento:在产品详细信息页面上添加 textboxx 以自定义尺寸
【发布时间】:2012-08-26 09:30:31
【问题描述】:

我在一个珠宝网站上工作(在 magento 1.7 中开发),其中我有一个戒指产品。在戒指产品的详细信息页面上,我的客户想要显示一个文本框,用户将在其中输入尺寸相关的详细信息。

产品的价格不会因输入的尺寸而异,此尺寸字段仅供客户参考,以便他们可以根据需要制作合适尺寸的戒指。

我已经检查过了,但 magento 不允许在可配置产品上使用文本框。

有没有人知道任何扩展(免费或付费)或任何代码可以帮助我在 magento 中实现此功能。

谢谢。

【问题讨论】:

    标签: magento magento-1.7


    【解决方案1】:

    在您的主题中,对于文件:template/checkout/cart.phtml 为购物车项目添加新标题以及其他标题。

    1
    <th><?php echo $this->__('Comments') ?></th>
    In the file: template/checkout/cart/item/default.phtml
    

    添加新列

    1
    <td class="a-center">
    2
    <textarea name="cart[<?php echo $_item->getId() ?>][comments]" rows="3" cols="20"><?php echo $_item->getItemcomment() ?></textarea>
    3
    </td>
    

    对于旧版本的 Magento,它将是:

    1
    <td class="a-center">
    2
    <textarea name="cart[<?php echo $_item->getId() ?>][comments]" rows="3" cols="20"><?php echo $this->getItemItemcomment($_item) ?></textarea>
    3
    </td>
    

    下一步是在客户更新购物车时将评论保存在数据库中。

    因此,在表格“sales_flat_quote_item”中添加一个新字段“itemcomment”。 (对于旧版本的 Magento,该表为“sales_quote_item”)

    现在我们将添加执行数据库操作的代码。为此,我们需要修改文件: app/code/core/Mage/Checkout/Model/Cart.php (注意:如果您打算升级您的 Magento 设置,请将此文件复制到本地并修改。)

    这里我们需要向函数 updateItems() 添加一些代码,这样函数现在应该如下所示:

    01
    public function updateItems($data)
    02
    {
    03
        Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data));
    04
    
    05
        foreach ($data as $itemId => $itemInfo) {
    06
    
    07
            $item = $this->getQuote()->getItemById($itemId);
    08
            if (!$item) {
    09
                continue;
    10
            }
    11
    
    12
            if (!empty($itemInfo['remove']) || (isset($itemInfo['qty']) && $itemInfo['qty']=='0')) {
    13
                $this->removeItem($itemId);
    14
                continue;
    15
            }
    16
    
    17
            $qty = isset($itemInfo['qty']) ? (float) $itemInfo['qty'] : false;
    18
            if ($qty > 0) {
    19
                $item->setQty($qty);
    20
            }
    21
    
    22
        /* Start: Custom code added for comments */
    23
        if(!empty($itemInfo['comments'])) {
    24
    
    25
            $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    26
    
    27
            # make the frame_queue active
    28
            $query = "UPDATE `sales_flat_quote_item` SET itemcomment = '".$itemInfo['comments']."' where item_id = $itemId";
    29
            $write->query($query);
    30
    
    31
            $item->setItemcomment($itemInfo['comments']);
    32
        }
    33
        /* End: Custom code added for comments */
    34
    
    35
        }
    36
    
    37
        Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data));
    38
        return $this;
    39
    }
    

    在管理员中显示评论 -> 查看订单

    在下面的文件中添加一个新函数 getItemcomment(): app/code/core/Mage/Adminhtml/Block/Sales/Order/View/Items.php

    如果您使用的是 1.5 或更高版本.. 请将其添加到下面的文件中。 app/code/core/Mage/Adminhtml/Block/Sales/Order/View/Items.php

    01
        public function getItemcomment($item) {
    02
            $itemId = $item->getId();
    03
    
    04
            $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    05
    
    06
            $query = "SELECT q.* FROM `sales_flat_order_item` o
    07
            LEFT JOIN `sales_flat_quote_item` q on o.quote_item_id = q.item_id
    08
            WHERE o.item_id = $itemId";
    09
    
    10
            # For older versions of Magento
    11
    /*      $query = "SELECT q.* FROM `sales_order_entity_int` o
    12
            LEFT JOIN `sales_flat_quote_item` q on o.value = q.entity_id
    13
            WHERE o.entity_id = $itemId AND o.attribute_id = 343";       */    
    14
    
    15
            $res = $write->query($query);
    16
    
    17
            while ($row = $res->fetch() ) {
    18
                if(key_exists('itemcomment',$row)) {
    19
                    echo nl2br($row['itemcomment']);
    20
                }
    21
            }
    22
        }   
    To add the comments column to the items edit the .phtml file below:
    app/design/adminhtml/default/default/template/sales/order/view/items.phtml
    
    Adding header for items to make it look like below:
    
    1
    .
    2
    .
    3
    <tr class="headings">
    4
        <th><?php echo $this->helper('sales')->__('Product') ?></th>
    5
        <th><?php echo $this->helper('sales')->__('Comments') ?></th>
    6
        <th><?php echo $this->helper('sales')->__('Item Status') ?></th>
    7
    .
    8
    .
    9
    .
    Adding Column with comments. app/design/adminhtml/default/default/template/sales/order/view/items/renderer/default.phtml
    Add a column for item comments juts before status columns to make it look a like below.
    
    view sourceprint?
    1
    .
    2
    .
    3
    <td><?php echo $this->getItemcomment($_item) ?></td> <!-- New column added for item comments -->
    4
    <td class="a-center"><?php echo $_item->getStatus() ?></td>
    5
    .
    6
    .
    

    这样做会在项目表中显示 cmets 列。 这将添加一个名称为 cmets 的文本框(尽快更改名称)。希望这会有所帮助。 注意:只有当商品被添加到购物车并且正如你所说的价格没有改变时,这才会添加一个盒子,我认为这会更合适。

    【讨论】:

    • 是否可以在产品详情页添加相同的内容?好像它只在购物车页面上可见,然后用户首先将商品添加到购物车,然后他们将从购物车页面添加他们的戒指尺寸详细信息,这将是两步过程。
    • magento 中存在一个错误,该错误会阻止内置功能或这样做会导致错误。我已经花了两个多星期的时间研究这个但没有用,相信我。如果你找到解决方案请在这里分享。我曾在 indiaplaza 担任 magento 开发人员。
    【解决方案2】:

    我已按照您的说明在我的单页结帐中添加评论框,但是当我检查它时,它没有在数据库表中添加评论。如果我做错了什么,你能告诉我吗?

    下面是你说要更新的我的 Cart.php 文件中的函数:

    公共函数 updateItems($data) { Mage::dispatchEvent('checkout_cart_update_items_before', array('cart'=>$this, 'info'=>$data));

        /* @var $messageFactory Mage_Core_Model_Message */
        $messageFactory = Mage::getSingleton('core/message');
        $session = $this->getCheckoutSession();
        $qtyRecalculatedFlag = false;
        foreach ($data as $itemId => $itemInfo) {
            $item = $this->getQuote()->getItemById($itemId);
            if (!$item) {
                continue;
            }
    
            if (!empty($itemInfo['remove']) || (isset($itemInfo['qty']) && $itemInfo['qty']=='0')) {
                $this->removeItem($itemId);
                continue;
            }
    
            $qty = isset($itemInfo['qty']) ? (float) $itemInfo['qty'] : false;
            if ($qty > 0) {
                $item->setQty($qty);
    
                $itemInQuote = $this->getQuote()->getItemById($item->getId());
    
                if (!$itemInQuote && $item->getHasError()) {
                    Mage::throwException($item->getMessage());
                }
    
                if (isset($itemInfo['before_suggest_qty']) && ($itemInfo['before_suggest_qty'] != $qty)) {
                    $qtyRecalculatedFlag = true;
                    $message = $messageFactory->notice(Mage::helper('checkout')->__('Quantity was recalculated from %d to %d', $itemInfo['before_suggest_qty'], $qty));
                    $session->addQuoteItemMessage($item->getId(), $message);
                }
            }
    
    
            /* Start: Custom code added for comments */
            if(!empty($itemInfo['comments'])) {
    
                $write = Mage::getSingleton('core/resource')->getConnection('core_write');
    
                # make the frame_queue active
                $query = "UPDATE `mgn_sales_flat_quote_item` SET itemcomment = '".$itemInfo['comments']."' where item_id = " . $itemId . "";
                $write->query($query);
                $item->setItemcomment($itemInfo['comments']);
    
            }
            /* End: Custom code added for comments */
    
    
        }
    
        if ($qtyRecalculatedFlag) {
            $session->addNotice(
                Mage::helper('checkout')->__('Some products quantities were recalculated because of quantity increment mismatch')
            );
        }
    
        Mage::dispatchEvent('checkout_cart_update_items_after', array('cart'=>$this, 'info'=>$data));
        return $this;
    }
    

    【讨论】:

    • 这不应该作为答案发布,而是作为对给定答案的评论,并且回答者可能不需要您重新粘贴所有代码。
    • 下次您应该将其他问题添加为 cmets,而不是发布问题作为答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2023-01-17
    相关资源
    最近更新 更多