Wordpress Woocommerce cart scripts enqeue for shortcodes - php

I'm trying to figure out how to load the cart.min.js scripts not only if IS_CART but also if SHORTCODE_EXISTS('woocommrece_cart'), I can see that all the fronted scripts enqueues are in class-wc-frontend-scripts.php file and I don't want to override it, is there any hook that I can use to add another IF statement to the same file?
THANKS :)
after some testing
I want to explain my problem again:
I have a 'woocommerce_cart' shortcode that doesn't loads the scripts that it needs to work via ajax (like when updating cart and etc...) on other pages than the cart page
I see that in the class-wc-frontend-scripts.php the enqueue of that script goes only when is_cart() statement.
I am figuring out that I need to add if(shortcode_exists('woccommerce_cart')) so it would enqueue also when it is a shortcode.
I am adding that IF and I see that everything works on front end.
I am thinking that it is not a good idea to put that in the original plugin file because it would be overwritten with the next update.
I am trying to enqueue the script in my functions.php file with the new IF.
Nothing works....
Going back to the original file
On line 247 (here: https://github.com/woothemes/woocommerce/blob/master/includes/class-wc-frontend-scripts.php) there is a function that adds more functionality to the ajax that without it it wont work...
Now I am wondering is there a way to add another IF to the same file using a hook? Or should I override all the file? (as I said before I think it is not a good practice because all the functions there wont get any updates).
So basically I know what I need to do, I just don't know what is the best way to do it and if it possible doing it with a hook or other function that maybe somebody knows and I'm not familiar with....
Thank you again for your help! :)

SHORT ANSWER
You need to define the WOOCOMMERCE_CART constant and make sure you're using a modern version of Woocommerce. I've tested this solution in Woocomerce 3.0.7.
if (!defined('WOOCOMMERCE_CART')) define( 'WOOCOMMERCE_CART', TRUE );
LONG ANSWER
I was having the same problem:
- I was calling the [woocommerce_cart] shortcode with the do_shortcode() function to display the cart in all the pages of my site.
- The cart was displaying well but the scripts (specifically cart.min.js) weren't loading in the pages. The effect of this is that the AJAX interactions weren't working.
The pages in my site were already running WC_Frontend_Scripts::init(). This function calls WC_Frontend_Scripts::load_scripts and that function loads our cart.min.js only if is_cart() (file wc-conditional-functions.php) is TRUE, as you already pointed.
Now, the current version of is_cart() evaluates TRUE when any of the following conditions is TRUE:
is_page( wc_get_page_id( 'cart' ) )
defined( 'WOOCOMMERCE_CART' )
wc_post_content_has_shortcode( 'woocommerce_cart' )
So, for me, the easiest way to fix the problem was to define the WOOCOMMERCE_CART constant.
Hope this helps. :-)

I had the exact same problem, combining cart and checkout pages broke the ajax auto cart
updates. I add the cart to the checkout page using:
function cart_on_checkout_page()
{
if ( is_wc_endpoint_url( 'order-received' ) ) return;
echo do_shortcode('[woocommerce_cart]');
}
add_action( 'woocommerce_before_checkout_form', 'cart_on_checkout_page', 5 );
My solution was to use the following code in functions.php to check if we are on the checkout page, and if so, enqueue/load the 'wc-cart' script. It's working with Woocommerce version 4.5.2.
function adjust_woocommerce_scripts()
{
if ( is_checkout() ) wp_enqueue_script( 'wc-cart' );
}
add_action( 'wp_enqueue_scripts', 'adjust_woocommerce_scripts', 11 );
You can adjust this function to perform whatever checks you need, such as if SHORTCODE_EXISTS('woocommrece_cart') - this means that the 'wc-cart' script is only loaded on the pages you need.

Related

Wordpress WooCommerce 'woocommerce_hidden_order_itemmeta' filter hook not being triggered

I've got the hook set up for 'woocommerce_hidden_order_itemmeta' but it is not being triggered during a purchase. My code is below. Note, the code is simplified with the function simply logging a message so I can see if it is being triggered.
function filter_woocommerce_hidden_order_itemmeta( $array ) {
error_log("GOT TO filter_woocommerce_hidden_order_itemmeta()");
return $array;
}
add_filter( 'woocommerce_hidden_order_itemmeta', 'filter_woocommerce_hidden_order_itemmeta', 10, 1 );
I have configured many other WooCommerce hooks and they are being triggered correctly. Is there something special about 'woocommerce_hidden_order_itemmeta'? Does something have to be configured elsewhere before this hook is triggered?
Thanks in advance for any help!!
Cheers!!
I had this same problem recently. Still trying to figure out what's going on, but I can sure you that, if you put this code at functions.php of theme, there's a great chance to get this specific hook to work.
However, this problem affects not only woocommerce_hidden_order_itemmeta hooks, but other ones like woocommerce_after_order_itemmeta, and probably everything suffixed by _order_itemmeta (only a personal guess in this last part)
Obs: Before trying this workaround above, certify yourself that you're using this hook on Order Details Metabox. I think this is the only place where these hooks like *_order_itemmeta are supposed to get work, anyway.

How to trace which function in Wordpress adds certain code to header?

I want to remove most css and scripts added to my Wordpress by plugins and maybe core Wordpress. How can I trace which function adds which line in my page header?
Tried to run search for the lines I want to remove but unsuccessfully. I know already that the code is done through wp_header hook but it does not help finding exact function.
If you know which plugins add the code you can go through plugin files in wp-content/plugins/plugin-name and find following functions.
wp_enqueue_script()
wp_enqueue_style()
Usually it will be together and wrapped in another function which will be called by action:
add_action( 'wp_enqueue_scripts', 'name_of_function' );
For not loading the scripts just uncomment add_action line.
But please be aware, that those scripts are usually vital for correct function of plugin so you will have to serve them from somewhere else.
Also when disabling wp_enqueue be sure you are not disabling admin scripts which would be loaded by action
add_action( 'admin_enqueue_scripts', 'name_of_function' );
To be sure always check parametres of wp_enqueue function where script name should match the script you had previously seen in head section of your site.
Every update of plugin will override the files and therefore enable scripts again
Hope this helps.

Conditionally Load Styles and Scripts in Wordpress

I'm looking for a solution to conditionally load scripts and styles on specific Wordpress pages. I've searched for solutions, but have hit a dead end. I haven't been able to use php selectors, such as is_page(), in the functions.php file to do this. Here is an example of my past attempt:
function blog_style() {
if ( is_page('blog') ) {
wp_enqueue_style( 'blog' );
}
}
add_action( 'wp_print_styles', 'blog_style' );
I have a feeling this is because the functions.php file is loaded before the page has been identified. Any advice is helpful. I have a basic understanding of php and Wordpress codex. Thanks!
I understand your confusion in this matter and must confess that WordPress sometimes has strange ways to handle things.
First of all; you should enqueue scripts and styles in the wp_enqueue_scripts hook:
add_action('wp_enqueue_scripts', 'themeslug_styles');
function themeslug_styles() {
wp_enqueue_style(...);
}
Or in shorthand-/anonymous-function-format, if you prefer:
add_action('wp_enqueue_scripts', function () {
wp_enqueue_style(...);
});
The basic idea of your is_page('blog') in an if-statement is correct. But also check out this detailed info about conditional tags - maybe there are better ways to target the blog-page of your website (see "The Blog Page" on the linked resource).
You must use the if-statement inside your function, because it is only at the point of wp_enqueue_scripts, where the function is run, that WordPress has information about the actual content ready.
add_action('wp_enqueue_scripts', function () {
if (is_home()) {
wp_enqueue_style('themeslug-stylename', URL-to-your-style);
wp_enqueue_script('themeslug-scriptname', URL-to-your-script);
}
});
Addition: You are right, the functions.php file is mainly used to set everything up and connect functions as actions to WordPress hooks. Therefore, the code is executed quite early in the WordPress execution flow to give you access to as many hooks as possible. The downside is, as you experienced, that many things are not directly accessable at this point.
I think you are using the wrong hook here. Try
add_action( 'wp_enqueue_scripts', 'blog_style' );
You can check this function. I had used this function to dynamically load scripts on different post types and pages. I used the screen_id to specify the current page.

Wordpress plugin - how to disable js/css per page

With Wordpress you have good/bad plugins, some being much more economical than others. By economical I mean that they only call the CSS and JavaScript required for the function when the page is loaded where that function is enabled. Others don't discriminate and call the code on all pages. This can have a negative effect on page load speed/performance.
I have some plugins that are heavy in CSS and are laden with reems of jQuery/Javascript files - I only want them to be enabled on particular pages (not home). In hand I have the page ID and alias. Looking in the plugin folder I also see the main php file that includes all the JS / CSS. Within that file I tried something like is_page() but it seems to have no impact as if is_page has not yet been set.
<?php if ( is_page( '3486' ) ) { exit; } ?>
exit on a line by itself kills the page indicating that the script is being called.
The question, "How and where do you place an if statement that will prevent the plugin CSS/JavaScript from being called on all pages but a particular one (or perhaps an array of pages)?
I could name the plugin but the question is really more generic to any plugin.
You can use wp_deregister_script, this will remove unwanted JS,CSS from specific pages.
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript()
{
if ( is_page('YOUR PAGE NAME') )
{
wp_deregister_script( 'WORDPRESS JS file NAME' );
}
}
Refer : https://codex.wordpress.org/Function_Reference/wp_deregister_script

How does wordpress wp_enqueue_script() does work more exactly

wp_enqueue_script() as I read about so far and went through the source code instantiates new wp_scripts object 'representing' the script.
My question is how does wordpress know when to load the the script source when needed ?
For example, on index I need bootstrap and jquery, I enqueue bootstrap with jquery dependency in functions.php. How does wordpress know to automatically load the bootstrap on the first page ? What I want to understand is the logic behind it.
I have to create a new plugin and I need some scripts for a slideshow, what I want is to create a quality plugin, optimized using wp_enqueue_scripts, but I don't really understand the concept in-depth so I can use it properly (to load the scripts only when the plugin is activated)
You can treat wp_enqueue_scripts as a kind of action_hook. I.e. when it is time to create pages, it will include the file. You can also make it dependant on another file being loaded, so if jQuery is needed, you can stipulate this in the function and it will load after jQuery. This maintains seperation of your code.
If you want to optimize the code, you can make the function dependant on the page you are loading.
Forexample:
function prepare_scripts() {
if(is_front_page() )
wp_enqueue_scripts('myscript', 'path to script', array( 'jquery' ) );
}
add_action( 'wp_enqueue_scripts', 'prepare_scripts' );
As #knoblik suggests, you can set the priority for the add_action() if needed. This will probably enqueue the script in the order of the priority.

Categories