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 );
Related
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;
}
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;
}
for my woocommerce e-shop i use WpAllImport plugin for importing my products with their variations. Typically one product with the variations is shown below
The parent product has no price/sale price values and the price/sale price values are all the same for all variations.
Now i want to export an xml file with only my parents products with WpAllExport plugin. As there is no price/sale price values the tag <price> </price> and <sale price> </sale price>is return empty with no values.
I realise that i need php function for the export to pass any variation price/sale price to parent product price fields.
Does someone help me with the php function i have to write?
Thank you in advance
OK, finally I add the following code and it works fine.
<?php
function get_regular_price_for_xml($parent, $price)
{
if ($parent == "0") {
global $product;
$product_variations = $product->get_available_variations();
if (!empty($product_variations) && is_array($product_variations)) {
$variation_product_id = $product_variations[0]['variation_id'];
$variation_product = new WC_Product_Variation($variation_product_id);
return $variation_product->regular_price;
} else {
return $price;
}
} else {
return $price;
}
}
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
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.