テーマのファイル: 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>
次のステップは、顧客がカートを更新したときにコメントを DB に保存することです。
そこで、テーブル「sales_flat_quote_item」に新しいフィールド「itemcomment」を追加します。(Magento の古いバージョンの場合、テーブルは「sales_quote_item」になります)
次に、DB 操作を行うコードを追加します。このためには、次のファイルを変更する必要があります: 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
.
ここまでやるとアイテムテーブルにコメント欄が表示されます。これにより、名前がコメントのテキスト ボックスが追加されます (できるだけ早く名前を変更してください)。これが役に立てば幸いです。注:これは、アイテムがカートに追加された場合にのみボックスを追加し、価格は変わらないとおっしゃっていましたが、これの方が適切だと思います。