Based on Woocommerce change prices for a certain country , I am trying to add to the product price an extra cost that it has to be divided by the cart item count.
add_filter('woocommerce_get_price', 'return_custom_price', $product, 2);
function return_custom_price($price, $product) {
global $post, $woocommerce;
// Array containing country codes
$container = 3000;
$county = array('GR');
// Get the post id
$post_id = $post->ID;
$cart_tot = $woocommerce->cart->cart_contents_count;
// Amount to increase by
$amount = ($container / $cart_tot);
// If the customers shipping country is in the array and the post id matches
if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) && ( $post_id == '1151' || $post_id == '1152' ) ){
// Return the price plus the $amount
return $new_price = $price + $amount;
} else {
// Otherwise just return the normal price
return $price;
}
}
The problem is that i get an error and I dont know how to solve it. Warning: Division by zero
When I used echo $woocommerce->cart->cart_contents_count; it shows the cart item count but multiple times in a row.…
Any help is appreciated.
Since Woocommerce 3, the hook woocommerce_get_price
is deprecated and has been replaced. Also the code is really outdated, full of mistakes and errors.
You can't really change the product price based on cart as it will always make errors and it's not the right way to handle what you would like.
Anyways here is your revisited code, but you should not use it (see the other way after below) :
add_filter( 'woocommerce_product_get_price', 'custom_specific_product_prices', 10, 2 );
function custom_specific_product_prices( $price, $product ) {
// Exit when cart is empty
if( WC()->cart->is_empty() )
return $price; // Exit
## ----- Your settings below ----- ##
$countries = array('GR'); // Country codes
$product_ids = array('1151', '1152'); // Product Ids
$container = 3000; // Container cost
## ------------------------------- ##
if( ! in_array( $product->get_id(), $product_ids ) )
return $price; // Exit
$cart_items_count = WC()->cart->get_cart_contents_count();
$shipping_country = WC()->customer->get_shipping_country();
// If the customers shipping country is in the array and the post id matches
if ( in_array( $shipping_country, $countries ) ) {
// Return the price plus the $amount
$price += $container / $cart_items_count;
}
return $price;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
What you can do is to add a "Container" fee:
add_action( 'woocommerce_cart_calculate_fees', 'add_container_fee', 10, 1 );
function add_container_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return; // Exit
## ----- Your settings below ----- ##
$countries = array('GR'); // Country codes
$product_ids = array('1151', '1152'); // Product Ids
$container = 3000; // Container cost
## ------------------------------- ##
$shipping_country = WC()->customer->get_shipping_country();
$items_found = false;
if ( ! in_array( $shipping_country, $countries ) )
return; // Exit
foreach( $cart->get_cart() as $cart_item ) {
if ( array_intersect( array( $cart_item['variation_id'], $cart_item['product_id'] ), $product_ids ) )
$items_found = true; // Found
}
if ( $items_found )
$cart->add_fee( __('Container fee'), $container );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.