php - 根据产品属性添加自定义 woocommerce 电子邮件

标签 php wordpress woocommerce attributes email-notifications

我尝试调整在 https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/ 上找到的自定义电子邮件插件

我的意图是,如果客户选择具有特定属性的产品(它是可变产品),则在客户下新订单时会发送一封自定义电子邮件(必须发送待处理或处理中) .

我的属性 slug 是“csr-dates”。自定义插件由两个文件组成(参见上面的链接):woocommerce-csr-order-email.php 和(存储在“includes”文件夹中)class-wc-csr-order-email.php

我猜问题出在类文件中,我在这里报告:

<?php

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * A custom Order WooCommerce Email class
 *
 * @since 0.1
 * @extends \WC_Email
 */
class WC_CSR_Order_Email extends WC_Email {


    /**
     * Set email defaults
     *
     * @since 0.1
     */
    public function __construct() {

        // set ID, this simply needs to be a unique name
        $this->id = 'customer_csr_order';
        $this->customer_email   = true;
        // this is the title in WooCommerce Email settings
        $this->title = 'CSR Cruise Order';

        // this is the description in WooCommerce email settings
        $this->description = 'CSR Cruise Order Notification emails are sent when a customer places an order for a CSR cruise';

        // these are the default heading and subject lines that can be overridden using the settings
        $this->heading = 'CSR Cruise Order';
        $this->subject = 'CSR Cruise Order';

        // these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar
        $this->template_html  = 'emails/customer-processing-order-csr.php'; //qui posso duplicare il template e farne uno ad hoc per questo tipo di mail con i file attached
        $this->template_plain = 'emails/plain/customer-processing-order.php';

        // Trigger on new paid orders
        add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ) );
        add_action( 'woocommerce_order_status_failed_to_processing_notification',  array( $this, 'trigger' ) );
        add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ) );
        add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'trigger' ) );
        // Call parent constructor to load any other defaults not explicity defined here
        parent::__construct();


        // if none was entered, just use the WP admin email as a fallback
        if ( ! $this->recipient )
            $this->recipient = get_option( 'admin_email' );
    }


    /**
     * Determine if the email should actually be sent and setup email merge variables
     *
     * @since 0.1
     * @param int $order_id
     */
    public function trigger( $order_id ) {

        // bail if no order ID is present
        if ( ! $order_id )
            return;

          $order = new WC_Order( $order_id );

          //step 1) find first the product_id
          $items = $order->get_items();
          foreach ( $items as $item ) {
              $product_id = $item['product_id'];
          }
                //set 2) from the product_id get the product attribute
                $product = new WC_Product( $product_id );  // create an object of WC_Product class

                $patt = $product->get_attribute();  // call get_attribute method

            //step 3) condition valid to send the email (if the attributes is csr-dates)
            if ( $patt == 'pa_csr-dates'  ) 
            {
                //send the email
                // setup order object
                $this->object = new WC_Order( $order_id );
                $this->recipient    = $this->object->billing_email;

                // replace variables in the subject/headings
                $this->find[] = '{order_date}';
                $this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) );

                $this->find[] = '{order_number}';
                $this->replace[] = $this->object->get_order_number();

                if ( ! $this->is_enabled() || ! $this->get_recipient() )
                    return;

                // woohoo, send the email!
                $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );

            }

            else
            {
                return; //do nothing if is not csr-dates attribute
            }
    }


    /**
     * get_content_html function.
     *
     * @since 0.1
     * @return string
     */
    public function get_content_html() {
        ob_start();
        woocommerce_get_template( $this->template_html, array(
            'order'         => $this->object,
            'email_heading' => $this->get_heading(),
            'sent_to_admin' => false,
            'plain_text'    => false,
            'email'         => $this
        ) );
        return ob_get_clean();
    }


    /**
     * get_content_plain function.
     *
     * @since 0.1
     * @return string
     */
    public function get_content_plain() {
        ob_start();
        woocommerce_get_template( $this->template_plain, array(
            'order'         => $this->object,
            'email_heading' => $this->get_heading(),
            'sent_to_admin' => false,
            'plain_text'    => true,
            'email'         => $this
        ) );
        return ob_get_clean();
    }


    /**
     * Initialize Settings Form Fields
     *
     * @since 2.0
     */
    public function init_form_fields() {

        $this->form_fields = array(
            'enabled'    => array(
                'title'   => 'Enable/Disable',
                'type'    => 'checkbox',
                'label'   => 'Enable this email notification',
                'default' => 'yes'
            ),
            'subject'    => array(
                'title'       => 'Subject',
                'type'        => 'text',
                'description' => sprintf( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject ),
                'placeholder' => '',
                'default'     => ''
            ),
            'heading'    => array(
                'title'       => 'Email Heading',
                'type'        => 'text',
                'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.' ), $this->heading ),
                'placeholder' => '',
                'default'     => ''
            ),
            'email_type' => array(
                'title'       => 'Email type',
                'type'        => 'select',
                'description' => 'Choose which format of email to send.',
                'default'     => 'html',
                'class'       => 'email_type',
                'options'     => array(
                    'plain'     => __( 'Plain text', 'woocommerce' ),
                    'html'      => __( 'HTML', 'woocommerce' ),
                    'multipart' => __( 'Multipart', 'woocommerce' ),
                )
            )
        );
    }


} // end \WC_CSR_Order_Email class

我想我为获取属性(第 1-2 步)和/或发送电子邮件的条件(第 3 步)而编写的代码有问题。

有人可以帮我解决这个问题吗?谢谢

最佳答案

您应该尝试以这种方式使用 get_attributes() 而不是使用 get_attribute() 函数:

//set 2) from the product_id get the product attribute
$product = new WC_Product( $product_id );  // create an object of WC_Product class

$patt = $product->get_attributes();  // call get_attributes method

//step 3) condition valid to send the email (if the attributes is csr-dates)
if ( array_key_exists('pa_csr-dates', $patt) ) 
{

试一试,这应该可以……

关于php - 根据产品属性添加自定义 woocommerce 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40633541/

相关文章:

javascript - 为什么我的提交按钮不起作用?

PHP - 优化数千种模式的 preg_match

javascript - 脚本在任何基于 WebKit 的浏览器中都可以正常工作,但在 Firefox 中却不行

php - WooCommerce:从订阅订单中获取名称和变体

php - 如果产品变体已添加到购物车,请移除该 block

php - 从其他类访问登录的 User 对象的最佳实践

php - 如何保护 imap_open 连接的安全

php - 未呈现模板

javascript - jquery:循环相似的div

css - 延迟覆盖在鼠标移出时消失