You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过编程获取Magento补货提醒客户详情并配置短信API

嘿,咱们一步步来解决Magento补货提醒短信通知的问题,我之前处理过类似场景,下面是具体操作:

1. 正确重写Mage_ProductAlert_Model_Observer

直接修改core目录的代码是不推荐的,咱们通过自定义模块来重写这个类,避免升级Magento时丢失修改:

步骤1:创建模块配置文件

app/code/local/Namespace/ProductAlertSms/etc/config.xml中添加以下内容(把Namespace改成你自己的命名空间,比如MyCompany):

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_ProductAlertSms>
            <version>1.0.0</version>
        </Namespace_ProductAlertSms>
    </modules>
    <global>
        <models>
            <productalert>
                <rewrite>
                    <observer>Namespace_ProductAlertSms_Model_Observer</observer>
                </rewrite>
            </productalert>
        </models>
        <!-- 数据库安装脚本配置(可选,用于添加短信发送标记字段) -->
        <resources>
            <productalertsms_setup>
                <setup>
                    <module>Namespace_ProductAlertSms</module>
                    <class>Mage_Core_Model_Resource_Setup</class>
                </setup>
            </productalertsms_setup>
        </resources>
    </global>
    <!-- 后台系统配置(可选,用于管理短信API参数) -->
    <adminhtml>
        <system>
            <config>
                <sections>
                    <catalog>
                        <groups>
                            <productalert>
                                <fields>
                                    <sms_api_key translate="label">
                                        <label>SMS API密钥</label>
                                        <frontend_type>text</frontend_type>
                                        <sort_order>100</sort_order>
                                        <show_in_default>1</show_in_default>
                                        <show_in_website>1</show_in_website>
                                        <show_in_store>1</show_in_store>
                                    </sms_api_key>
                                    <sms_api_url translate="label">
                                        <label>SMS API接口地址</label>
                                        <frontend_type>text</frontend_type>
                                        <sort_order>101</sort_order>
                                        <show_in_default>1</show_in_default>
                                        <show_in_website>1</show_in_website>
                                        <show_in_store>1</show_in_store>
                                    </sms_api_url>
                                </fields>
                            </productalert>
                        </groups>
                    </catalog>
                </sections>
            </config>
        </system>
    </adminhtml>
</config>

步骤2:启用模块

app/etc/modules/Namespace_ProductAlertSms.xml中添加:

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_ProductAlertSms>
            <active>true</active>
            <codePool>local</codePool>
        </Namespace_ProductAlertSms>
    </modules>
</config>
2. 获取补货提醒的客户与产品详情

重写_processStock方法(这个方法专门处理库存补货提醒),在原有邮件逻辑之外,获取客户和产品信息:

创建app/code/local/Namespace/ProductAlertSms/Model/Observer.php

class Namespace_ProductAlertSms_Model_Observer extends Mage_ProductAlert_Model_Observer
{
    protected function _processStock(Mage_ProductAlert_Model_Email $email)
    {
        // 先执行父类的邮件发送逻辑,保留原有功能
        parent::_processStock($email);
        
        // 获取已经标记为"已通知邮件"的库存提醒记录
        $alertCollection = Mage::getModel('productalert/stock')
            ->getCollection()
            ->addFieldToFilter('status', Mage_ProductAlert_Model_Stock::STATUS_NOTIFIED)
            ->addCustomerData(); // 关联客户表数据
        
        foreach ($alertCollection as $alert) {
            // 1. 获取客户详情
            $customer = $alert->getCustomer();
            $customerPhone = $customer->getTelephone(); // 客户电话(需客户资料中已填写)
            $customerName = $customer->getName();
            
            // 如果客户没有电话,跳过发送并记录日志
            if (empty($customerPhone)) {
                Mage::log("Skipped SMS: Customer {$customer->getId()} has no phone number", null, 'product_alert_sms.log');
                continue;
            }
            
            // 2. 获取产品详情
            $product = $alert->getProduct();
            $productName = $product->getName();
            $productUrl = $product->getProductUrl();
            $productSku = $product->getSku();
            
            // 发送短信
            $this->_sendStockAlertSms($customerPhone, $customerName, $productName, $productUrl);
            
            // 标记该提醒已发送短信(需先添加sms_notified字段,见下文)
            $alert->setSmsNotified(1)->save();
        }
        
        return $this;
    }
    
    /**
     * 对接短信API发送提醒
     */
    protected function _sendStockAlertSms($phone, $customerName, $productName, $productUrl)
    {
        // 从后台配置获取API参数
        $apiKey = Mage::getStoreConfig('catalog/productalert/sms_api_key');
        $apiUrl = Mage::getStoreConfig('catalog/productalert/sms_api_url');
        
        // 组装短信内容(注意符合运营商规范,可添加退订提示)
        $smsContent = "您好{$customerName},您关注的商品【{$productName}】已经补货啦,点击查看:{$productUrl} 回复TD可退订";
        
        // 调用短信API(示例用curl,根据你的API文档调整)
        $postData = [
            'phone' => $phone,
            'content' => $smsContent,
            'api_key' => $apiKey
        ];
        
        $ch = curl_init($apiUrl);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 生产环境建议开启SSL验证
        
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        // 记录发送日志,方便排查问题
        Mage::log(
            sprintf("SMS sent to %s | HTTP Code: %d | Response: %s", $phone, $httpCode, $response),
            null,
            'product_alert_sms.log'
        );
    }
}

可选:添加短信发送标记字段

为了避免重复发送短信,咱们给productalert_stock表加一个sms_notified字段:

创建app/code/local/Namespace/ProductAlertSms/sql/productalertsms_setup/install-1.0.0.php

<?php
$installer = $this;
$installer->startSetup();

// 添加sms_notified字段,标记是否已发送短信
$installer->getConnection()->addColumn(
    $installer->getTable('productalert/stock'),
    'sms_notified',
    'TINYINT(1) DEFAULT 0 NOT NULL COMMENT "是否已发送短信提醒"'
);

$installer->endSetup();
3. 测试与排查
  1. 模拟场景:设置一个产品为缺货,用客户账号订阅补货提醒,然后将产品库存改为有货。
  2. 触发cron:Magento的补货提醒是通过cron任务执行的,运行php cron.php触发任务。
  3. 查看日志:检查var/log/product_alert_sms.logvar/log/system.log,排查API调用是否成功。
  4. 注意事项
    • 确保短信API支持你的发送频率,避免被限流。
    • 短信内容要符合运营商规范,避免敏感词。
    • 如果客户没有填写电话,要跳过发送并记录日志。

内容的提问来源于stack exchange,提问作者Mesk

火山引擎 最新活动