Determine language in functions.php [wpml] - php

I have function in functions.php , which automatically adds woocommerce product by ID to cart when website is visited.
The website is bilingual and the function can't determine translated product ID.
So, I want to know if is possible to add wpml function in functions.php, which determines first language of front end, then executes function, something like this:
<?php if(wpml_getLanguage()=='en'); ?>
---do function for product 22---
<?php elseif(wpml_getLanguage()=='it'); ?>
---do function for product 45--
<?php endif; ?>
My code:
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 22;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}

You can easily get this by using the native WPML variable ICL_LANGUAGE_CODE.
You can find more information about this topic on the following page:
https://wpml.org/documentation/support/wpml-coding-api/

This can be dropped into functions.php
add_action( 'init', 'add_product_on_language' );
function add_product_on_language(){
if ( ! is_admin() ) {
if( ICL_LANGUAGE_CODE == 'en' ){
$product_id = 22;
} elseif ( ICL_LANGUAGE_CODE == 'it' ) {
$product_id = 45;
}
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}

Related

Removing auto added product from WooCommerce

So I can use this snippet from the woocommerce docs and it works to add a free product when a cart total is reached. However, our customers need to be able to remove the product from the cart if they don't want it (repeat customers that don't want the same gift every time). With this snippet, the remove from cart button doesn't work for the free product.
/**
* Add another product depending on the cart total
*/
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 2831; //replace with your product id
$found = false;
$cart_total = 30; //replace with your cart total needed to add above item
if( $woocommerce->cart->total >= $cart_total ) {
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
$woocommerce->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
}
``
code above is from here: https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/
Removing the product from the cart will actually work, but the problem is that the code will also automatically re-add the product again.
One way to solve this would be to remember that the product has already been added once, and to not re-add the product if the product would be added another time (after it has been removed from the cart for example).
A simple way to do that would be to save that data inside of the WooCommerce session, using WC()->session. Modified code below:
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 2831; //replace with your product id
$found = false;
$cart_total = 10; //replace with your cart total needed to add above item
if( $woocommerce->cart->total >= $cart_total && ! WC()->session->get( 'free_item_given', false ) ) { // <--- ADDED CHECK FOR free_item_given HERE
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->get_id() == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found ) {
$woocommerce->cart->add_to_cart( $product_id );
WC()->session->set( 'free_item_given', true ); // <--- REMEMBER free_item_given HERE
}
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
WC()->session->set( 'free_item_given', true ); // <--- REMEMBER free_item_given HERE
}
}
}
}
I hope that it solves your problem!

Change cart item prices for specific product categories in Woocommerce

I would like to change regular price to custom price of products in the cart for specific categories only ('t-shirts-d','socks-d','joggers-d','boxers-d') as each of the products share 2 different categories.
I tried doing so and it worked but the custom price affects on other categories too, and I want to only show the original price for other categories('t-shirts','socks','joggers','boxers').
I Need help on this.
Here is my code so far:
function changeprice($html, $cart_item, $cart_item_key){
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
//$thepro = $woocommerce->cart->get_cart();
$product = $cart_item['data'];
$heading_nicename = array('t-shirts-d','socks-d','joggers-d','boxers-d');
//$heading_nicename1 = array('t-shirts','socks','joggers','boxers');
$termsother = $heading_nicename;
foreach( $termsother as $termsnew ) {
if (is_cart()) {
$price_adjusted = 666.666666667; // your adjustments here
$price_base = $cart_item['data']->sale_price;
if (!empty($price_adjusted)) {
if (intval($price_adjusted) > 0) {
$cart_item['data']->set_price($price_adjusted);
} /*else {
$html = '<span class="amount">' . wc_price($price_base)
. '</span>';
}*/
}
}
}
}
}
add_filter('woocommerce_cart_item_price', 'changeprice', 100, 3);
THE RIGHT WORKING HOOK (Updated):
add_action( 'woocommerce_before_calculate_totals', 'change_cart_items_prices', 10, 1 );
function change_cart_items_prices( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
foreach ( $cart->get_cart() as $cart_item ) {
if( has_term( array('t-shirts-d','socks-d','joggers-d','boxers-d'), 'product_cat', $cart_item['product_id'] ) ){
$price_adjusted = 666.666666667; // your adjustments here
if ( ! empty( $price_adjusted ) && intval( $price_adjusted ) > 0) {
$cart_item['data']->set_price( $price_adjusted );
}
}
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This time everything work on cart and on checkout pages. Totals are updated too.

Woocommerce: add to cart automatically when visiting a page (variable product IDs)

I am aware that there's already an existing function where you can add a certain product on cart upon visiting the website. The problem is you can only do it with 1 product and I want it variable depending on the page you are visiting.
here's the existing code (added on functions.php file)
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 275;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
Inside my landing page called page-template.php, there's a custom field that echos the ID of a certain product selected. So when my user visits this page, I want this ID to replace the $product_id on the functions.php file. How do I achieve this?
Many thanks in advance for the help.
maybe the global variable is loaded after the function. instead of just setting the variable initiate the function passing the variable and avoid the hook:
in functions.php:
function add_product_to_cart($product_id) {
if ( ! is_admin() ) {
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
in page-template.php:
add_product_to_cart(275);

Automatically add multiple products to cart on visit WooCommerce

I am working on a webshop test case and what I would like is to add automatically items/products to the cart on visit. So I searched for something like that when I found the same code over and over everywhere.
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
From:
https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/
https://gist.github.com/kloon/2376300
So this works fine if you have only one product but I would like to add more than just 1 product. Is there someone with some PHP knowledge (and a some WordPress) that can help me out? Thanks in advance!
There are actually two ways you can do this, at least. You can either call the add_to_cart function more than once, or create a loop. Both ways are down here:
Method 1
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
WC()->cart->add_to_cart( $product_id2 );
WC()->cart->add_to_cart( $product_id3 );
WC()->cart->add_to_cart( $product_id4 );
}
}
}
Method 2
foreach ($articles as $article) {
WC()->cart->add_to_cart( $article );
}
Do note that you should create a new array called articles holding all the IDs from the desired products. Another thing that is going to bother you then is checking whether the cart holds more than 0 items, and checking if all of them are in there.
Method 2 would look something like this:
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$articles = array(64);
$found = false;
// check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if (($key = array_search($_product->id, $articles)) !== false)
unset($articles[$key]);
}
// if product not found, add it
if ( count($articles) > 0 ) {
foreach ($articles as $article) {
WC()->cart->add_to_cart($article);
}
}
} else {
// if no products in cart, add it
foreach ($articles as $article) {
WC()->cart->add_to_cart( $article );
}
}
}
}

woocomerce Automatically add product to cart on visit - error

I'm trying to implement the following solution:
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
$woocommerce->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
from here:
http://docs.woothemes.com/document/automatically-add-product-to-cart-on-visit/
but it throws an error:
Sorry, this product cannot be purchased.
Anyone knows what am I doing wrong?
maybe your product stock quantity is zero or product is not there. Please check that.

Categories