最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

themes - How to properly use a hook to create template for custom product type in a plugin such as Woocommerce?

programmeradmin3浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I created my own product type for Woocommerce. But now I am looking for the solution for a frontend template.

I see in de folder templates/single-product/add-to-cart templates for Simple, grouped variable etc. How can I define a path for my own product type frontend template?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I created my own product type for Woocommerce. But now I am looking for the solution for a frontend template.

I see in de folder templates/single-product/add-to-cart templates for Simple, grouped variable etc. How can I define a path for my own product type frontend template?

Share Improve this question edited Apr 20, 2020 at 13:00 西門 正 Code Guy - JingCodeGuy 1,5911 gold badge13 silver badges15 bronze badges asked Apr 8, 2020 at 22:48 LiekeeeLiekeee 33 bronze badges 1
  • would you mind updating the question topic? Eg how to add add to cart template for custom product type in Woocommerce so that future audience may benefit from it. – 西門 正 Code Guy - JingCodeGuy Commented Apr 11, 2020 at 22:46
Add a comment  | 

1 Answer 1

Reset to default 1

You may use an action in the format woocommerce_YOUR_PRODUCT_TYPE_add_to_cart to reach the goal. Here is the example.

  • The following code is for putting in functions.php, if you are writing a plugin. Please remember to change the callback.

eg. you have a product type called Special, you want to add the add-to-cart template for it.

  • add the action filter woocommerce_special_add_to_cart
  • add the action function woocommerce_special_add_to_cart
// it is better to follow the naming conventions in Woocommerce for the ease of understanding

add_action( 'woocommerce_special_add_to_cart', 'woocommerce_special_add_to_cart', 30 );
function q363582_special_add_to_cart() {
   /**
     * Output the special product add to cart area.
     */
    function woocommerce_special_add_to_cart() {
        wc_get_template( 'your-own-path/special.php' ); // if you have arguments, you could use, read the the manual links of this function for reference
    }
}

For the path, since Woocommerce allow template customisation in theme, so your path could be something like your-theme/single-product/add-to-cart/special.php if you use wc_get_template() function.

The woocommerce_special_add_to_cart action will be called by another action woocommerce_template_single_add_to_cart which will read the type of the product and create the respective action.

// usage
wc_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' )

According to this function, you may also use your own $template_path, so I think this should meet your need.

For details, you may read docs: wc_get_template()

For the usage of wc_get_template(), you may refer to source code for examples of default product types like grouped, variation and so on: wc-template-functions.php

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论