0

I am not a php programmer, and it takes me forever to figure out why. So hopefully SO could help me out. Basically, I installed a woocommerce plugin called interfax-woocommerce, which is used to send order completion email to fax number. On the documentation, it says the plugin uses order completion template to send the fax. It does include everything on the order email template except leaving the order note field out. I believe it must have something to do with the plugin. maybe it passes the order array without including that particular order note field? I can not figure out why and how to fix it. Below is the source code of interfax plugin

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/' );
    }
Adam M
  • 136
  • 6
John Li
  • 43
  • 6
  • you are unlikely to get a answer for this. Unfortunately, the value you want to include on the fax is not present above, you will need to look through the original plugin to find where the value is available and this requires a php programmer. If you google hire php programmers you should be able to find someone to do this for you – David Feb 04 '15 at 23:35
  • David, thanks for your comment. The code above is the actual source code from original plugin. I think add_filter( 'woocommerce_checkout_fields', array( $this, 'add_checkout_field' ) ); does include all the checkout fields by using the filter per woocommerce documentation, however, only the order note field value isnt caught by the fax plugin. – John Li Feb 05 '15 at 04:44
  • `add_filter('woocommerce_checkout_fields', array( $this, 'add_checkout_field' ));` only adds the fax number field to the checkout form. This does not relate to whether the order note appears in the fax. The fax appears to be borrowing the `emails/customer-completed-order.php` template. If you don't see the order note in this email, then you won't see it in the fax either. – helgatheviking Feb 05 '15 at 10:45
  • Hi helgatheviking, Thanks for your comment. You are correct. The weird thing is I see the order note in email but not in the fax. The fax is exactly same as the order completion email except missing that particular order note field.. – John Li Feb 05 '15 at 22:20
  • BTW, the order note field is one of the default woocommerce checkout fields, not something i create. – John Li Feb 05 '15 at 22:25

1 Answers1

1

Here is an example of adding the customer's note (which is the order post's "excerpt") to the bottom of all emails:

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 );
    }
}

The woocommerce_email_after_order_table hook is definitely available in WooCommerce 2.3, but I think it is available in 2.2+ as well.

Alternatively, you could copy the customer-completed-order.php template into your theme and work directly on it.

helgatheviking
  • 25,596
  • 11
  • 95
  • 152