我创建了自定义标签,我想在magento admin的编辑产品页面中单击保存按钮时将数据保存在数据库中。
config.xml 文件
<?xml version="1.0"?>
<config>
<modules>
<ProductCustomizer_ProductCustomizer>
<version>1.0.0</version>
</ProductCustomizer_ProductCustomizer>
</modules>
<global>
<helpers>
<productcustomizer>
<class>ProductCustomizer_ProductCustomizer_Helper</class>
</productcustomizer>
</helpers>
<models>
<productcustomizer>
<class>ProductCustomizer_ProductCustomizer_Model</class>
<resourceModel>productcustomizer_mysql4</resourceModel>
</productcustomizer>
<productcustomizer_mysql4>
<class>ProductCustomizer_ProductCustomizer_Model_Mysql4</class>
<entities>
<cliparts>
<table>productcustomizer_cliparts</table>
</cliparts>
<backgrounds>
<table>productcustomizer_backgrounds</table>
</backgrounds>
<fonts>
<table>productcustomizer_fonts</table>
</fonts>
<product_setings>
<table>productcustomizer_setings</table>
</product_setings>
</entities>
</productcustomizer_mysql4>
</models>
<resources>
<productcustomizer_setup>
<setup>
<module>ProductCustomizer_ProductCustomizer</module>
<class>ProductCustomizer_ProductCustomizer_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</productcustomizer_setup>
<productcustomizer_write>
<connection>
<use>core_write</use>
</connection>
</productcustomizer_write>
<productcustomizer_read>
<connection>
<use>core_read</use>
</connection>
</productcustomizer_read>
</resources>
<blocks>
<productcustomizer>
<class>ProductCustomizer_ProductCustomizer_Block</class>
</productcustomizer>
<adminhtml>
<rewrite>
<catalog_product_edit_tabs>ProductCustomizer_ProductCustomizer_Block_Adminhtml_Tabs</catalog_product_edit_tabs>
</rewrite>
</adminhtml>
</blocks>
</global>
<frontend>
<routers>
<productcustomizer>
<use>standard</use>
<args>
<module>ProductCustomizer_ProductCustomizer</module>
<frontName>productcustomizer</frontName>
</args>
</productcustomizer>
</routers>
<layout>
<updates>
<productcustomizer>
<file>productcustomizer.xml</file>
</productcustomizer>
</updates>
</layout>
</frontend>
<admin>
<routers>
<productcustomizer>
<use>admin</use>
<args>
<module>ProductCustomizer_ProductCustomizer</module>
<frontName>admin_productcustomizer</frontName>
</args>
</productcustomizer>
</routers>
</admin>
<adminhtml>
<menu>
<productcustomizer module="productcustomizer">
<title>Product Customizer</title>
<sort_order>100</sort_order>
<children>
<productcustomizercliparts module="productcustomizer">
<title>Clip arts</title>
<sort_order>0</sort_order>
<action>admin_productcustomizer/adminhtml_productcustomizercliparts</action>
</productcustomizercliparts>
</children>
<!-- <children>
<productcustomizerbackgrounds module="productcustomizer">
<title>Backgrounds</title>
<sort_order>1</sort_order>
<action>admin_productcustomizer/adminhtml_productcustomizerbackgrounds</action>
</productcustomizerbackgrounds>
</children> -->
<children>
<productcustomizerfonts module="productcustomizer">
<title>Fonts</title>
<sort_order>2</sort_order>
<action>admin_productcustomizer/adminhtml_productcustomizerfonts</action>
</productcustomizerfonts>
</children>
</productcustomizer>
</menu>
<acl>
<resources>
<all>
<title>Allow Everything</title>
</all>
<admin>
<children>
<productcustomizer module="productcustomizer" translate="title">
<title>Product Customizer</title>
<sort_order>1000</sort_order>
<children>
<productcustomizerbackend translate="title">
<title>Product Customizer Setting</title>
</productcustomizerbackend>
</children>
</productcustomizer>
</children>
</admin>
</resources>
</acl>
<layout>
<updates>
<productcustomizer>
<file>productcustomizer.xml</file>
</productcustomizer>
</updates>
</layout>
<events>
<catalog_product_save_after>
<observers>
<productcustomizer>
<type>singleton</type>
<class>productcustomizer/observer</class>
<method>saveProductTabData</method>
</productcustomizer>
</observers>
</catalog_product_save_after>
</events>
</adminhtml>
</config>
我创建了“ ProductCustomizer / ProductCustomizer / Model / Observer.php ”
<?php
class ProductCustomizer_ProductCustomize_Model_Observer
{
public function saveProductTabData(Varien_Event_Observer $observer)
{
//$product = $observer->getEvent()->getProduct();
$product = $observer->getProduct();
Mage::log($product->getID());
try {
$product = $this->getProduct();
$productId = $product->getId();
$data = $this->_getRequest()->getPost();
$customize_price = $data['customize_price'];
$customize_data = $data['customize_data'];
$customize_status = $data['customize_status'];
Mage::log($data);
$checkProduct = Mage::getModel('productcustomizer/productsetting')->loadByProductId($productId);
if($checkProduct->getProductId() == ''){
$productModel = Mage::getModel('productcustomizer/productsetting');
$productModel->setProductId($productId);
$productModel->setCustomizePrice($customize_price);
$productModel->setCustomizeData($customize_data);
$productModel->setCustomizeStatus($customize_status);
$productModel->save();
} else {
$checkProduct = Mage::getModel('productcustomizer/productsetting')->loadByProductId($productId);
$checkProduct->setImprintWidth($imprint_width);
$checkProduct->setImprintHeight($imprint_height);
$checkProduct->save();
}
/**
* Uncomment the line below to save the product
*
*/
//$product->save();
}
catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
}
}
/**
* Retrieve the product model
*
* @return Mage_Catalog_Model_Product $product
*/
public function getProduct()
{
return Mage::registry('product');
}
/**
* Shortcut to getRequest
*
*/
protected function _getRequest()
{
return Mage::app()->getRequest();
}
}
我已清除缓存和已启用日志,但没有任何内容登录“system.log”文件。
ProductCustomizer / ProductCustomizer / Model / Productsetting.php file
<?php
class ProductCustomizer_ProductCustomizer_Model_Productsetting extends Mage_Core_Model_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('productcustomizer/productsetting');
}
public function loadByProductId($productid)
{
$data= $this->_getResource()->loadByProductId($productid);
$this->setData($data);
return $this;
}
}
ProductCustomizer / ProductCustomizer /型号/ Mysql4 / Productsetting.php
<?php
class ProductCustomizer_ProductCustomizer_Model_Mysql4_Productsetting extends Mage_Core_Model_Mysql4_Abstract
{
public function _construct()
{
// Note that the magentorex_id refers to the key field in your database table.
$this->_init('productcustomizer/productsetting', 'id');
}
public function loadByProductId($productid)
{
$adapter = $this->_getReadAdapter();
$select = $adapter->select()
->from($this->getTable('productcustomizer/product_setings'), '*')
->where('product_id = :product_id');
$bind = array(':product_id' => (string)$productid);
return $adapter->fetchRow($select, $bind);
}
}
ProductCustomizer / ProductCustomizer /型号/ Mysql4 \ Productsetting / Collection.php
<?php
class ProductCustomizer_ProductCustomizer_Model_Mysql4_Productsetting_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{
public function _construct()
{
parent::_construct();
$this->_init('productcustomizer/productsetting');
}
}
答案 0 :(得分:2)
模型类中的名称是错误的类ProductCustomizer_ProductCustomize_Model_Observer它应该是ProductCustomizer_ProductCustomizer_Model_Observer它应该调用你的观察者。