使用自定义模块将SKU添加到Magento中的sales_flat_quote

时间:2013-01-22 07:44:11

标签: magento moodle

为了将Magento与Moodle(LMS)集成,我需要用户数据和在单个表中购买的商品的SKU。最接近的表是sales_flat_quote。如果我可以只购买购买的SKU,那么Moodle可以在登录时检查该表并将购买的课程添加到用户配置文件中。

使用Ivan Chepurnyi's tutorial,此related post和此similar thread我设法创建了一个用于写入测试服务器上的日志的模块。现在它不再做任何事了。

我在\ app \ etc \ modules \ minerva_moodleConnect.xml中有这个:

<?xml version="1.0" encoding="utf-8"?>
<config>
  <modules>
    <minerva_moodleConnect>
      <active>true</active>
      <codePool>local</codePool>
    </minerva_moodleConnect>
  </modules>
</config>

我在\ app \ code \ local \ minerva \ moodleConnect \ etc \ config.xml中有这个:

<?xml version="1.0" encoding="utf-8"?>
<config>
  <modules>
    <minerva_moodleConnect>
      <version>0.0.1</version>
    </minerva_moodleConnect>
  </modules>
  <global>
    <models>
      <minerva_moodleConnect>
        <class>minerva_moodleConnect_Model</class>
      </minerva_moodleConnect>
    </models>
    <resources>
      <minerva_moodleConnect>
        <setup>
          <module>minerva_moodleConnect</module>
          <class>Mage_Sales_Model_Mysql4_Setup</class>
        </setup>
      </minerva_moodleConnect>
    </resources>
    <events>
      <sales_order_place_after>
        <observers>
          <minerva_moodleConnect>
            <type>singleton</type>
            <class>minerva_moodleConnect_Model_observer</class>
            <method>registerCourse</method>
          </minerva_moodleConnect>
        </observers>
      </sales_order_place_after>
    </events>
  </global>
</config>

我在\ app \ code \ local \ minerva \ moodleConnect \ model \ observer.php中有这个:

<?php
class minerva_moodleConnect_Model_Observer
{
    public function __construct()
    {

    }
    public function registerCourse(Varien_Event_Observer $observer)
    {


        // Retrieve the product being updated from the event observer


        Mage::log("register course called");
        /* @var $resource Mage_Sales_Model_Mysql4_Order */

        /*$resource = $observer->getEvent()->getResource();
        $resource->addVirtualGridColumn(
            'sku',
            'sales/flat_quote',
            array('sku' => 'sku'),
            'sku'
        );*/

    }
}

和\ app \ code \ local \ minerva \ moodleConnect \ sql \ minerva_moodleConnect_setup \ mysql4-install-0.1.0.php:

<?php
/**
 * Setup scripts, add new column and fulfills
 * its values to existing rows
 *
 */
/* @var $this Mage_Sales_Model_Mysql4_Setup */
$this->startSetup();
// Add column to grid table
$this->getConnection()->addColumn(
    $this->getTable('sales/flat_quote'),
    'sku',
    "varchar(255) not null default ''"
);
// Add key to table for this field,
// it will improve the speed of searching & sorting by the field
$this->getConnection()->addKey(
    $this->getTable('sales/flat_quote'),
    'sku',
    'sku'
);
// Now you need to fullfill existing rows with data from address table
$select = $this->getConnection()->select();
$select->join(
    array('sku'=>$this->getTable('sales/flat_quote_item')),
    $this->getConnection()->quoteInto(
        'sku.sku = flat_quote_item.sku',
        Mage_Sales_Model_Quote_Address::TYPE_BILLING
    ),
    array('sku' => 'sku')
);
$this->getConnection()->query(
    $select->crossUpdateFromSelect(
        array('sku' => $this->getTable('sales/flat_quote'))
    )
);
$this->endSetup();

到目前为止,我在很大程度上猜测与我的案例有关的变化。我的想法是我想要复制SKU并调用新行SKU。其他一切感觉都很稳固。

那里有一个善良的灵魂可以拍我的手腕并让我回到正轨吗?

1 个答案:

答案 0 :(得分:0)

我正在使用类似的东西在订单和报价表中添加一些字段:

<?php
/* @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();

Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$installer = new Mage_Sales_Model_Mysql4_Setup;

$attribute  = array(
    'type'          => 'text',
    'backend_type'  => 'text',
    'frontend_input' => 'text',
    'is_user_defined' => true,
    'label'         => 'External Reference',
    'visible'       => true,
    'required'      => false,
    'user_defined'  => false,
    'searchable'    => false,
    'filterable'    => false,
    'comparable'    => false,
    'default'       => ''
);

$installer->addAttribute('order', 'ext_reference', $attribute);
$installer->addAttribute('quote', 'ext_reference', $attribute);

$attribute  = array(
    'type'          => 'text',
    'backend_type'  => 'text',
    'frontend_input' => 'text',
    'is_user_defined' => true,
    'label'         => 'License Plate',
    'visible'       => true,
    'required'      => false,
    'user_defined'  => false,
    'searchable'    => false,
    'filterable'    => false,
    'comparable'    => false,
    'default'       => ''
);

$installer->addAttribute('order', 'licenseplate', $attribute);
$installer->addAttribute('quote', 'licenseplate', $attribute);

$installer->endSetup();