In WooCommerce, Is it possible to display specific country only if cart total is over 100? - wordpress

I have tried to do it using a filter hook called "woocommerce_countries". I tried getting the cart total in this hook but it's not working. Is anyone have any idea on this OR suggest any hook?

Please try with this code. WooCommerce 3.6.2, they have removed the ability to check the cart data. But cart session is accessible right now so we use this function WC()->session->cart
add_filter( 'woocommerce_countries', 'products_disable_country', 10, 1 );
function products_disable_country( $countries ) {
$cartdata = WC()->session->cart;
if (is_array($cartdata) && !empty($cartdata)) {
foreach ($cartdata as $itemdata) {
$sum += $itemdata['line_total'];
}
if ($sum >= 100) {
unset($countries["CA"]);
}
}
return $countries;
}

Related

Solution to changing currency for Woocommerce products

Hi so I would like to be able to change currency for different products added to the cart/checkout.
I found solution that is filtering woocommerce_currency hook and using cart session to change currency.
Two questions:
Is there a better way of doing it?
Do you see any potential problems with the following solution?
public function modifyWoocommerceCurrency( $currency ) {
$cart_data = WC()->session->get('cart');
foreach ($cart_data as $item) {
if (isset($item["_campaign_options"]["user_paying_currency"])) {
$currency = $item['_campaign_options']['user_paying_currency'];
}
}
return $currency;
}
add_filter( 'woocommerce_currency', [$this,'modifyWoocommerceCurrency'], 10, 1 );

Getting product stock quantity isn't working

On single product page I'm trying to get stock quantity or atleast stock status from woocommerce product but it seems not working properly, even if it's simple product (manage stock status is checked) or variable product the button is not showing. And strange thing happens - I've got Yith Product Bundle plugin and even if every item is in stock it shows that button but whatever, that's not main problem.
So here is code I'm using
add_action('woocommerce_before_add_to_cart_button', 'show_wishlist');
function show_wishlist() {
global $product;
$stockQ=$product->get_stock_quantity();
if ( $stockQ < 1 ) {
echo "<button>Hello</button>";
}
}
I've also tried but completely nothing happens.
add_action('woocommerce_before_add_to_cart_button', 'show_wishlist');
function show_wishlist() {
global $product;
if ( ! $product->is_in_stock() ) {
echo "<button>Hello</button>";
}
}
First check if the hook is firing properly.
add_action( 'woocommerce_before_add_to_cart_button', 'show_wishlist' );
function show_wishlist(){
echo '<p>Some custom text before</p>';
}
If you are able to confirm that the hook is working then use the below code.
function show_wishlist() {
global $product;
// Change In Stock Text
if ( $product->managing_stock() && $product->is_in_stock() ) {
//add your code here for Products in Stock
}
else{
//add your code here for Products out of Stock
}
}

Update Specific product subtotal in woo-commerce cart

foreach( $cart as $cart_item_id => $citem ) {
if ( $citem['product_id'] == $gift ) {
$item_id = $cart_item_id;
$grp = $citem['data']->get_regular_price();
//$woocommerce->cart->cart_contents[$cart_item_id]['line_subtotal'] = 1;
$woocommerce->cart->line_subtotal($cart_item_id, '0');
$woocommerce->cart->cart_contents[$cart_item_id]['line_subtotal'] = 1;
//WC()->cart->set_session();
//$woocommerce->cart->set_session();
}
}
I can get access to cart details in "woocommerce_before_calculate_totals" hook.
But I can not manage to change the line_subtotal of a specific product.
Is there any way to do it using "woocommerce_before_calculate_totals" hook?
Thanks in advance.
There is a way doing this using woocommerce_before_calculate_totals, but as you do that it would also affect the individual price of the product.
So if you choose to do this, you would have to reset the individual price of the product by then filtering woocommerce_cart_item_price.
But this is kind of wierd so I would try using woocommerce_cart_item_subtotal, if it works in your usecase.
add_filter('woocommerce_cart_item_subtotal','new_cart_item_subtotal_filter', 10, 3);
function new_cart_item_subtotal_filter( $oldTotal, $cart_item, $cart_item_key){
//filter...
return $oldTotal;
}

Woocommerce display product variations

Im having a issue with function:
I need to display product variations (not parent) on category page. -
Let's say:
If is category "t-shirts" I need to display all product variations with attribute "t-shirts".
Im using this function but it's not working:
add_action( 'woocommerce_product_query', 'rc_modify_query_get_design_projects' );
function rc_modify_query_get_design_projects( $product ) {
if (is_tax('pa_tshirts') || is_tax('pa_shirts') || is_product_category('summer')) {
$product->set('post_type', 'product_variation');
}
}
Hope anyone can help.
Thanks

Hide Shipping Options Woocommerce

So I'm trying to hide certain ship methods in Woocommerce based on a product tag. The main problem I face is my own lack PHP knowledge so I frankensteined the following code together with the help of some very friendly folks:
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the tag you set. Switch to true if the tag is found.
foreach ($cart as $array_item) {
if (isset($array_item['product_tag']) && $array_item['product_tag'] == "CHOSEN_TAG") { // Replace "CHOSEN_TAG" with what ever tag you want
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_tag( $available_methods ) {
// use the function abve to check the cart for the tag.
if ( check_cart_for_share() ) {
// remove the rate you want
unset( $available_methods['flat_rate'] ); // Replace "flar_rate" with the shipping option that yu want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
I understand that this code is by no means universal and thus the variables will be different from case to case but perhaps someone can tell me if something looks off in the code. Much appreciated
No doubt you have sorted this by now but your code was a good start for me ... and since I sorted it I have published it below. Your problem was that woocommerce doesn't have the product_tag in the cart array so you have to go and get it.
/* !Hide Shipping Options Woocommerce */
add_filter( 'woocommerce_available_shipping_methods', 'hide_shipping_based_on_tag' , 10, 1 );
function check_cart_for_share() {
// load the contents of the cart into an array.
global $woocommerce;
$cart = $woocommerce->cart->cart_contents;
$found = false;
// loop through the array looking for the tag you set. Switch to true if the tag is found.
foreach ($cart as $array_item) {
$term_list = wp_get_post_terms( $array_item['product_id'], 'product_tag', array( "fields" => "names" ) );
if (in_array("Heavy",$term_list)) { // Replace "Heavy" with what ever tag you want
$found = true;
break;
}
}
return $found;
}
function hide_shipping_based_on_tag( $available_methods ) {
// use the function above to check the cart for the tag.
if ( check_cart_for_share() ) {
// remove the rate you want
unset( $available_methods['flat_rate'] ); // Replace "flat_rate" with the shipping option that you want to remove.
}
// return the available methods without the one you unset.
return $available_methods;
}
You can use shipping class of Woocommerce to achieve this.
Here is the step by step instructions.
Create a shipping class (woocommerce->settings->shipping->shipping classes).
Associate this shipping class with products (that you wand to hide shipping methods for)
Use this snippet. (copy and pate to function.php).
For more information about the snippet is available here.

Resources