Can I make a WooCommerce shortcode from variable template part? - templates

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');

Related

Can you make WooCommerce shortcodes from template parts?

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

Get base url in custom not.html.twig in Drupal 8

I made a custom node to show content type, say: node--blog.html.twig. Now I want to call base URL, but unable to do so. I tried code like below, but nothing worked:
Read More
Apply
Apply
How can I get base URL? I think there must be some variable like Drupal 7.
You can add the following to your .theme file:
function YOUR_THEME_preprocess_node(&$variables) {
$variables['someVarName'] = $GLOBALS['base_url'];
}
And then access it in your node template like this:
{{ someVarName }}
just in your template in YOUR_THEME.theme, add the following snippet.
function YOUR_THEME_NAME_preprocess_node(&$variables) {
$variables['base_path'] =base_path();
}

How to set Joomla template for user/visit

I want to use specific Joomla template (with no navigation, no top and bottom) for users that connect to website through my mobile app with iframe in it. I assume that this users will start connection from specific address with tmpl param (or something similar) but is there a way to lock this template for these users?
Maybe there is some plugin to set template in session data and override default template choice?
There is a Joomla extension called Template Assigner - http://extensions.joomla.org/extension/template-assigner . It does exactly what you want.
Plugin allows me to set template only for specyfic user group, not to switch template accordingly to url but thanks to Your idea I created very simple plugin to do this, code below:
defined('_JEXEC') or die;
class plgSystemMobiler extends JPlugin
{
public function onAfterInitialise()
{
$app = JFactory::getApplication();
$jinput = $app->input;
if (isset($_REQUEST['mobile'])) $par=(int)$_REQUEST['mobile'];
if (isset($par)) $jinput->cookie->set('mobile', $par, time() + 100000, $app->get('cookie_path', '/'), $app->get('cookie_domain'), $app->isSSLConnection());
$cookie=$jinput->cookie->get('mobile');
if (intval($cookie)>0) JFactory::getApplication()->input->set('templateStyle', intval($cookie));
}
}

Why use templates in Kohana?

I don't understand the purpose of using templates in Kohana. I see almost no difference in the process of building a view with a template controller vs a regular controller, except that the template controller is tied to a given template and so is less flexible. What are the advantages?
Building view with regular controller:
Class Controller_Hello extends Controller
{
public function action_index()
{
$view = View::factory('page');
$view->page_title = 'My Hello App';
$view->content = 'hello, world!';
$view->sidebar = View::factory('parts/sidebar');
$this->response->body($view);
}
}
Building view with template controller:
Class Controller_Hello extends Controller_Template
{
public $template = 'page';
public function action_index()
{
$this->template->page_title = 'My Hello App';
$this->template->content = 'hello, world!';
$this->template->sidebar = View::factory('parts/sidebar');
}
}
Controller_Template is just an example of how you can implement your own templating-system.
It is not ready-to-use solution (at least for my projects usually). Check this one controller (it is also not ready-to-use solution but possibly it will help you understand point of extending different controllers for different purposes): http://pastie.org/2563595
I am sure there are other, maybe better solutions for templating systems. But why am I using templates in Kohana?
Think about multiple pages, all based upon one layout/design scheme. So I build a template controller using a certain view, defining layout/design, defining content, header and footer "areas". In the template controller I am loading the CSS files and script files, setting the title and meta values of the website, because every single site is using these CSS/script files with the same meta values and title.
So in every Controller extending the template controller I don't need to load the CSS/script files anew, set the meta values and title etc... But I could change all these values, maybe add a CSS file only for a single site.
Maybe all the mentioned sites have the same footer and/or header: I assign the header/footer view to the template within the template controller, so I don't need to do that in all the controller extending the template controller. Or all actions in one controller have the same header/footer, so I assignt he header and footer few in the before() function of the controller...
For me templates in kohana are a good utility for building small web applications.

Why is php_template_preprocess_page function not called in Drupal 6x?

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.