1

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.)

Jason
  • 23
  • 6

1 Answers1

0

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

Some context from One page checkout readme.

To register your template, attach a callback to the 'wcopc_templates' filter and add a new array of your template's details to the $templates array passed to your function.

For example, to register a custom pricing table template, the code would be similar to:

function eg_add_opc_template( $templates ) {

    $templates['my-custom-pricing-table'] = array(
        'label'       => __( 'My Pricing Table', 'eg' ),
        'description' => __( "Display a sophisticated and colourful pricing table with each product's attributes, but not weight or dimensions.", 'eg' ),
    );

    return $templates;
}
add_filter( 'wcopc_templates', 'eg_add_opc_template' ) );

The key used in the $templates array should be the template's file name (excluding the extension). The label element of the array is the name displayed on the One Page Checkout dialog. The description element is used for the tooltip next to the template's name.

mujuonly
  • 11,370
  • 5
  • 45
  • 75
  • If there is no filter, why do they suggest to use that `is_wcopc_checkout` so we can use the shortcode with do_shortcode. Yes, I'm aware of that shortcode, the issue is that shortcode ONLY works if it's in the content of a page. It does NOT work if you use it in a template using the `do_shortcode` function as I mentioned. The goal is that I'm trying to be able to use that shortcode in a page template using the `do_shortcode` function. So I'm afraid this answer so far still doesn't help. – Jason Apr 28 '22 at 04:20
  • @Jason You may try the updated answer section under - Some context from One page checkout readme. – mujuonly Apr 28 '22 at 10:31
  • Thanks, yes, I saw that on the documentation, and that would be if we wanted to add a custom pricing table template, etc. I'm not trying to add a custom template, so it's not relevant. My quest is simple, I just want to be able to use their shortcode (i.e. `[woocommerce_one_page_checkout]`) on my own template pages using the `do_shortcode` function. – Jason Apr 28 '22 at 19:41