简体   繁体   English

**更新**如何在表sales_flat_quote_item中创建新列并使用$ cart-> addProduct添加数据

[英]**update** How i can create new column in table sales_flat_quote_item and add data with $cart->addProduct

How i can set data in new column in sales_flat_quote_item with $cart->addProduct code 如何使用$ cart-> addProduct代码在sales_flat_quote_item的新列中设置数据

I try to insert in different way but result is same product is add to cart but my data not insert into database 我尝试以不同的方式插入,但结果是将同一产品添加到购物车,但我的数据未插入数据库

i cant understand what wrong with me or i just do anything before in app\\code\\core\\Mage\\Checkout\\sql\\checkout_setup ? 我无法理解我的问题所在,还是在app \\ code \\ core \\ Mage \\ Checkout \\ sql \\ checkout_setup之前做任何事情?

It's my exem code PS: I change on $cart->addProduct line 这是我的exem代码PS:我在$ cart-> addProduct行上进行更改

1st 1

$cart = Mage::getSingleton('checkout/cart'); 
            $cart->init();
            $cart->addProduct($product, array('qty' => $qty), array('ref_order_id' => $ref_order_id));
            Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
            $cart->save();

2nd. 第2位。

$param = array(
                'product' => $product->getId(),
                'qty' => $qty,
                'ref_order_id' => $ref_order_id
            ); 
            $request = new Varien_Object();
            $request->setData($param);
            $cart->addProduct($product, $param); 

3rd 第3

$cart->addProduct($product, array('qty' => $qty,'original_custom_price' => $ref_order_id ));

UPDATE i try to insert this code in addProductAdvanced class in 更新我尝试将此代码插入到addProductAdvanced类中

app/code/core/Mage/Sales/Model/Quote.php: addProduct(Mage_Catalog_Model_Product $product, $request=null) 应用/代码/核心/法师/销售/模型/Quote.php:addProduct(Mage_Catalog_Model_Product $ product,$ request = null)

foreach ($cartCandidates as $candidate) {
            $item = $this->_addCatalogProduct($candidate, $candidate->getCartQty());
            ...
            ...
            ...
            $item->setref_order_id('SpecialOrderFromCustomPage');
            ...
            ...
}

but data is insert for all item in quote how can i check if this come from my page? 但是数据已插入报价中所有项目,我如何检查这是否来自我的页面?

Adding a new attribute to sales_flat_order_item table can be achieved through the XML config file and creating an install script. 可以通过XML配置文件并创建安装脚本来向sales_flat_order_item表添加新属性。 There is a particular method for doing this which can be seen in any of the core upgrade and install scripts of core modules. 有一种特殊的方法可以在任何核心模块的核心升级和安装脚本中看到。

You have to add the column though to many tables, not just the order item. 您必须将列添加到许多表中,而不仅仅是订单项。 When a customer is adding items to their basket they are building up a quote. 当客户将商品添加到他们的购物篮中时,他们正在建立报价。 You will need the need column on the sales_flat_quote_item table first so when the customer adds the item to the basket the data gets stored against the quote. 首先,您需要在sales_flat_quote_item表上的需求列。因此,当客户将商品添加到购物篮中时,数据将根据报价进行存储。

When the customer places the order, the quote gets converted to an order, so you need all the item data from the quote carried over to the order item as well. 当客户下订单时,报价将转换为订单,因此您还需要从报价结转到订单物料中的所有物料数据。 This is where XML in your module config can be used to achieve your desired result. 在这里,可以使用模块配置中的XML来获得所需的结果。 You will also need an observer to put the data against the quote item when the customer clicks add to basket. 当客户单击添加到购物篮时,您还需要一个观察员将数据放在报价项目中。

Consider the following example; 考虑下面的例子;

You have a product which has a deposit custom attribute set against it. 您有一个为其设置了存款自定义属性的产品。

You then want this value stored against the quote item and the order item when a customer wants or buys this item. 然后,当客户需要或购买此项目时,您希望将该值与报价项目和订单项目相对应存储。 So you create an install script similar to the following in your custom module; 因此,您可以在自定义模块中创建类似于以下内容的安装脚本;

<?php

$installer = $this;

$installer->installEntities();

$setup = new Mage_Sales_Model_Mysql4_Setup('core_setup');
$setup->startSetup();

$setup->addAttribute(
    'order_item', 
    'base_deposit_price', 
    array(
        'type' => Varien_Db_Ddl_Table::TYPE_DECIMAL,'default' => 0,'visible' => true 
    )
);

$setup->addAttribute(
    'quote_item', 
    'base_deposit_price', 
    array(
        'type' => Varien_Db_Ddl_Table::TYPE_DECIMAL,'default' => 0,'visible' => true 
    )
);

$setup->addAttribute(
    'invoice_item', 
    'base_deposit_price', 
    array(
        'type' => Varien_Db_Ddl_Table::TYPE_DECIMAL,'default' => 0,'visible' => true, 
    )
);

$setup->addAttribute(
    'creditmemo_item', 
    'base_deposit_price', 
    array(
        'type' => Varien_Db_Ddl_Table::TYPE_DECIMAL,'default' => 0,'visible' => true            
    )
);

$setup->endSetup();

What this does is adds the columns to your tables nicely as part of your module install script. 这样做是将列很好地添加到表中,作为模块安装脚本的一部分。

Then in your modules config.xml file you need something like the following XML defining; 然后,在模块config.xml文件中,您需要类似以下XML定义的内容:

<?xml version="1.0"?>
<config>
    <global>
         <sales>
                <quote>
                    <item>
                        <product_attributes>
                            <base_deposit_price />
                        </product_attributes>
                    </item>
                </quote>
                <order>
                    <item>
                        <product_attributes>
                            <base_deposit_price />
                        </product_attributes>
                    </item>
                </order>
            </sales>
            <fieldsets>
                <sales_convert_quote_item>
                    <base_deposit_price>
                        <to_order_item>*</to_order_item>
                    </base_deposit_price>
                </sales_convert_quote_item>
                <sales_convert_order_item>
                    <base_deposit_price>
                        <to_cm_item>*</to_cm_item>
                        <to_invoice_item>*</to_invoice_item>
                    </base_deposit_price>
                </sales_convert_order_item>
            </fieldsets>
            <events>
              <sales_quote_item_set_product>
                <observers>
                    <quoteitem_set_deposit_data>
                        <type>singleton</type>
                        <class>YourNameSpace_YourModule_Model_Observer</class>
                        <method>setDepositOnQuoteItem</method>
                    </quoteitem_set_deposit_data>
                </observers>
              </sales_quote_item_set_product>
            </events>
    </global>
</config>

The config file has some definitions which basically allow magento to automatically copy the values over from the quote_item table to the order_item table. 配置文件具有一些定义,这些定义基本上允许magento自动将值从quote_item表复制到order_item表。 The example above also copies the values to credit memos items and invoice items from the order as well. 上面的示例还将值复制到订单的贷项通知单项目和发票项目中。

The magic bit is the observer which sets the data on the quote item in the first place when the customer adds the item to their basket. 魔术师是观察者,当客户将报价项目添加到他们的购物篮中时,它首先将报价项目上的数据设置好。

<?php

class YourNameSpace_YpurModule_Model_Observer {

    /**
     * Flag to stop observer executing more than once
     *
     * @var static bool
     */
    static protected $_singletonFlag = false;

    /*
     * Gets Deposit Price Values For Quote Product Items. These Will Later Be
     * Be Converted To Order Items, Invoice Items, & Credit Memo Items.
     * 
     */

    public function setDepositOnQuoteItem($oObserver) {
        $oProduct = $oObserver->getProduct();
        $oQuoteItem = $oObserver->getQuoteItem();
        $deposit = $oProduct->getData('deposit_price', null);

        if ($deposit > 0) {
            $oQuoteItem->setData('base_deposit_price', $deposit);
        }
    }
}

You could add your product attribute through the admin Product Attributes system, or you could code your module to also setup the custom product attribute you want to put on the quote item and order item as well by using an EAV setup script; 您可以通过admin产品属性系统添加产品属性,也可以对模块进行编码,以使用EAV设置脚本来设置要放在报价项和订单项上的自定义产品属性;

class YourNameSpace_YourModule_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{

  public function getDefaultEntities()
  {
        return array(
          'catalog_product' => array(
              'entity_model'      => 'catalog/product',
              'attribute_model'   => 'catalog/resource_eav_attribute',
              'table'             => 'catalog/product',
              'additional_attribute_table' => 'catalog/eav_attribute',
              'entity_attribute_collection' => 'catalog/product_attribute_collection',
              'attributes'        => array(
                  'deposit_price' => array(
                      'group'             => 'Prices',
                      'label'             => 'Deposit Price',
                      'type'              => 'decimal',
                      'input'             => 'price',
                      'default'           => '0',
                      'class'             => 'validate-number',
                      'frontend'          => '',
                      'source'            => '',
                      'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
                      'visible'           => true,
                      'required'          => false,
                      'user_defined'      => false,
                      'searchable'        => false,
                      'filterable'        => false,
                      'comparable'        => true,
                      'visible_on_front'  => true,
                      'visible_in_advanced_search' => false,
                      'unique'            => false,
                      'backend'           => 'catalog/product_attribute_backend_price',
                  ),
                )
              )
            );
    }         
}

Hopefully that should help you achieve what you want to do in the correct way. 希望这可以帮助您以正确的方式完成您想做的事情。 Just build it all as a custom module. 只需将其构建为自定义模块即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento - 向sales_flat_quote_item和sales_flat_order_item添加新列 - Magento - Adding a new column to sales_flat_quote_item and sales_flat_order_item 未完成Magento订单创建sales_flat_quote_item - Magento Order creation sales_flat_quote_item not being made Magento V1.9 在事件观察器中的 sales_flat_quote_item 更新后超时运行 - Magento V1.9 is running in timeout after sales_flat_quote_item update in event observer 为什么core_write在Magento CE 1.9.1中的sales_flat_quote_item上不起作用 - Why core_write is not working on sales_flat_quote_item in Magento CE 1.9.1 如何将产品特定的属性列添加到sales_flat_order_item表? - How to add product specific attribute column to sales_flat_order_item table? 如何将每个产品作为新商品添加到购物车 - How can I add every product as a new item into cart Magento-更新sales_flat_order_item表中发票的数量 - Magento - update the qty invoiced in sales_flat_order_item table 找不到表“ sales_flat_quote_address”-复制现有的Magento网站以在本地运行时 - Can't find the table “sales_flat_quote_address”- While Copying an existing Magento site to run locally 在sales_flat_quote和sales_flat_order_item表中添加额外信息 - Adding extra information in sales_flat_quote and sales_flat_order_item tables 从magento中的自定义模块在sales_flat_order_item表中添加新字段 - Adding new field in sales_flat_order_item table from custom module in magento
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM