0

我不是 php 程序员,我花了很长时间才弄清楚为什么。所以希望SO可以帮助我。基本上,我安装了一个名为 interfax-woocommerce 的 woocommerce 插件,用于将订单完成电子邮件发送到传真号码。在文档中,它说插件使用订单完成模板来发送传真。它确实包括订单电子邮件模板上的所有内容,除了留下订单备注字段。我相信它必须与插件有关。也许它通过了订单数组而不包括那个特定的订单备注字段?我不知道为什么以及如何解决它。下面是interfax插件的源代码

if ( ! defined( 'ABSPATH' ) ) exit;

class WooCommerce_InterFax_Integration {
    private $dir;
    private $file;
    private $assets_dir;
    private $assets_url;
    private $settings;
    private $interfax_class;
    private $username;
    private $password;
    private $store_fax_number;
    private $template_html;
    private $heading;
    private $send_store_fax;
    private $order;
    private $fax_number;

    public function __construct( $file ) {
        $this->dir = dirname( $file );
        $this->file = $file;
        $this->assets_dir = trailingslashit( $this->dir ) . 'assets';
        $this->assets_url = esc_url( trailingslashit( plugins_url( '/assets/', $file ) ) );

        // Add settings link to plugin page
        add_filter( 'plugin_action_links_' . plugin_basename( $this->file ), array( $this, 'add_settings_link' ) );

        // Get integration settings
        $this->settings = get_option( 'woocommerce_interfax_settings' );

        // Only handle actions if integration is enabled
        if( $this->settings['wcif_enable'] == 'yes' ) {
            $this->interfax_class = 'http://ws.interfax.net/dfs.asmx?wsdl';
            $this->username = $this->settings['wcif_username'];
            $this->password = $this->settings['wcif_password'];
            $this->store_fax_number = $this->settings['wcif_fax_number'];
            $this->template_html = 'emails/customer-completed-order.php';
            $this->heading = __( 'Your order is complete', 'woocommerce' );
            $this->send_store_fax = true;

            // Add fax number field to checkout
            add_filter( 'woocommerce_checkout_fields', array( $this, 'add_checkout_field' ) );

            // Send fax on selected order status (deault to 'completed' if no status is selected)
            if( $this->settings['wcif_fax_status'] ) {
                $fax_status = $this->settings['wcif_fax_status'];
            } else {
                $fax_status = 'completed';
            }
            add_action( 'woocommerce_order_status_' . $fax_status, array( $this, 'trigger' ) );

            // Add order action to dashboard
            add_filter( 'woocommerce_order_actions', array( $this, 'add_order_action' ) );

            // Handle order actions
            add_action( 'woocommerce_order_action_send_customer_fax', array( $this, 'process_order_action_customer' ) );
            add_action( 'woocommerce_order_action_send_store_fax', array( $this, 'process_order_action_store' ) );

            // Add fax number field to user profile page in WP dashboard
            add_filter( 'woocommerce_customer_meta_fields', array( $this, 'add_user_meta_field' ) );

            // Add fax number field to edit billing address page
            add_filter( 'woocommerce_billing_fields', array( $this, 'add_address_field' ), 10, 2 );
        }

        // Handle localisation
        $this->load_plugin_textdomain();
        add_action( 'init', array( $this, 'load_localisation' ), 0 );

    }

    public function trigger( $order_id ) {
        if ( $order_id ) {
            $this->order = new WC_Order( $order_id );

            // Send fax to store owner
            if( $this->store_fax_number && strlen( $this->store_fax_number ) > 0 ) {
                $this->send_store_fax();
            }

            // Send fax to customer
            $this->fax_number = get_post_meta( $order_id, '_billing_fax', true );
            if( $this->fax_number && strlen( $this->fax_number ) > 0 ) {
                $this->send_customer_fax();
            }
        }
    }

    private function send_customer_fax() {
        $interfax_client = new SoapClient( $this->interfax_class );

        $params->Username  = $this->username;
        $params->Password  = $this->password;
        $params->FaxNumber = $this->fax_number;
        $params->Data      = $this->get_fax_content();
        $params->FileType  = 'HTML';

        $result = $interfax_client->SendCharFax( $params );

        if( isset( $result->SendCharFaxResult ) && $result->SendCharFaxResult > 0 ) {
            return true;
        }

        return false;
    }

    private function send_store_fax() {
        $interfax_client = new SoapClient( $this->interfax_class );

        $params->Username  = $this->username;
        $params->Password  = $this->password;
        $params->FaxNumber = $this->store_fax_number;
        $params->Data      = $this->get_fax_content();
        $params->FileType  = 'HTML';

        $result = $interfax_client->SendCharFax( $params );

        if( isset( $result->SendCharFaxResult ) && $result->SendCharFaxResult > 0 ) {
            return true;
        }

        return false;
    }

    public function get_fax_content() {
        global $woocommerce;

        ob_start();

        $template_data = array(
            'order'         => $this->order,
            'email_heading' => $this->heading
        );

        if( version_compare( $woocommerce->version, '2.1-beta-1', ">=" ) ) {
            wc_get_template( $this->template_html, $template_data );
        } else {
            woocommerce_get_template( $this->template_html, $template_data );
        }

        return ob_get_clean();
    }

    public function add_checkout_field( $fields ) {

        $fields['billing']['billing_fax'] = array(
            'label'             => __( 'Fax Number (including country code)', 'wc_interfax' ),
            'placeholder'       => _x( 'e.g. +27861234567', 'placeholder', 'wc_interfax' ),
            'required'          => false,
            'class'             => array( 'form-row' ),
            'clear'             => false
        );

        return $fields;

    }

    public function add_order_action( $actions ) {
        $actions['send_customer_fax'] = 'Send customer fax';
        $actions['send_store_fax']    = 'Send store fax';
        return $actions;
    }

    public function process_order_action_customer( $order ) {
        $this->fax_number = get_post_meta( $order->id, '_billing_fax', true );
        if( $this->fax_number && strlen( $this->fax_number ) > 0 ) {
            $this->order = $order;
            $this->send_customer_fax();
        }
    }

    public function process_order_action_store( $order ) {
        if( $this->store_fax_number && strlen( $this->store_fax_number ) > 0 ) {
            $this->order = $order;
            $this->send_store_fax();
        }
    }

    public function add_user_meta_field( $fields ) {

        $fields['billing']['fields']['billing_fax'] = array(
            'label' => __( 'Fax number', 'wc_interfax' ),
            'description' => ''
        );

        return $fields;

    }

    public function add_address_field( $fields, $country ) {

        $fields['billing_fax'] = array(
            'label'         => __( 'Fax Number (including country code)', 'wc_interfax' ),
            'placeholder'   => _x( 'e.g. +27861234567', 'placeholder', 'wc_interfax' ),
            'required'      => false,
            'class'         => array( 'form-row' ),
            'clear'         => true
        );

        return $fields;
    }

    public function add_settings_link( $links ) {
        $settings_link = '<a href="admin.php?page=woocommerce&tab=integration&section=interfax">' . __( 'Configure', 'wc_interfax' ) . '</a>';
        array_unshift( $links, $settings_link );
        return $links;
    }

    public function load_localisation () {
        load_plugin_textdomain( 'wc_interfax' , false , dirname( plugin_basename( $this->file ) ) . '/lang/' );
    }

    public function load_plugin_textdomain () {
        $domain = 'wc_interfax';
        $locale = apply_filters( 'plugin_locale' , get_locale() , $domain );

        load_textdomain( $domain , WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
        load_plugin_textdomain( $domain , FALSE , dirname( plugin_basename( $this->file ) ) . '/lang/' );
    }
4

1 回答 1

1

下面是在所有电子邮件底部添加客户备注(即订单帖子的“摘录”)的示例:

add_action( 'woocommerce_email_after_order_table' , 'so_28333042_add_customer_note' );
function so_28333042_add_customer_note( $order ){
    $post = get_post( $order->id );
    if( $post->post_excerpt != '' ){
        echo "<strong>Customer Note:</strong><br/>" . wp_kses_post( $post->post_excerpt );
    }
}

woocommerce_email_after_order_table钩子在 WooCommerce 2.3 中绝对可用,但我认为它在 2.2+ 中也可用。

或者,您可以将模板复制customer-completed-order.php到您的主题中并直接使用它。

于 2015-02-05T11:02:27.620 回答