0

I'm trying to optimize my functions.php file as I have a Woocommerce site with a bunch of customizations to the theme. Currently, my functions look like the following:

add_action('wp_footer', 'enqueue_product_modals');
function enqueue_product_modals() {
    global $product;  //Accessing the Global
    $product_id = $product->get_id();
    if (is_product()) {
        //Standard Modal:
        require_once 'modal-product.php';

        if (has_term('guitar-pickups', 'product_cat', $product_id)) {
            include_once 'modal-polarity.php';
        };

        if (has_term('mini-humbuckers', 'product_cat', $product_id)) {
            include_once 'modal-minihum.php';
        };
    }
}

add_action('woocommerce_after_single_product_summary', function () {
    global $product; //Accessing the Global Again
    $name = $product->get_name();
    if (have_comments()) {
        echo '<p class="reviews-tagline">Trying to leave a review for our ' . $name . '? <a class="expand" data-toggle="collapse" data-target="#review_form_wrapper" >Leave one here.</a></p>';
    }
}, 50);

etc.

I understand accessing the global $product multiple times is bad practice. So, I started to rewrite functions by declaring the global $product at the top of my functions.php file, (like I would in JS) and passing it in to each individual function like so:

global $product;

add_action('wp_footer', 'enqueue_product_modals');
function enqueue_product_modals($product) {
    $product_id = $product->get_id();
    if (is_product()) {
       // code here...
    }
}

add_action('woocommerce_after_single_product_summary', function ($product) {

    $name = $product->get_name();
    if (have_comments()) {
       //code here
}, 50);

The problem is that now all functions are broken and when doing a var_dump($product) inside each function $product is an empty string. I am now positive I don't know what I'm doing. Can someone offer any assistance on the correct way to access the $product global in each function...or a better way?


Edit:

I also tried passing the $post object, so rewriting the function like so, but it's still an empty string:

add_action('wp_footer', 'enqueue_product_modals');
function enqueue_product_modals($post) {

    if (is_product()) {

$product = wc_get_product($post->ID); // hoping to use this
        var_dump($post); //Still empty String

    }
}

6
  • I don’t see anything wrong with what you were doing originally. – Jacob Peattie Apr 23 at 10:44
  • I was under the assumption and have read that using global multiple times is a bad practice and can slow down code. Is this incorrect? – sackadelic Apr 23 at 11:08
  • The global is there for exactly this reason. Using it to add features and extending on the existing code is what it is designed to do :) – Anake.me Apr 23 at 11:31
  • Is there a way to pass it as an argument to clean the code up? – sackadelic Apr 23 at 12:29
  • 1
    You're overthinking it. There's nothing wrong with your original code. You were using the global the way it's intended to be used. The reason it's empty when you access it the other way is because it does not exist as a global inside your function (because you didn't declare it as a global variable). It's just a new variable for that function, and therefore empty. If you want to access the global variable, you have to declare it as global INSIDE your function. – butlerblog Apr 23 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.