How to round total only if BACS method checked. Woocommerce - woocommerce

I fonud function that rounds the total, but can't understand how to make it work only if BACS payment checked on checkout page.
add_filter( 'woocommerce_calculated_total', 'custom_calculated_total' );
function custom_calculated_total( $total ) {
$total = round( $total, 1 );
return ceil($total);
}

If someone will need, I found solution:
add_filter( 'woocommerce_calculated_total', 'custom_calculated_total' );
function custom_calculated_total( $total ) {
if( WC()->session->get( 'chosen_payment_method' ) == 'bacs' )
{
$total = round( $total, 1 );
$total = ceil( $total );
}
return $total;
}
The woocommerce_calculated_total hook is documented here:

Related

WooCommerce, disable shipping based on an acf field

I added a true/false field inside the user profile through acf.
I need to hide a specific shipment if this field is selected or not.
If selected, the shipment must be hidden.
This is my code:
function filter_woocommerce_package_rates( $rates, $package ) {
$user_id = get_current_user_id();
$verified=false;
$acf_field=get_user_meta( $user_id, 'fast_shipping', true );
if($acf_field) $verified=true;
// Loop trough
foreach ( $rates as $rate_id => $rate ) {
if ( $verified ) {
unset( $rates['flat_rate:8'] );
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
But nothing is hidden.
Can you please try below code:
function filter_woocommerce_package_rates( $rates, $package ) {
$user_id = get_current_user_id();
$verified=false;
$acf_field=get_user_meta( $user_id, 'fast_shipping', true );
if(is_bool($acf_field) == 1) $verified=true;
if ( is_bool($verified) == 1 ) {
unset( $rates['flat_rate:8'] );
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
Please let me know if you find any issues.

Issue with changing WooCommerce cart item prices based on product meta

When the product subtotal is more than 3000, it has to be another price.
I added it as a meta field to products. On Stack Overflow, a long time ago someone advised me the filter for a special price called woocommerce_cart_item_name
I modified it a little for my purpose:
function kia_add_subtitle_to_cart_product( $title, $cart_item ){
$threshold = 3000; // Change price if > 3000
$_product = $cart_item['data'];
$meta = $_product->get_meta( '_opt_price_var');
$price = $_product->get_regular_price();
$subtotal = $cart_item['quantity'] * $price;
if ( $subtotal > $threshold && ( $meta )) {
$_product->set_price( $meta );
}
echo $title;
}
add_filter( 'woocommerce_cart_item_name', 'kia_add_subtitle_to_cart_product', 10, 2 );
The special price is showing only in the cart form, but not in cart totals and not in the checkout, in totals and in checkout the price is still regular, the main calculator doesn't take it from this filter.
How can I fix it? How to include it in the main calculator too?
I tried different hooks that can be responsive for the calculator - both add_action and add_filter, with and without 10, 2 - it isn't it.
add_action('woocommerce_before_shipping_calculator', 'kia_add_subtitle_to_cart_product',10,2);
add_action('woocommerce_before_cart_totals', 'kia_add_subtitle_to_cart_product', 10, 2 );
The woocommerce_cart_item_name hook is being misused in your code attempt. A filter hook should always return something compared to the use of echo.
You should use the woocommerce_before_calculate_totals hook instead
So you get:
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
// Change price if > 3000
$threshold = 3000;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
// Get regular price
$price = $cart_item['data']->get_regular_price();
// Subtotal = Get quantity * price
$subtotal = $cart_item['quantity'] * $price;
// Greater than
if ( $subtotal > $threshold ) {
// Get meta
$meta = $cart_item['data']->get_meta( '_opt_price_var', true );
// NOT empty
if ( ! empty ( $meta ) ) {
// Set new price
$cart_item['data']->set_price( $meta );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
add_filter('woocommerce_calculated_total', 'custom_calculated_total', 10, 2);
function custom_calculated_total($total, $cart_object) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if (!WC()->cart->is_empty()):
foreach ($cart_object->cart_contents as $key => $cart_item) {
$meta = $cart_item['data']->get_meta('_opt_price_var');
if ($total >= 3000 && ( $meta )) {
$total = $meta;
}
}
endif;
return $total;
}

Woocommerce cart calculate fees issue

I have create hook action
do_action('wpyoug_update_price');
add_action('wpyoug_update_price', 'wpyoug_update_woocommerce_price');
function wpyoug_update_woocommerce_price(){
global $totalshipCharge;
add_action( 'woocommerce_cart_calculate_fees', 'cp_add_custom_price' );
}
When I echo inside wpyoug_update_woocommerce_price() function it return . but add_action( 'woocommerce_cart_calculate_fees', 'cp_add_custom_price' ); is not working
function cp_add_custom_price(){
global $woocommerce;
$thiscarttotal = WC()->cart->get_cart_total();
$thiscarttotal = preg_replace('/$/', '', $thiscarttotal);
$woocommerce->cart->add_fee( __('Miscellaneous Handling Fee', 'woocommerce'), 10 );
}
// Hook before adding fees
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( WC_Cart $cart ){
$fees = 0.08;
$cart->add_fee( 'Handling fee', $fees);
}
add_action( 'woocommerce_cart_calculate_fees','xa_add_surcharge' );
function xa_add_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$surcharge = 5;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
From here

Woocommerce - custom functions runs only on update cart

I wrote a very small function to disable installation as a method (from table rate shipping plugin) if a product is not in the cart or if the quantity of that product in the cart is less than 6.
This works, but only when I click on "update cart" button and not, for example, when I click on cart.
here's the function, directly from my function.php file in my custom theme:
function disable_installation_for_less_than( $rates ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$installation = $rates['table_rate_shipping_installation'];
foreach ( $items as $item => $values ) {
$productID = $values['product_id'];
$productQuantity = $values['quantity'];
unset( $rates['table_rate_shipping_installation'] );
if ( $productID == 2412 ) {
if ( $productQuantity < 6 ) {
unset( $rates['table_rate_shipping_installation'] );
} else {
array_push($rates, $installation);
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'disable_installation_for_less_than', 10, 3 );
Any idea why? am I using the wrong hook?
Thanks for any help
Also, rather then un-setting the installation and re-set it only when needed, is there a better way to say "if this product is NOT in the cart" then remove this?
thanks
Ok, I managed to solve it this way:
function disable_installation_for_less_than( $rates ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$showInstallation= false;
foreach ( $items as $item => $values ) {
$productID = $values['product_id'];
$productQuantity = $values['quantity'];
if ( $productID == 2412 ) {
$showInstallation= true;
if ( $productQuantity < 6 ) {
unset( $rates['table_rate_shipping_installation'] );
}
}
}
if ($showInstallation== false){
unset( $rates['table_rate_shipping_installation'] );
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'disable_installation_for_less_than', 10, 2 );
It's working now.

woocommerce add custom price while add to cart

Hi I need to add an extra price to product price while add to cart.
http://url/warenkorb/?add-to-cart=1539&added_price=5.00
I have used the code following code to achive this.
add_filter( 'woocommerce_add_cart_item', 'c_other_options_add_cart_item', 20, 1 );
function c_other_options_add_cart_item( $cart_item ) {
if (isset($cart_item['_other_options'])) :
if( isset($cart_item['_other_options']['product-price']) )
$extra_cost = floatval($cart_item['_other_options']['product-price']);
$cart_item['data']->adjust_price( $extra_cost );
// here the real adjustment is going on...
endif;
return $cart_item;
}
add_filter( 'woocommerce_add_cart_item_data', c_other_options_add_cart_item_data', 10, 2 );
function c_other_options_add_cart_item_data($cart_item_meta, $product_id){
global $woocommerce;
$product = new WC_Product( $product_id);
$price = $product->price;
if(empty($cart_item_meta['_other_options']))
$cart_item_meta['_other_options'] = array();
$cart_item_meta['_other_options']['product-price'] = esc_attr($_REQUEST['price']) - $price;
return $cart_item_meta;
}
It shows the modified price on add to cart page but not in the cart/checkout page. Please Help me to achieve this. Thanks in advance.
You have to use woocommerce_before_calculate_totals hook for this purpose,
like this
function calculate_extra_fee( $cart_object ) {
/* Extra fee */
$additionalPrice = 100;
foreach ( $cart_object->cart_contents as $key => $value ) {
/* if you want to add extra fee for particular product, otherwise remove the if condition */
if( $value['product_id'] == 100) {
$quantity = intval( $value['quantity'] );
$orgPrice = intval( $value['data']->price );
$value['data']->price = ( ( $orgPrice + $additionalPrice ) * $quantity );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_extra_fee', 1, 1 );

Resources