How To Use do_shortcode with WooCommerce One Page Checkout

452 views Asked by At

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

1

There are 1 answers

3
mujuonly On

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.