Answers:
或者,如果您有客户的电子邮件地址,也可以尝试以下操作:
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
if ($subscriber->getId()) {
// put your logic here...
}
或者,如果您有客户ID,则可以直接在newsletter_subscriber
表中检查客户ID是否存在。
is_subscribed
是在哪个表中保存还是在会话中保存
`newsletter_subscriber`.`subscriber_status`
。请注意,表名称可能有前缀,具体取决于您的配置。
您还必须检查订阅状态:
if(Mage::getSingleton('customer/session')->isLoggedIn()){
$email = Mage::getSingleton('customer/session')->getCustomer()->getData('email');
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
if($subscriber->getId())
{
$isSubscribed = $subscriber->getData('subscriber_status') == Mage_Newsletter_Model_Subscriber::STATUS_SUBSCRIBED;
}
}
在我看来,上述答案都不是很方便,因为它们要么不检查客户是否已实际订阅,要么不处理未找到订阅的情况,因此,这里是:
$customerIsSubscribed = false;
$customer = Mage::getSingleton('customer/session')->getCustomer();
if ($customer) {
$customerEmail = $customer->getEmail();
$subscriber = Mage::getModel('newsletter/subscriber')->loadByEmail($customerEmail);
if ($subscriber) {
$customerIsSubscribed = $subscriber->isSubscribed();
}
}
扩展@Mufaddal的答案:
$subscriberModel = Mage::getModel('newsletter/subscriber')->loadByEmail($email);
$subbed = ($subscriberModel->isSubscribed() ? true : false);
这样,您可以检查订阅记录是否存在以及订阅状态是否为true。
$subscriberModel->getId()