1

i try to develope a Theme for displaying the Data of my own Plugin.

Now I have the following scenario:

  • I'm getting the PostId over get_the_ID(); (e.g. PostType = 'Product')
  • then I'm getting Data from the Database. e.g. Productprices, ProductTaxes, Discounts, Campaigns etc.
  • Now I want to calculate the actual Price for the Product

And now I want to display all the Data I was getting from the database and all calculated data.

At this time, I have all the code I need for getting the data above in my single-product.php-File. But I know that this is not a good solution. Because when I create another theme and want to change the style my product data are displayed, the calculate-Methods stays the same.

So my question is, how should I calculate my data and passing the values to my single-product.php-File, so that the only task of the single-product.php is to display these data?

3
  • You can have a look at this article, it can provide a solution on how to load template from plugn, but you can also do that using a shortcode. Dec 10, 2015 at 8:53
  • Thanks for your answer! I had already used a template_loader. But In the comments of your article I found my answer :-) I'm passing my variables I want to use in the template file to an array, and bevore I include the founded template file I call the extract method. So I can use the variables in the template without to do the calculate methods in it :-) Dec 10, 2015 at 10:14
  • 1
    Nice you got an answer. Better answer your own question with some example code, so that someone with similar issue can get help. Dec 10, 2015 at 11:12

1 Answer 1

0

I found my answer. See this article (postet by Mayeenul Islam in the comments of my question Post) to get an understanding of template_loader.

If you understand then, how you locate a template, you can write one small methode to pass parameters to this template.

I wrote the following method:

    public static function get_my_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
        if ( $args && is_array( $args ) ) {
            extract( $args );
        }

        $located = self::locate_template( $template_name, $template_path, $default_path );

        include( $located );
}

as you can see, in this method you are able to pass $args as parameter. The function "extract", extracts the array so that every key is his own variable. (See the manual for more information)

But this was not my idea! Someone in the comments of the article write this suggestion. And when you look at the code of woocommerce you will see that they doing the same way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.