I made a redirection to a custom page when the cart is empty. Website is now multilingual, using weglot.
When the language is french, it should redirect the user to the french version of the custom page, same scenario when language is italian.
The redirection per se works, but I always end up on the german (initial language) version.
Example: When the site language is french and you click on cart (which is empty), the browser makes the following redirections:
Start: https://example.com/fr --> Clicking on cart --> https://example.com/fr/cart --> https://example.com/fr/cart-empty (where it should stay) --> https://example.com/fr/?wg-choose-original=false --> https://example.com/cart-empty
// Redirect if cart is empty
add_action("template_redirect", 'redirection_function');
function redirection_function(){
global $woocommerce;
if(is_cart()){
if( WC()->cart->cart_contents_count == 0 && weglot_get_current_language()=='fr'){
wp_safe_redirect('https://example.com/fr/cart-empty' );
exit;
}
if(WC()->cart->cart_contents_count == 0 && weglot_get_current_language()=='it'){
wp_safe_redirect('https://example.com/it/cart-empty' );
exit;
}
if(WC()->cart->cart_contents_count == 0){
wp_safe_redirect('https://example.com/cart-empty' );
exit;
}
}
}
What am I missing?
Related
I have created multiple different headers as templates with Elementor.
I would like to display all of the different headers based on user role (Logged in/Logged out) and page.
I'm looking for a code snippet that I could easily customize to assign all of the different headers for different scenarios.
Could someone please create an example code snippet that would:
Display header A for Logged Out users on the entire
website, EXCEPT pages X and Y.
Display header B for Logged In users on the entire
website, EXCEPT pages X and Y.
Display header C for Logged Out users only on pages X and
Y.
Display header D for Logged In users only on pages X and
Y.
This way, people can easily copy the code and customize it to fit their needs.
EDIT
There's 2 places where I can create templates.
1st one is added by Elementor and is found in Admin > Templates > Saved Templates. Here I can create either section or page templates (Screenshot).
2nd one is added by my theme, OceanWP, and is found in Admin > Theme Panel > My Library. Here I can create only 1 type of template. The templates created here can later be assigned as custom headers or footers to individual pages or the entire website.
Are the templates created in these 2 places considered to be template parts? Is there a difference where I choose to create the header templates?
Here's a list of the header templates I have created:
Template title
Post ID
A
Main Header (Logged Out)
5448
B
Main Header (Logged In)
6714
C
Checkout Header (Logged Out)
6724
D
Checkout Header (Logged In)
3960
Here's the page I want to have a different header than the entire website:
Page title
Post ID
Slug
X
Checkout
18
checkout
Something like this should work:
<?php
if (! is_user_logged_in() && ! is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header A
}
if (is_user_logged_in() && ! is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header B
}
if (! is_user_logged_in() && is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header C
}
if (is_user_logged_in() && is_page(array( 'page-x-slug', 'page-y-slug' ))){
// display header D
}
?>
The following offer the same result as the previous answer but is minified and has less repetitiveness.
<?php
if ( is_page( [ 'page-x', 'page-y' ] ) )
if ( is_user_logged_in() )
get_header( 'B' ); //... header-B.php
else
get_header( 'A' ); //... header-A.php
else
if ( is_user_logged_in() )
get_header( 'D' ); //... header-D.php
else
get_header( 'C' ); //... header-C.php
?>
Following your comments
I'm guessing that what you refer as...
section templates
...are in fact templates part. Instead of using get_header( string $name ); you would then use get_template_part( string $slug, string $name = null );.
$slug and $name can be anything that you chose.
Source # https://developer.wordpress.org/reference/functions/get_template_part/
For example, section-A.php would be get_template_part( 'section', 'A' );.
<?php
//...
if ( is_user_logged_in() )
get_template_part( 'section', 'B' ); //... section-B.php
else
get_template_part( 'section', 'A' ); //... section-A.php
?>
In regards to specifying pages and templates. is_page() can take IDs, slugs or titles.
is_page( int|string|int[]|string[] $page = '' )
Parameter
Description
$page
(int|string|int[]|string[]) (Optional) Page ID, title, slug, or array of such to check against. Default value: ''
Source # https://developer.wordpress.org/reference/functions/is_page/
But you could also use other is_ function like is_search() or is_archives(), is_404()... etc.
Here is a complete list # https://codex.wordpress.org/Conditional_Tags
If you want to add multiple conditional statement you can just add elseif statements in-between.
<?php
if ( is_page( [ 'page-x', 'page-y' ] ) )
//...
elseif ( is_search() || is_archive() )
//...
else
//...
?>
If you want to get a better understanding of PHP operators, which are how conditional statements are built, take a look # https://www.w3schools.com/php/php_operators.asp
I have a full website that I've built already. I have now installed Wordpress in a subdirectory (/shop) and I'm using WooCommerce to power the sale of products.
In the top menu bar while in the shop page I have a cart icon showing the number of products in the cart.
I would like that when the user clicks back into the non-Wordpress pages of the site to continue showing the number of items in the cart. Can someone point me in the right direction? I have tried to include the main class-woocommerce.php class but it's not working.
Here's what I have been using to test in my index.php file:
include 'shop/wp-content/plugins/woocommerce/includes/class-woocommerce.php';
global $woocommerce;
var_dump($woocommerce->cart);
EDIT:
I have now included wp-load.php instead of the class-woocommerce.php and when I dump the WooCommerce cart I can see it but it's completely empty, ie has 0 items even though I had items placed in the cart.
Update:
This could be done also setting a custom cookie or better using javascript session storage this way:
add_action('wp_footer', 'custom_cart_item_count_script');
function custom_cart_item_count_script(){
if( WC()->cart->is_empty() )
$cart_count = 0;
else
$cart_count = WC()->cart->get_cart_contents_count();
if(isset($cart_count)){
?>
<script type="text/javascript">
jQuery(function($){
var cartCount = <?php echo $cart_count; ?>,
csName = 'CARTCOUNT',
csStorage = sessionStorage.getItem(csName);
if(csStorage == null || csStorage == 'undefined'){
sessionStorage.setItem(csName, cartCount);
console.log(sessionStorage.getItem(csName));
}
});
</script>
<?php
}
}
This code goes in function.php file of your active child theme (or active theme).
Tested and works.
On your non Wordpress pages:
You will need to get this browser session storage value. so you will add something like the following in your non Wordpress pages:
?>
<script type="text/javascript">
jQuery(function($){
var caName = 'CARTCOUNT',
caStorage = sessionStorage.getItem(caName);
if(caStorage == null || caStorage == 'undefined')
$('span.cart-count').html('0');
else
$('span.cart-count').html(caStorage);
console.log('css-read: '+caStorage);
});
</script>
<p>Cart count: <span class="cart-count"></span></p>
<?php
Tested and works:
So basically I have several pages with Woocommerce products listed in them (same products in Page 1 and Page 2) . What I have to do is basically open up different form based on which page the product was clicked on.
Example:
I open 1st page width products and click on product -> Form nr 1 Opens
I open 2nd page with products and click on product -> Form nr 2 Opens
Any ideas how to approach this problem? Don't know which direction to even look.
You can add a content filter to detect the page and amend the content of it.
add_filter( 'the_content', 'my_content_filter' );
function gwp_ec_content_filter ($content)
{
global $post;
if ($post->ID == $page1_page_id)
{
// Change $content
} else if ($post->ID == $page2_page_id)
{
// Change $content
}
}
Or if you doing this inside a theme page template, then
global $post;
if ($post->ID == $page1_page_id)
{
// open form 1
} else if ($post->ID == $page2_page_id)
{
// open form 2
}
Here's what is currently working with not displaying on the homepage. And displaying on all other pages, need help not displaying on /checkout/cart/ and /onepage/ and onepage/successs.
<?php if(Mage::getBlockSingleton('page/html_header')->getIsHomePage()): ?>
<?php else: ?>
(Div I am trying to display only on pages other than homepage,checkout,success)
<?php endif; ?>
Please try this
$routeName = Mage::app()->getRequest()->getRouteName();
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
if($routeName == 'cms' && $identifier == 'home') {
echo 'This is Magento Homepage.';
} else {
echo 'This is not a Magento Homepage.';
}
this should work for home page only .For hide on multiple pages you can create a array of pages and check if route name exist in that
thanks
place below code in your header file.
for home page :
<?php if ($this->getIsHomePage()){echo 'this is home page';}else{echo 'this is not home page';}
now we will take one variable $pageIdentifier for other then home page
<?php $pageIdentifier = Mage::app()->getFrontController()->getAction()->getFullActionName();?> /* this identifier value will change as per page.so echo this variable so you will get page identifier on all pages*/
for cart page :
if($pageIdentifier == 'checkout_cart_index'){echo 'this is cart page';}
for onepage checkout page :
if($pageIdentifier == 'aw_onestepcheckout_index_index'){echo 'this is checkout page';}
for order sucess page :
if($pageIdentifier == 'checkout_onepage_success'){echo 'this is sucess page';}
using this code you can get other pages identity and use as per your need.
Hope this will work.
I've three headers in my theme, and i theming the shop with my theme using templates overriding default templates.
i've this in my header.php
Header 1: For my Home site (Not shop)
Header 2: For all pages of my site (Not shop)
Header 3: For shop (Product Categories, single product, archive product)
But, when i buy any product, i go to any checkout page (Register, Cart, My Account, etc), Header 2 appears.
Simplified header.php code:
if(!is_woocommerce()) {
<!--Header Wordpress-->
if(is_home);
Home site header (Header 1)
} else {
All site header (except Home) (Header 2)
<!--End Header Wordpress-->
} else { // Woocommerce conditional
Shop header (Header 3)
}
How to solve it?
The cart and checkout are standard pages with short codes and aren't included in the other Woo templates, i.e. is_woocommerce() won't return true for them. So perhaps use
if ( is_page( 'checkout-page-slug' ) || is_page( 'other_shop_page' ) ) {
instead to target them.
Edit:
Replace checkout-page-slug or other_shop_page with the slug of a page you want to target to have the shop header:
if ( ! is_woocommerce() ) {
if ( is_home() ) {
// Home site header (Header 1)
} elseif( is_page( 'checkout-page-slug' ) || is_page( 'other_shop_page' ) ) {
// Shop header
} else {
// All site header (except Home) (Header 2)
}
} else { // Woocommerce conditional
// Shop header
}