• Resolved magnificentjake

    (@magnificentjake)


    Hi folks,

    I’m in the middle of developing a small plugin for my site to integrate my WooCommerce store with a third-party. This integration is meant to pull a code from that third-party and then email it to the customer when they buy certain products.

    After following some tutorials, I’ve managed to get a new transactional email set up, and I can get WooCommerce to send it on an appropriate hook. The trouble is, I was wanting to include the email template inside my plugin itself, and for the life of me I can’t get it to work. I validated that everything worked by putting the tempalte in the woocommerce/templates/emails folder where it sent fine, and I’ve also got it working by putting it in a woocommerce/emails folder within my theme. When I just direct it to use the template in the plugin directory, nothing happens.

    I *think* this is an issue with the email’s ‘template_base’ field. I’ve set it to ‘WP_PLUGIN_DIR . ‘/my-email-plugin/templates/”, with the template_html as ’emails/code-email.php’, but it seems to still be looking in the native woocommerce directory for the template, as I can sub in other vanilla WC emails in the ‘template_html’ field and it sends them instead.

    The custom email is registered in the WooCommerce Settings-> Email page, and when I click on it, I’m able to view the template in the plugin directory fine. It just seems to freak out when it tries to send it.

    Any help would be most welcome!

    • This topic was modified 4 years, 4 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello, could you share your snippet with us?

    What are you setting in $this->template_base ?

    Thread Starter magnificentjake

    (@magnificentjake)

    public function __construct() {
    		
    		// Unique ID for custom email
    		$this->id = 'foundry_code_email';
    
    		// Is a customer email
    		$this->customer_email = true;
    
    		// Title field in WooCommerce Email settings
    		$this->title = __( 'Foundry Premium Content Email', 'woocommerce' );
    
    		// Description field in WooCommerce email settings
    		$this->description = __( 'Foundry email is sent when customer purchases Foundry Premium Content', 'woocommerce' );
    
    		
    				$this->template_base	= WP_PLUGIN_DIR . '/foundry-premium-content/templates/';
    		$this->template_html = 'emails/foundry-code-email.php';
    		$this->template_plain = 'emails/plain/foundry-code-email.php';
    		// $this->template_html = 'customer-reset-password.php';
    
    		$this->placeholders = array(
    			// '{site_title}'	=> $this->get_blogname(),
    			'{order_date}'	=> '',
    			'{order_number}' => '',
    		);
    
    		// Trigger email when payment is complete
    		add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ), 10, 2 );
    		
    
    		// Call parent constructor to load any other defaults not explicitly defined here.
    		parent::__construct();
    	}

    That’s my __construct() function for my custom email class. The plugin itself is called ‘foundry-premium-content’, and the email template in question’s path is ‘foundry-premium-content/templates/emails/foundry-code-email.php’

    And you’re passing the template_basein wc_get_template_html ?

    Thread Starter magnificentjake

    (@magnificentjake)

    public function get_content_html() {
    		return wc_get_template_html(
    				$this->template_html,
    				array(
    					'order'              => $this->object,
    					'email_heading'      => $this->get_heading(),
    					'additional_content' => $this->get_additional_content(),
    					'sent_to_admin'      => false,
    					'plain_text'         => false,
    					'email'              => $this,
    				)
    			);
    	}
    
    	public function get_content_plain() {
    		return wc_get_template_html(
    				$this->template_plain,
    				array(
    					'order'              => $this->object,
    					'email_heading'      => $this->get_heading(),
    					'additional_content' => $this->get_additional_content(),
    					'sent_to_admin'      => false,
    					'plain_text'         => true,
    					'email'              => $this,
    				)
    			);
    	

    These are my get_content_html() and get_content_plain() functions, based on the existing WC email examples. Do I need to add template_base in here somewhere?

    You’re not passing base in function. that’s the problem, follow this article https://www.ibenic.com/create-custom-woocommerce-email/

    Thread Starter magnificentjake

    (@magnificentjake)

    That’s it working now, thanks so much for your help – I suspected it’d be something simple like that!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Custom Email not Getting Template’ is closed to new replies.