Magento PHP在表中创建了一个新列,尝试使用列来过滤和执行条件语句

时间:2017-10-17 17:59:27

标签: php mysql magento

我在" sales_flat_order "中创建了一个新列。表格为" gc_sent ",默认值为 0

从这里开始,我尝试在代码中执行下面的if语句,说明卡的gc_sent是否为0,然后向其中的礼品卡发送电子邮件给客户。然后将card_status设置为1,将gc_sent设置为1.

但是,我在开发环境中遇到的情况是,我收到电子邮件后每分钟收到1封电子邮件,其中包含我的礼品卡信息。那应该不会发生。这就是我想要阻止的。这就是我做条件的原因,包括curDate(当前日期)和MailDeliveryDate。为了确保一切都能生效。

那么,我做错了什么?我需要改变什么?顺便说一句,我对Magento来说相对较新,现在只做了几个月。

最相关的部分是:

    $curDate = date('Y-m-d');
    $cards = Mage::getModel('giftcards/giftcards')->getCollection()
        ->addFieldToFilter('order_id', $order->getId())
        ->addFieldToFilter('gc_sent', 0);
    foreach($cards as $card) {
        if (($card->getGcSent() == 0) && ($curDate == $card->getMailDeliveryDate())) {
            if ((($card->getMailDeliveryDate() == null) || ($curDate == $card->getMailDeliveryDate())) && $card->getCardType() != 'offline') {
                $this->_send($post, 'giftcards/email/email_template', $mail, $storeId);
                $card->setCardStatus(1)->save();
                $order->setGcSent(1)->save();
            }
        }
    }

但这是完整的代码:

<?php
class Sportys_Giftcardoverride_Model_Giftcards extends Webtex_Giftcards_Model_Giftcards
{
    protected function _sendEmailCard($storeId = 0)
    {
        if($order = Mage::getModel('sales/order')->load($this->getOrderId())){
            $storeId = $order->getStoreId();
        } else {
            $storeId = 1;
        }

        $amount = number_format(Mage::app()->getStore($storeId)->convertPrice($this->getCardAmount(), false, false),2);

        if(Mage::helper('giftcards')->isUseDefaultPicture() || !$this->getProductId()) {
            $picture = Mage::getDesign()->getSkinUrl('images/giftcard.png',array('_area'=>'frontend'));
        } else {
            $product = Mage::getModel('catalog/product')->load($this->getProductId());
            if (!$product->getId() || $product->getImage() != 'no_selection') {
                $picture = Mage::helper('catalog/image')->init($product, 'image');
            } else {
                $picture = Mage::getDesign()->getSkinUrl('images/giftcard.png',array('_area'=>'frontend'));
            }
        }

        //Change picture if one is found in picture array
        $cardDesigns = __DIR__ . '/../../../../../../sportysadmin/giftcarddesigns.php';
        if(file_exists($cardDesigns)){
            include $cardDesigns;
        }

        $post = array(
            'amount'        => $this->_addCurrencySymbol($amount,$this->getCardCurrency()),
            'code'          => $this->getCardCode(),
            'email-to'      => $this->getMailTo(),
            'email-from'    => $this->getMailFrom(),
            'recipient'     => $this->getMailToEmail(),
            'email-message' => nl2br($this->getMailMessage()),
            'store-phone'   => Mage::getStoreConfig('general/store_information/phone'),
            'picture'       => $picture,
        );

        $mail = trim($this->getMailToEmail()) ;

        if(empty($mail)) {
            $mail = $order->getCustomerEmail() ;
        }

        $curDate = date('Y-m-d');
        $cards = Mage::getModel('giftcards/giftcards')->getCollection()
            ->addFieldToFilter('order_id', $order->getId())
            ->addFieldToFilter('gc_sent', 0);
        foreach($cards as $card) {
            if (($card->getGcSent() == 0) && ($curDate == $card->getMailDeliveryDate())) {
                if ((($card->getMailDeliveryDate() == null) || ($curDate == $card->getMailDeliveryDate())) && $card->getCardType() != 'offline') {
                    $this->_send($post, 'giftcards/email/email_template', $mail, $storeId);
                    $card->setCardStatus(1)->save();
                    $order->setGcSent(1)->save();
                }
            }
        }
    }

    protected function _send($post, $template, $email, $storeId)
    {
        if ($email) {
            $translate = Mage::getSingleton('core/translate');
            $translate->setTranslateInline(false);
            $postObject = new Varien_Object();
            $postObject->setData($post);
            $postObject->setStoreId($storeId);
            $mailTemplate = Mage::getModel('core/email_template');
            $pdfGenerator = new Webtex_Giftcards_Model_Email_Pdf();
            //$this->_addAttachment($mailTemplate, $pdfGenerator->getPdf($postObject), 'giftcard.pdf');
            $mailTemplate->setDesignConfig(array('area' => 'frontend', 'store' => $storeId))
                ->sendTransactional(
                    Mage::getStoreConfig($template, $storeId),
                    'general',
                    $email,
                    null,
                    array('data' => $postObject)
                );
            $translate->setTranslateInline(true);
        } else {
            throw new Exception('Invalid recipient email address.');
        }
    }
}
?>

1 个答案:

答案 0 :(得分:1)

问题在于您的以下代码

if ((($card->getMailDeliveryDate() == null) || ($curDate == $card->getMailDeliveryDate())) && $card->getCardType() != 'offline') {
  $this->_send($post, 'giftcards/email/email_template', $mail, $storeId);
  $card->setCardStatus(1)->save();
  $order->setGcSent(1)->save();
}

您在$ order而不是setGcSent(1)上设置$card尝试将该代码更改为

 $card->setCardStatus(1)
      ->setGcSent(1)
      ->save();