|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Plugin Name: Plain Formatted Billing Address for WooCommerce |
| 4 | + * Plugin URI: https://wordpress.org/plugins/plain-formatted-billing-address-for-woocommerce/ |
| 5 | + * Description: Adds a plain-text formatted billing address placeholder to WooCommerce. |
| 6 | + * Version: 1.0.0 |
| 7 | + * Requires at least: 5.0 |
| 8 | + * Requires PHP: 7.2 |
| 9 | + * Author: Maximum.Software |
| 10 | + * Author URI: https://maximum.software/ |
| 11 | + * License: GPL v2 or later |
| 12 | + * License URI: http://www.gnu.org/licenses/gpl-2.0.html |
| 13 | + * Text Domain: plain-formatted-billing-address-for-woocommerce |
| 14 | + * WC requires at least: 3.0.0 |
| 15 | + * WC tested up to: 9.6 |
| 16 | + */ |
| 17 | + |
| 18 | +// Exit if accessed directly |
| 19 | +if( ! defined( 'ABSPATH' ) ) |
| 20 | + exit; |
| 21 | + |
| 22 | +class Plain_Formatted_Billing_Address_for_WooCommerce |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Constructor for setting up the hooks |
| 26 | + */ |
| 27 | + public function __construct() |
| 28 | + { |
| 29 | + // Add hooks after WooCommerce is fully loaded |
| 30 | + add_action( 'woocommerce_init', array( $this, 'setup_hooks' ) ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Set up hooks |
| 35 | + */ |
| 36 | + public function setup_hooks() |
| 37 | + { |
| 38 | + add_filter( 'woocommerce_email_format_string', array( $this, 'process_formatted_billing_address_plain_placeholder' ), 10, 2 ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Build the billing address in plain-text format |
| 43 | + * |
| 44 | + * @param WC_Order $order The WooCommerce order object. |
| 45 | + * @return string A plain-text address with new line separators. |
| 46 | + */ |
| 47 | + private function get_plaintext_billing_address( $order ) |
| 48 | + { |
| 49 | + // Compose the plain text billing address |
| 50 | + $lines = array_filter( array( |
| 51 | + trim( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ), |
| 52 | + $order->get_billing_company(), |
| 53 | + $order->get_billing_address_1(), |
| 54 | + $order->get_billing_address_2(), |
| 55 | + trim( $order->get_billing_city() . ', ' . $order->get_billing_state() . ' ' . $order->get_billing_postcode() ), |
| 56 | + $order->get_billing_country(), |
| 57 | + ) ); |
| 58 | + |
| 59 | + // Join with new line characters |
| 60 | + return implode( "\n", $lines ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Replace our custom placeholder `{formatted_billing_address_plain}` with a plain-text billing address. |
| 65 | + * |
| 66 | + * Hooked into 'woocommerce_email_format_string'. |
| 67 | + * |
| 68 | + * @param string $string The email string (subject/body/heading/etc.). |
| 69 | + * @param mixed $email The email object (or some data) from WooCommerce. |
| 70 | + * @return string |
| 71 | + */ |
| 72 | + public function process_formatted_billing_address_plain_placeholder( $string, $email ) |
| 73 | + { |
| 74 | + // Bail if the placeholder is not found |
| 75 | + if( false === strpos( $string, '{formatted_billing_address_plain}' ) ) |
| 76 | + return $string; |
| 77 | + |
| 78 | + // Safely detect the order object from $email |
| 79 | + $order = ( is_object( $email ) && property_exists( $email, 'object' ) ) ? $email->object : null; |
| 80 | + |
| 81 | + // If an order is found, replace the placeholder |
| 82 | + if( $order && is_a( $order, 'WC_Order' ) ) |
| 83 | + { |
| 84 | + // Build a plain-text billing address |
| 85 | + $address_string_plaintext = $this->get_plaintext_billing_address( $order ); |
| 86 | + |
| 87 | + // Replace our custom placeholder |
| 88 | + $string = str_replace( |
| 89 | + '{formatted_billing_address_plain}', |
| 90 | + $address_string_plaintext, |
| 91 | + $string |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return $string; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +$plain_formatted_billing_address_for_woocommerce = new Plain_Formatted_Billing_Address_for_WooCommerce(); |
0 commit comments