Home > Mobile >  How To Use do_shortcode with WooCommerce One Page Checkout
How To Use do_shortcode with WooCommerce One Page Checkout

Time:04-28

I'm trying to use the WordPress do_shortcode function for the WooCommerce One Page Checkout Plugin which uses shortcode like this: [woocommerce_one_page_checkout template="product-table" product_ids="product, ids, here"]. It seems like I can only use this shortcode IF it's in the content editor and won't allow me to add this to a page template using the do_shortcode function.

Their documentation here says:

If you wish to display the One Page Checkout Shortcode using WordPress’ do_shortcode() function instead of including the shortcode in a post or page’s content, you will also need to attach custom code to the 'is_wcopc_checkout' filter and make sure a boolean true value is returned.

So I tried adding the following to the functions.php file:

add_filter( 'is_wcopc_checkout', function(){ return true; } );

and it didn't seem to do the trick.

I also tried:

add_filter( 'is_wcopc_checkout', 'my_one_page_checkout' );
function my_one_page_checkout(){
  return true;
}
add_filter( 'is_wcopc_checkout', 'true' );

That didn't seem to do it either.

Am I adding this code to the functions.php wrong? Any help on how I can get the One Page Checkout Plugin to work using do_shortcode?

Here's my full code in the page template for reference:

<?php 
echo do_shortcode('[woocommerce_one_page_checkout template="product-table" product_ids="62, 122, 438, 52, 433, 435, 512, 514"]');
?>

Thanks for your help.

(I tried contacting WooCommerce support and they were no help saying that this is custom code and they can't do anything to help.)

CodePudding user response:

The simplest way to return a true to a filter is like sitting the call back to WP default __return_true. So the function will be like

add_filter( 'is_wcopc_checkout', '__return_true' );

There is no filter named is_wcopc_checkout in the code of WooCommerce one page checkout version 1.0.2

From their doc- You can also manually add a shortcode [woocommerce_one_page_checkout] to any page or post and use the shortcode's attributes.

Usage: [woocommerce_one_page_checkout product_ids="30,45,12"]

CodePudding user response:

I bought Onepage checkout plugin for woocommerce and i need to display it in all woocommerce product pages using a shortcode. The documentation says it requires additional custom code: https://woocommerce.com/document/woocommerce-one-page-checkout/#section-38 I would like to use this shortcode: [woocommerce_one_page_checkout product_ids="2530,2575" template="product-table"] This works if i put it in any page, but i want to use this “layout” by default. I will always use this shortcode. I added this code to functions.php, but it doesn’t work.

add_filter('woocommerce_after_single_product', 'show_onepage_checkout'); function show_onepage_checkout(){ return (do_shortcode('[woocommerce_one_page_checkout product_ids="2530,2575" template="product-table"]')); }

  • Related