{"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

While building the \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\n\n\n\n

The initial position<\/h2>\n\n\n\n

Until the change, I used the following \u2013 simplified \u2013 code to display the buy button:<\/p>\n\n\n

<?php<\/span>\n$theme_id = get_field( 'woocommerce-product'<\/span> );\n$theme_product = wc_get_product( $theme_id );\n$add_to_cart_url = $theme_product->add_to_cart_url(); ?><\/span><\/span>\n<a<\/span> class<\/span>=\"add-to-cart\"<\/span> href<\/span>=\"<?php echo $add_to_cart_url; ?>\"<\/span>><\/span>\n\t<?php<\/span> _e( 'Add to Cart'<\/span>, 'fbn'<\/span> ); ?><\/span><\/span>\n<\/a<\/span>><\/span><\/code><\/div>Code language:<\/span> HTML, XML<\/span> (<\/span>xml<\/span>)<\/span><\/small><\/pre>\n\n\n

I fetch the ID of the WooCommerce product from an ACF field, the product object with 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

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

\/**\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( 'single-product\/add-to-cart\/simple.php'<\/span> );<\/code><\/div>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

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":{"footnotes":"","wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","lazy_load_responsive_images_disabled":false},"categories":[115],"tags":[],"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"}],"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}]}}

To get the included template working, I used 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 wp_reset_postdata()<\/code><\/a> after the loop.<\/p>\n","protected":false},"excerpt":{"rendered":"