php商品低库存报警,Magento中产品库存不报警解决方案

简单说来,这个事件在Magento处理产品报库存警的集合方法里。我们的客户有超过40000封订阅和超过30000个需要将“缺货”状态改为“有货”。所以当Magento尝试从product_alert_stock表读取40000条记录到集合里的时候,由于内存限制而失败了。可能你的product_alert_stock表有超过20000记录时,产品库存报警功能就失效了。

解决方案相对简单。通过以1000为集合创建分页来遍历超过30000条记录。正如Magento在系统中所做的那样。新的功能是通过遍历所有的这些记录,循环出status=0(未处理)的记录,检查产品现在是否有货。当true=> set status=1就发送电子邮件通知客户。

那么,让我们创建我们的模块,Alwayly_ProductAlert,这将提高Magento方法。

1. Create file in app/etc/modules/ Alwayly_ProductAlert.xml with the following content:

true

local

2. Create file in app/code/local/Alwayly/ProductAlert/etc/ config.xml with the following content:

1.0.0.0

Alwayly_ProductAlert_Model_Observer

你可以看到我们重写了Mage_ProductAlert_Model_Observer类

Create file in /www/app/code/local/Alwayly/ProductAlert/Model/ Observer.php with the following content:

/*

* ProductAlert observer

*/

class Alwayly_ProductAlert_Model_Observer extends Mage_ProductAlert_Model_Observer

{

/**

* Process stock emails

*

* @param Mage_ProductAlert_Model_Email $email

* @return Mage_ProductAlert_Model_Observer

*/

protected function _processStock(Mage_ProductAlert_Model_Email $email)

{

$email->setType('stock');

foreach ($this->_getWebsites() as $website) {

/* @var $website Mage_Core_Model_Website */

if (!$website->getDefaultGroup() || !$website->getDefaultGroup()->getDefaultStore()) {

continue;

}

if (!Mage::getStoreConfig(

self::XML_PATH_STOCK_ALLOW,

$website->getDefaultGroup()->getDefaultStore()->getId()

)) {

continue;

}

try {

$wholeCollection = Mage::getModel('productalert/stock')

->getCollection()

// ->addWebsiteFilter($website->getId())

->addFieldToFilter('website_id', $website->getId())

->addFieldToFilter('status', 0)

;

// $wholeCollection->getSelect()->order('alert_stock_id DESC');

/* table: !product_alert_stock!

alert_stock_id: 1

customer_id: 1

product_id: 1

website_id: 1

add_date: 2013-04-26 12:08:30

send_date: 2013-04-26 12:28:16

send_count: 2

status: 1

*/

}

catch (Exception $e) {

Mage::log('error-1-collection $e=' . $e->getMessage(), false, 'product_alert_stock_error.log', true);

$this->_errors[] = $e->getMessage();

return $this;

}

$previousCustomer = null;

$email->setWebsite($website);

try {

$originalCollection = $wholeCollection;

$count = null;

$page = 1;

$lPage = null;

$break = false;

while ($break !== true) {

$collection = clone $originalCollection;

$collection->setPageSize(1000);

$collection->setCurPage($page);

$collection->load();

if (is_null($count)) {

$count = $collection->getSize();

$lPage = $collection->getLastPageNumber();

}

if ($lPage == $page) {

$break = true;

}

Mage::log('page=' . $page, false, 'check_page_count.log', true);

Mage::log('collection=' . (string)$collection->getSelect(), false, 'check_page_count.log', true);

$page ++;

foreach ($collection as $alert) {

try {

if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {

$customer = Mage::getModel('customer/customer')->load($alert->getCustomerId());

if ($previousCustomer) {

$email->send();

}

if (!$customer) {

continue;

}

$previousCustomer = $customer;

$email->clean();

$email->setCustomer($customer);

}

else {

$customer = $previousCustomer;

}

$product = Mage::getModel('catalog/product')

->setStoreId($website->getDefaultStore()->getId())

->load($alert->getProductId());

/* @var $product Mage_Catalog_Model_Product */

if (!$product) {

continue;

}

$product->setCustomerGroupId($customer->getGroupId());

if ($product->isSalable()) {

$email->addStockProduct($product);

$alert->setSendDate(Mage::getModel('core/date')->gmtDate());

$alert->setSendCount($alert->getSendCount() + 1);

$alert->setStatus(1);

$alert->save();

}

}

catch (Exception $e) {

Mage::log('error-2-alert $e=' . $e->getMessage(), false, 'product_alert_stock_error.log', true);

$this->_errors[] = $e->getMessage();

}

}

}

Mage::log("\n\n", false, 'check_page_count.log', true);

} catch (Exception $e) {

Mage::log('error-3-steps $e=' . $e->getMessage(), false, 'product_alert_stock_error.log', true);

}

if ($previousCustomer) {

try {

$email->send();

}

catch (Exception $e) {

$this->_errors[] = $e->getMessage();

}

}

}

return $this;

}

/**

* Run process send product alerts

*/

public function process()

{

Mage::log('ProductAlert started @' . now(), false, 'product_alert_workflow.log', true);

$email = Mage::getModel('productalert/email');

/* @var $email Mage_ProductAlert_Model_Email */

$this->_processPrice($email);

$this->_processStock($email);

$this->_sendErrorEmail();

Mage::log('ProductAlert finished @' . now(), false, 'product_alert_workflow.log', true);

return $this;

}

}

你可以看到,我们重写了process()和 _processStock()方法。在process()方法中,我们添加了Mage::log()创建起止时间到var/log/product_alert_workflow.log。这样我们就能知道脚本是否执行完成。同样的,我们增强_processStock()方法的功能,我们增加了一些Mage:log()调用来跟踪,看所有的行为是否按期待的方式表现。

最后,如果你不想等待你的Cron被触发,你可以在Magento根目录下创建一个文件,让我们调用它。

Alwayly_cron.php

require_once 'app/Mage.php';

Mage::app();

try {

Mage::getModel('productalert/observer')-&gtprocess();

} catch (Exception $e) {

Mage::log('error-0-start $e=' . $e-&gtgetMessage() . ' @' . now(), false, 'product_alert_stock_error.log', true);

}

就是这些了,这个解决思路同样适用于产品价格报警。

(责任编辑:最模板)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP语言开发的ERP进销存管理系统多仓库全开源无限制优化版,扫描入库+库存预警+仓库管理+商品管理+供应商管理。有购货,销货,仓库管理,商品管理,供应商管理,职员管理等非常多的功能。 本系统开发 PHP + MySQL 采用CI2.x框架 运行环境 php5.6 、MYSQL5.6 、Linux、apache、nginx 该ERP系统部署安装非常简单,只需要空间支持PHP上传到根目录即可简单安装即可。运行速度和数据处理效率都非常高,非常适合小企业仓库管理使用。 更新日志: 2019年12月03日: 1.修复销货单修改状态下选择商品后无法带出单价的问题 2.将所有单据,点击行新增按钮后由从行前添加改为从行位添加空白行,防止选择多个商品时将后面的已选商品覆盖 3.商品增加生产日期、有效期天数、货位字段,并可带入销货单支持EXCEL导入 4.修复已知小bug 2019年07月04日: 1.修复销货单偶尔因网络延迟获取不到联系人的bug 2.增加销售合同、采购合同附件对word,excel,pdf文件上传的支持 3.导入商品资料增加备注等字段的导入 4.修复一部分小bug 2018年12月30日: 1.新增管理员强制重置密码功能 2018年08月10日: 1.新增客户等级价格功能(在 客户订单、销售单生效) 2018年4月30日: 1.修复调拨单无法删除 2.修复销售单修改时无法 自动带出 联系人、地址、电话 问题 3.销货单 打印增加 左上角显示Logo、右上角显示说明文字、右边多联说明 4.销货单支持A5纸多页连打 5.购货单、销货单、收款单、付款单 增加选择打印行数 6.增加收款单打印功能 7.增加付款单打印功能 2017年12月25日: 1.新增购货单附件上传,销售单附件上传,此附件不仅用于图片,可上传word,excel等各种文件。 2.新增重磅级功能->销售利润表,可以按客户和日期范围查询销货单的利润明细和统计,并且支持根据销售人员查询该员工的销售业绩,见下图。 3.增加收款单、付款单在收付款时,做指定销货单或购货单的收付款,进行了关联。 4.增加供应商、客户、商品模块的excel导入功能。 5.商品增加套餐(组合商品)功能,销售套餐,对应的子商品立减库存。 6.增加客户联系人“职位”字段标注。 7.销货单、退货单增加“序列号”字段的录入。 8.修复后台商品管理不稳定问题,造成任何用户多次操作才能生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值