{"id":4176,"date":"2017-08-05T12:15:53","date_gmt":"2017-08-05T10:15:53","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=4176"},"modified":"2020-02-09T10:59:46","modified_gmt":"2020-02-09T09:59:46","slug":"show-add-to-cart-template-on-no-woocommerce-template","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/show-add-to-cart-template-on-no-woocommerce-template-4176\/","title":{"rendered":"Show add to cart template on no-WooCommerce template"},"content":{"rendered":"\n<p>While building the <a href=\"https:\/\/florianbrinkmann.com\/en\/4057\/pay-what-you-want\/\">\u00bbpay what you want\u00ab solution for my shop<\/a> last week, I needed to tinker a little bit to get the particular plugin working, because I do not use the product page of WooCommerce but the single view of an own custom post type to display the buy option.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">The initial position<\/h2>\n\n\n\n<p>Until the change, I used the following \u2013 simplified \u2013 code to display the buy button:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n$theme_id = get_field( <span class=\"hljs-string\">'woocommerce-product'<\/span> );\n$theme_product = wc_get_product( $theme_id );\n$add_to_cart_url = $theme_product-&gt;add_to_cart_url(); <span class=\"hljs-meta\">?&gt;<\/span><\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">a<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"add-to-cart\"<\/span> <span class=\"hljs-attr\">href<\/span>=<span class=\"hljs-string\">\"&lt;?php echo $add_to_cart_url; ?&gt;\"<\/span>&gt;<\/span>\n\t<span class=\"php\"><span class=\"hljs-meta\">&lt;?php<\/span> _e( <span class=\"hljs-string\">'Add to Cart'<\/span>, <span class=\"hljs-string\">'fbn'<\/span> ); <span class=\"hljs-meta\">?&gt;<\/span><\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">a<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>I fetch the ID of the WooCommerce product from an ACF field, the product object with <code class=\"lang-php\">wc_get_product()<\/code> and after that the add to cart URL. I put the URL into a link, and that was it.<\/p>\n\n\n\n<p>The easiest way to use the pay what you want plugin seems to be including the add to cart template of WooCommerce. That can be done fairly easy:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/**\n * Setup post data of theme post, so we can\n * get the included WooCommerce template working.\n *\/<\/span>\nsetup_postdata( $theme_id );\nwc_get_template( <span class=\"hljs-string\">'single-product\/add-to-cart\/simple.php'<\/span> );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>To get the included template working, I used <code class=\"lang-php\">setup_postdata()<\/code> to setup the post data of the theme before. That does not lead to any issues with the following code \u2013 if you are using it in a loop, you may need to use <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_reset_postdata\/\"><code class=\"lang-php\">wp_reset_postdata()<\/code><\/a> after the loop.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While building the \u00bbpay what you want\u00ab solution for my shop last week, I needed to tinker a little bit to get the particular plugin working, because I do not use the product page of WooCommerce but the single view of an own custom post type to display the buy option.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[115],"tags":[],"class_list":["post-4176","post","type-post","status-publish","format-standard","hentry","category-wordpress-snippets"],"wp-worthy-pixel":{"ignored":false,"public":null,"server":null,"url":null},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4176","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=4176"}],"version-history":[{"count":2,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4176\/revisions"}],"predecessor-version":[{"id":5863,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/4176\/revisions\/5863"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=4176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=4176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=4176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}