I'm interested in creating shortcodes that basically fill in template parts of the WooCommerce checkout. For example, in functions.php of my child theme:
function shortcode_review_order() {
//get the template part from woocommerce/templates/checkout/review-order.php
wc_get_template_part('checkout/review-order');
}
add_shortcode( 'custom_review_order', 'shortcode_review_order' );
...and then in my page...
<div>[custom_review_order]</div>
When I tried this, nothing appeared in my checkout page.
Is this even possible?
there are few things wrong in your code...
first, you should add a shortcode using the init hook.
add_action( 'init', 'add_shortcodes' );
function add_shortcodes(){
add_shortcode( 'custom_review_order', 'shortcode_review_order' );
}
then you lack the .php part of the template. Also it needs the array parameter like below. And you might get more accurate result using wc_get_template.
function shortcode_review_order(){
wc_get_template( 'checkout/review-order.php', array( 'checkout' => WC()->checkout() ) );
}
To know more on how to correctly use it's template, search for each on the plugin. You'll see how it's being used. And you can get a hint on how you can use it for yourself.
I found that wc_get_template echos out the template, instead, for shortcodes returning the template is better. You can use:
$string = wc_get_template_html( $template_name, $args, $template_path, $default_path );
It is "Like wc_get_template, but returns the HTML instead of outputting."
https://docs.woocommerce.com/wc-apidocs/function-wc_get_template_html.html
Related
I am using a hierarchal custom post type (post type is called locations, slug = location) in WordPress. Locations can be nested (country/state/city)
I have successfully added custom query vars :
add_filter('query_vars', function($vars) { $vars[] = "view"; return $vars; });
which I use to decide what data to show for the location.
For example, mysite.com/location/country/?view=facts or mysite.com/location/country/state/city/?view=events
All of which is working great.
But I want to be able to access it as:
mysite.com/location/country/facts
mysite.com/location/country/state/city/facts
I have been playing around with add_rewrite_rule but can't make it work. Not sure if my $regex or $query is the problem; regex isn't my strong suit.
add_rewrite_rule( '/(view)/g', 'index.php?post_type=locations?view=$matches[1]','top' );
Try add_rewrite_endpoint, it is actually much simpler
https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
https://make.wordpress.org/plugins/2012/06/07/rewrite-endpoints-api/
I want to create a shortcode that will show the template parts mytheme/woocommerce/single-product/add-to-cart/variable.php on my Woocommerce product page where I want. My target is to set my page fully from WP Barkery on Long Description and not call this template part from templates files.
I made :
function shortcode_add_to_cart_variations(){
wc_get_template_part( 'single-product/add-to-cart/variable' );
}
add_shortcode('add-to-cart-variations', 'shortcode_add_to_cart_variations');
The template part is called but the variations it's not shown because $available_variations = NULL : https://github.com/woocommerce/woocommerce/blob/3.5.0/templates/single-product/add-to-cart/variable.php#L26
How can I make my shortcode and show variations where I want on my product page (for example in my Revolution Slider)?
I found myself the answer.
I only need to use the core woocommerce function woocommerce_variable_add_to_cart()
Detail here: http://hookr.io/functions/woocommerce_variable_add_to_cart/
Add this in my functions.php (child theme) :
function shortcode_add_to_cart_variations(){
woocommerce_variable_add_to_cart();
}
add_shortcode('add-to-cart-variations', 'shortcode_add_to_cart_variations');
My showAction() looks like this:
public function showAction() {
$form = $this->formRepository->findByUid($this->settings['form']);
$arguments = $this->request->getArguments();
$this->view->assignMultiple(
array(
'form' => $form,
'arguments' => $arguments
)
);
}
In the template of this action I try to give 'arguments' to all my partials:
<f:render partial="FormElement/{formElement.type}" arguments="{formElement:formElement, arguments:arguments}" />
The debug of {arguments} in the template shows me that everything is fine. If I debug the {arguments} inside the partials, it returns NULL. Even if I give all arguments to the partials (arguments="{_all}"), NULL will be returned by debugging {arguments} inside the partials.
Everything else works fine!
Your code looks fine to me. You might want to check your php.ini file for the following setting "max_input_nesting_level". Sometimes, if this parameter is set to a low value and your handling a complex object you run into this restriction. You might want to change it to 128 oder even 256. (Don't forget to restart your apache after changing your php.ini)
I got problem when implementing my CMS using Codeigniter 1.7.2 and Dwoo. I use Phil Sturgeon Dwoo library. My problem is I want user create template from the admin panel, it means all template will be stored into database including all Dwoo variable and functions.My questions:
Is it possible to load dwoo template from database?
How to parse dwoo variable or function from database? I tried to load content from database which is include dwoo var and function inside it, and i have tried to do evaluation using dwoo eval() function and phil sturgeon string_parse() but still have no luck.
for example:
my controller
$data['header'] = "<h1>{$header}</h1>"; --> this could be loaded from database
$this->parser->parse('header',$data);
my view
{$header}
This is the error message:
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Undefined index: header_title</p>
<p>Filename: compiled/805659ab5e619e094cac7deb9c8cbfb5.d17.php</p>
<p>Line Number: 11</p>
header_title is dwoo variable that loaded from DB.
Thank you,
It is definitely possible to do this, but for performance reasons it would probably be faster to store the templates as files on the filesystem, even if they're edited by users. If you're smart with file naming you can avoid most hits to the database.
Anyway, if you really want to do it with the database, the following should work:
// rendering the header
$template = '<h1>{$header}</h1>'; // loaded from db, don't use double-quotes for examples or $header will be replaced by nothing
$data = array('header' => 'Hello'); // loaded from db as well I assume
$headerOutput = $this->parser->parse_string($template, $data, true); // true makes it return the output
// now rendering the full page (if needed?)
$data = array('header' => $headerOutput);
$this->parser->parse('header', $data); // no 'true', so goes straight to output
The view would then contain {$header} and the output from the header template is passed to that variable.
Now I'm not sure how CI works so it might be better to just output the result of the first template and skip the second one entirely, but I'll leave that up to you.
From another forum I found the following example:
"I was looking for a way to pull node data via ajax and came up with the following solution for Drupal 6. After implementing the changes below, if you add ajax=1 in the URL (e.g. mysite.com/node/1?ajax=1), you'll get just the content and no page layout.
in the template.php file for your theme:
function phptemplate_preprocess_page(&$vars) {
if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
$vars['template_file'] = 'page-ajax';
}
}
then create page-ajax.tpl.php in your theme directory with this content:
<?php print $content; ?>
"
This seems like the logical way to do it and I did this, but the phptemplate_preprocess_page function is never called ... any suggestions?
I figured it out for myself from a Drupal Support Theme Development page:
"Maybe this helps
leahcim.2707 - May 29, 2008 - 05:40
I was trying to get the same thing done and for me this works, but I'm not sure if it is the correct way as I'm still new to Drupal:
in "template.php" I added the following function:
function phptemplate_preprocess_page(&$vars)
{
$css = $vars['css'];
unset($css['all']['module']['modules/system/system.css']);
unset($css['all']['module']['modules/system/defaults.css']);
$vars['styles'] = drupal_get_css($css);
}
I think after adding the function you need to go to /admin/build/themes so that Drupal recognises the function."
The part in bold is what did the trick ... you have to re-save the configuration so it recognizes that you've added a new function to the template.