YayMail - WooCommerce Email Customizer
  • Getting Started
    • Introduction
    • How to Install
    • Enter license key
    • System Requirements
    • How To Update YayMail
    • Uninstall and Delete
  • How It Works
    • Email Templates
    • Global Header and Footer
    • Settings
    • Addon
    • Preview Email
  • YayMail Main Customization Interface
    • The Elements Panel
      • Basic Elements
        • Embed a YayMail shortcode into a button
        • Add HTML and shortcode
        • Duplicate/Copy an Element
      • General Elements
      • WooCommerce Elements
        • Change Order Item Titles
      • Blocks
      • Drag and Drop Interface
      • Block-Based Live Editor
    • The Patterns Panel
      • Header
      • Footer
      • Banner
      • Pre-Designed Patterns
    • The Settings Panel
      • Global Settings
        • Customize with CSS
        • Hide Elements of Order Table
      • Email Settings
    • View History
  • Upper Toolbar
    • Shortcodes
    • Custom shortcode
    • Reset Templates
    • Import/copy template from
  • Visibility Control
  • Guides
    • Display coupons
    • Add column in order item table
  • Developer zone
    • Demo Plugin
    • Register Templates
      • Register new template
      • Email class
      • Render Templates content
    • Register Elements
      • Register New Elements
      • Element Class
      • Render Element content
      • Default element data
      • Custom Property Editor (coming soon)
    • Register Shortcodes
      • Register New Shortcodes
      • Shortcode Class
  • Integrations
    • Advanced Custom Fields (ACF) by WP Engine
    • Advanced Local Pickup for WooCommerce by Zorem
    • Back In Stock Notifier by codewoogeek
    • Checkout Field Editor by ThemeHigh
    • Checkout Field Editor by WooCommerce
    • Custom Order Status by Nuggethon
    • Custom Order Status by TycheSoftwares
    • Custom Order Status Manager for WooCommerce by Bright Plugins
    • Flexible Checkout Fields by WPDesk
    • FooEvents for WooCommerce
    • Shipment Tracking
    • Software Addon by WooCommerce
    • WooCommerce Admin Custom Order Fields by SkyVerge
    • WooCommerce Order Status Manager by SkyVerge
    • WooCommerce Show Attributes by Isabel Castillo
    • WPML & WooCommerce Multilingual
    • Weglot Translate
    • Loco Translate
    • TranslatePress/Polylang
    • Translate text in YayMail
    • User meta
  • Addons
    • Conditional Logic
    • WooCommerce Subscriptions
    • Germanized for WooCommerce
    • Dokan Multi Vendor
    • AutomateWoo
    • WooCommerce Follow Ups
    • WooCommerce Bookings
    • WooCommerce Gift Cards
    • Smart Coupons
    • WooCommerce Memberships and Teams for WooCommerce Memberships
    • WooCommerce Pre-Orders
    • Parcel Panel Order Tracking
    • WCFM Marketplace
    • Affiliate For WooCommerce
    • Back In Stock Notifications
    • PW WooCommerce Gift Cards
    • Appointments for WooCommerce
    • WooCommerce Bookings And Appointments by PluginHive
    • B2BKing
    • B2B Market
    • B2B & Wholesale Suite
    • TrackShip for WooCommerce
    • SG WooCommerce Order Approval
    • SUMO Subscriptions
    • SUMO Payment Plans
    • WooCommerce German Market
    • WC Vendors Marketplace
    • WooCommerce Print Invoices & Packing Lists
    • WooCommerce Deposits
    • WooCommerce Cart Abandonment Recovery by CartFlows
    • WooCommerce Waitlist
    • WooCommerce Simple Auctions
    • YITH Auctions for WooCommerce
    • YITH WooCommerce Stripe
    • YITH Membership
    • YITH Subscription
    • YITH Booking and Appointment
    • YITH WooCommerce Recover Abandoned Cart
    • YITH Wishlist
    • YITH Pre-Order
    • YITH Gift Cards
    • YITH Review For Discounts
    • YITH Multi Vendor
    • YITH WooCommerce Points and Rewards
    • YITH WooCommerce Request a Quote
    • YITH WooCommerce Affiliates
    • YITH WooCommerce Coupon Email System
    • YITH Advanced Refund System for WooCommerce
    • YITH Easy Login & Register Popup
    • YITH WooCommerce Delivery Date
    • WooCommerce Order Delivery Date Pro
    • Colissimo Shipping Methods for WooCommerce
    • WooCommerce Deposits & Partial Payments
    • Custom User Registration Fields by Addify
    • Quotes for WooCommerce
    • RMA Return Refund and Exchange For WooCommerce
    • License Manager for WooCommerce
    • WooCommerce Product Vendors
    • MultiVendorX
    • WooCommerce Stripe Payment Gateway
    • WooCommerce B2B
    • Order Cancellation Email To Customer
    • WooCommerce Order Delivery
    • Account Funds
    • TeraWallet for WooCommerce
    • Points And Rewards for WooCommerce
    • WooCommerce Split Orders
    • WP Crowdfunding
    • WooCommerce PDF Product Vouchers
    • MultiLocation Inventory & Order Routing
    • Woo Sell Services
    • WooCommerce Return & Warranty Management
    • WholesaleX
    • ShopMagic
    • WooCommerce Contact for Shipping Quote
    • MarketKing
    • AliDropship for WooCommerce
    • Custom Fields by Addify
  • Why Upgrade?
  • OTHER LINKS
    • FAQs
    • Troubleshooting
      • Continuous loading
      • Unable to save template
      • Unable to save Emoji
      • Unable to send test email
      • How to clear cache
      • How to use YayMail with GoDaddy
    • Changelog
    • Useful Links
      • AppSumo Deal
    • Support
      • How to pay without PayPal
      • How to renew the license key
      • How to change payment method
  • Articles
    • Design Tips
      • Customize Order Confirmation Email
      • Preview and Test Your Emails
      • Design a Black Friday Banner
      • Embed a Video in WooCommerce Email
      • Add Conditional Logic to WooCommerce Emails
    • Email Marketing
      • Setup Next Order Coupon in WooCommerce Emails
      • Automatically Send “Customer Invoice/Order Details" to Customers
      • Create Emails with Dynamic Content
      • Customize WooCommerce Membership Emails
      • Recover Abandoned Cart
    • Advanced Guides
      • Create WooCommerce Custom Emails Per Product
      • Customize ShopMagic Automation Emails
      • Create Custom Shortcode for YayMail
      • Translate Emails with Loco Translate
Powered by GitBook
On this page

Was this helpful?

  1. Developer zone
  2. Register Shortcodes

Shortcode Class

PreviousRegister New ShortcodesNextIntegrations

Last updated 21 days ago

Was this helpful?

Creating a Custom Shortcode Class

To create a custom shortcode in WooCommerce with YayMail, you’ll need to use the BaseShortcode class provided by YayMail as a starting point. Here’s a clear example to help you set up your own custom shortcode class

  1. Extending BaseShortcode:

    class YourNewShortcodes extends \YayMail\Abstracts\BaseShortcode {
    
        private static $instance;
        
        public static function instance() {
    	if ( null === static::$instance ) {
    		static::$instance = new static();
    	}
    	return static::$instance;
        }
    
        protected function __construct() {
            /**
            * This refers to the array of template IDs where shortcodes can be applied.
            * We can assign it in 1 of 4 ways
            * 1. Special Array Templates ID: Examples include new_order, customer_processing_order.
            * 2. YAYMAIL_WITH_ORDER_EMAILS: Shortcodes applicable only for templates containing order information.
            * 3. YAYMAIL_NON_ORDER_EMAILS: Shortcodes applicable only for templates without order information.
            * 4. YAYMAIL_ALL_EMAILS: Shortcodes applicable to all email templates.
            */
            $this->available_email_ids = [ YAYMAIL_ALL_EMAILS ];
            parent::__construct();
        }
        
        public function get_shortcodes() {
         // Define your function here
        }
    }

    Begin by defining a new class that extends the BaseShortcode class provided by YayMail. Make sure to include essential variables and methods needed for the custom shortcode to work properly.

  2. Function: get_shortcodes

    public function get_shortcodes() {    
        $shortcodes = [];
    
        $shortcodes[] = [
            'name'        => 'new_shortcode_1',
            'description' => 'Custom shortcode 1',
            'group'       => 'Custom Shortcode',
            'callback'    => [ $this, 'new_shortcode_1_render_function' ],
        ];
    
        $shortcodes[] = [
            'name'        => 'yaymail_new_shortcode_2',
            'description' => 'Custom shortcode 2',
            'attributes'  => [
                'text_link' => 'here',
            ],
            'group'       => 'Custom Shortcode',
            'callback'    => [ $this, 'new_shortcode_2_render_function' ],
        ];
    
        return $shortcodes;
    }

    The get_shortcodes function creates a list of shortcodes. Each shortcode follows this structure:

    When setting up a shortcode, include these key fields:

    • Name: A unique identifier for the shortcode.

    • Description: A short explanation of what the shortcode does.

    • Group: A category to organize related shortcodes together.

    • Callback: The function that generates the shortcode’s output.

    • Attributes (optional): Custom properties, like text_link or color, to enhance the shortcode’s functionality.

  3. Callback function for a shortcode

    public function new_shortcode_1_render_function( $data ) {
        $render_data           = isset( $data['render_data'] ) ? $data['render_data'] : [];
        $is_placeholder        = isset( $data['is_placeholder'] ) ? $data['is_placeholder'] : false;
        $is_sample             = isset( $render_data['is_sample'] ) ? $render_data['is_sample'] : false;
        $is_customized_preview = isset( $render_data['is_customized_preview'] ) ? $render_data['is_customized_preview'] : false;
    
        $content            = 'Shortcode content sample';
    
        return $content;
    }
    
    public function new_shortcode_2_render_function( $data, $shortcode_atts = [] ) {
        $template = ! empty( $data['template'] ) ? $data['template'] : null;
        $is_placeholder = isset( $data['is_placeholder'] ) ? $data['is_placeholder'] : false;
        $text_link = isset( $shortcode_atts['text_link'] ) ? $shortcode_atts['text_link'] : '';
    
        if ( empty( $template ) ) {
            $text_link_color = YAYMAIL_COLOR_WC_DEFAULT;
        } else {
            $text_link_color = $template->get_text_link_color();
        }
        return '<a style="color: ' . esc_attr( $text_link_color ) . ';" href="' . esc_url( get_home_url() ) . '"> ' . $text_link . ' </a>';
    }

    This function fetches content for displaying shortcodes in the YayMail Editor and in actual emails. It uses two parameters: data and shortcode_atts (for shortcodes with attributes, if applicable).

    Variables:

    • data: Contains key properties like render_data, template, and is_placeholder.

    • shortcode_atts: An array of attributes defined for the shortcode in the get_shortcodes function.

    • render_data: Holds important details, such as order, is_sample, and is_customized_preview in the YayMail Editor. It also includes data from the get_content_html function when an actual email is sent.

    • is_placeholder: (Boolean) Indicates whether the value should be treated as a placeholder. Returns true if it is a placeholder, false otherwise.

    • is_sample: (Boolean) Returns true when a sample order is selected in the YayMail Editor, false otherwise.

    • is_customized_preview: (Boolean) Returns true when sending a test email or previewing an email in the YayMail Editor, false otherwise.

We’ve followed the steps provided, and the file now correctly includes the use YayMail\Abstracts\BaseShortcode statement.

class YourNewShortcodes extends \YayMail\Abstracts\BaseShortcode {

    private static $instance;
    
    public static function instance() {
	if ( null === static::$instance ) {
		static::$instance = new static();
	}
	return static::$instance;
    }

    protected function __construct() {
        /**
        * This refers to the array of template IDs where shortcodes can be applied.
        * We can assign it in 1 of 4 ways
        * 1. Special Array Templates ID: Examples include new_order, customer_processing_order.
        * 2. YAYMAIL_WITH_ORDER_EMAILS: Shortcodes applicable only for templates containing order information.
        * 3. YAYMAIL_NON_ORDER_EMAILS: Shortcodes applicable only for templates without order information.
        * 4. YAYMAIL_ALL_EMAILS: Shortcodes applicable to all email templates.
        */
        $this->available_email_ids = [ YAYMAIL_ALL_EMAILS ];
        parent::__construct();
    }
    
    public function get_shortcodes() {
        $shortcodes = [];
    
        $shortcodes[] = [
            'name'        => 'new_shortcode_1',
            'description' => 'Custom shortcode 1',
            'group'       => 'Custom Shortcode',
            'callback'    => [ $this, 'new_shortcode_1_render_function' ],
        ];
    
        $shortcodes[] = [
            'name'        => 'new_shortcode_2',
            'description' => 'Custom shortcode 2',
            'attributes'  => [
                'text_link' => 'here',
            ],
            'group'       => 'Custom Shortcode',
            'callback'    => [ $this, 'new_shortcode_1_render_function' ],
        ];
    
        return $shortcodes;
    }
    
    public function new_shortcode_1_render_function( $data ) {
        $render_data           = isset( $data['render_data'] ) ? $data['render_data'] : [];
        $is_placeholder        = isset( $data['is_placeholder'] ) ? $data['is_placeholder'] : false;
        $is_sample             = isset( $render_data['is_sample'] ) ? $render_data['is_sample'] : false;
        $is_customized_preview = isset( $render_data['is_customized_preview'] ) ? $render_data['is_customized_preview'] : false;
    
        $content            = 'Shortcode content sample';
    
        return $content;
    }

    public function new_shortcode_2_render_function( $data, $shortcode_atts = [] ) {
        $template = ! empty( $data['template'] ) ? $data['template'] : null;
        $is_placeholder = isset( $data['is_placeholder'] ) ? $data['is_placeholder'] : false;
        $text_link = isset( $shortcode_atts['text_link'] ) ? $shortcode_atts['text_link'] : '';
    
        if ( empty( $template ) ) {
            $text_link_color = YAYMAIL_COLOR_WC_DEFAULT;
        } else {
            $text_link_color = $template->get_text_link_color();
        }
        return '<a style="color: ' . esc_attr( $text_link_color ) . ';" href="' . esc_url( get_home_url() ) . '"> ' . $text_link . ' </a>';
    }
}
Email render data
Email render data