0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Welcartの会員 と MailPoet3の購読者リストを連携する

Posted at

MailPoetには標準でWooCommerceと連携する機能がありますが、
Welcartはなかったので、作ってみました。
最初は$wpdbで直接テーブルにinsertしてましたが、
ウェルカムメールが送信されないなどの問題があったので、プラグインのAPI経由で登録します。
https://github.com/mailpoet/mailpoet/tree/master/doc

$mailPoetIntegrator = mailPoetIntegrator::getInstance();

class mailPoetIntegrator
{
  private static $instance = [];

  private $mailpoetApi;
  const SEGMENT_PURCHASE = 【wp_mailpoet_segmentsのid】; //購入者用のメルマガリスト
  const SEGMENT_REGISTER = 【wp_mailpoet_segmentsのid】; //会員登録者のメルマガリスト

  private function __construct()
  {
    if ( ! class_exists(\MailPoet\API\API::class)) return;

    $this->mailpoetApi = \MailPoet\API\API::MP('v1');

    add_action('usces_action_after_send_ordermail_to_manager', array(&$this, 'completionPurchase'), 10, 4);
    add_action('usces_action_member_registered', array(&$this, 'registeredMember'), 10, 2);
  }

  public static function getInstance()
  {
    $class = get_called_class();
    if (!isset(self::$instance[$class]))
      self::$instance[$class] = new $class;

    return self::$instance[$class];
  }
  public final function __clone()
  {
    throw new \Exception('Clone is not allowed against' . get_class($this));
  }

  private function insertMember( $email, $first_name, $last_name, $segment_id )
  {
    try {
      $this->mailpoetApi->addSubscriber(
        array(
          'email' => $email,
          'first_name' => $first_name,
          'last_name' => $last_name
        ),
        array(
          $segment_id,
        ),
        array(
          'send_confirmation_email' => true,
          'schedule_welcome_email' => true,
          'skip_subscriber_notification' => true
        )
      );
    } catch (\Exception $e) {
      // ログ出しとかエラーメッセージを出したければここで
      return;
    }
  }

  public function completionPurchase( $res, $bcc_para, $entry, $data )
  {
  
    if( ! $email = @$entry['customer']['mailaddress1'] ){
      if( ! $email = @$entry['delivery']['mailaddress1'] ){
        $email = @$data['order_email'];
      }
    }
    if( ! $email ) return;
  
    if( ! $first_name = @$entry['customer']['name2'] ){
      if( ! $first_name = @$entry['delivery']['name2'] ){
        if( ! $first_name = @$data['order_name2'] ){
          $first_name = '';
        }
      }
    }
  
    if( ! $last_name = @$entry['customer']['name1'] ){
      if( ! $last_name = @$entry['delivery']['name1'] ){
        if( ! $last_name = @$data['order_name1'] ){
          $last_name = '';
        }
      }
    }

    $segment_id = self::SEGMENT_PURCHASE;

    $this->insertMember( $email, $first_name, $last_name, $segment_id );
  
  }

  public function registeredMember( $post_member, $user )
  {
  
    if( ! $email = @$post_member['mailaddress1'] ) return;
  
    if( ! $first_name = @$post_member['name2'] ){
      $first_name = '';
    }
  
    if( ! $last_name = @$post_member['name1'] ){
      $last_name = '';
    }

    $segment_id = self::SEGMENT_REGISTER;

    $this->insertMember( $email, $first_name, $last_name, $segment_id );

  }

}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?