1

Please forgive this totally newb question. I'm not a dev, and I'm a little too far that I can deal with right now...

I'd like to add a link to the WooCommerce cart page using the cart icon from my menu located at: http://gimp4.devcaneve.ca. I have a friend who coded the php function and I believe I just have to add it in the functions.php file of my child theme. Here is the code of the function :

// cart display function

function directByLang()
{

 $current_lang = apply_filters('wpml_current_language', NULL);

 if ($current_lang == 'en') {
 // English link
 echo '<a href="/en/cart/">English Cart</a>';
 } elseif ($current_lang == 'fr') {
 // French link
 echo '<a href="/panier/">French Panier</a>';
 } else {
 // Default to French if language is not determined
 echo '<a href="/panier/">French Panier</a>';
 }
}

Now first of all neither my friend nor I know if this function is properly coded because my friend never worked with WP... And I'm no dev...

On my home page, there is a shopping cart icon with a red bubble with the number of items in the cart. When this is clicked, it opens a popup cart that I want to hide. I was able to hide this ugly popup with css using display: none;

But I would like that the cart icon could direct the users to either the French cart (/panier/) or the English cart (/en/cart/) onclick on the icon, depending on the language selected by the users when they entered the site.

I'm pretty sure this has something to do with css but honestly I'm overwhelmed, as I have a very steep learning curve ahead of me.

I know I'm asking a lot, and any help will be appreciated. I gotta start somewhere, if I want to learn these things one day... Thanks.

1 Answer 1

0

There's a few things to consider here.

1) You should always first check for a third-party plugin like WPML. If, for whatever reason, WPML stops working or gets deactivated, assuming it's there could result in a fatal error.

function directByLang() {
    if( defined( 'ICL_SITEPRESS_VERSION' ) ) {
        $current_lang = apply_filters('wpml_current_language', NULL);
         if ($current_lang == 'en') {
             // English link
             echo '<a href="/en/cart/">English Cart</a>';
         } elseif ($current_lang == 'fr') {
             // French link
             echo '<a href="/panier/">French Panier</a>';
         } else {
            // Default to French if language is not determined
            echo '<a href="/panier/">French Panier</a>';
         }
    } else {
        // Default to French if WPML not active
        echo '<a href="/panier/">French Panier</a>';
    }
}

I haven't used WPML in a few years so not sure if this is the correct method anymore. I found it here: https://wpml.org/forums/topic/check-if-wpml-is-active/

2) This does check for the language and then depending on the language it outputs the different cart URLs. But just adding this snippet/function alone to your functions.php won't do anything. Because it doesn't get loaded anywhere. This is basically a template tag, so in the file where you want this to appear, you have to add <?php directByLang(); ?>.

I would assume that you want it in your header.php.

For example, in your header.php you'll have some <div> and <nav> elements that organize he logo/site name, navigation, search field, etc...

Somewhere in there, where it makes sense, you'll want to add <?php directByLang(); ?> so that the function executes in there.

You'll probably need some CSS, maybe even wrap it in it's own container, like so:

<div class="multi-language-cart-link">
    <?php directByLang(); ?>
</div>

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.