Ecommerce Shopify WordPress Discussion

Adding costs to woocommerce products at checkout

I have the code below, I have it setup so each product has the following fields: shipping_national_free_shipping - checkbox shipping_national_one_item_cost - number field shipping_national_additional_items_cost - number field If shipping_national_free_shipping is disabled, the code below works out the shipping cost and adds it to the product. That works. However if shipping_national_free_shipping is enabled and there are still values in the other two fields it still adds the shipping cost when it should ignore those fields when shipping_national_free_shipping is enabled. function kraftdeck_national_shipping_cost_logic($cart) { if (is_admin() && !defined('DOING_AJAX')) { return; } error_log('Starting national shipping cost logic...'); $customer_shipping_country_code = WC()->customer->get_shipping_country(); $customer_shipping_country = kraftdeck_get_full_country_name($customer_shipping_country_code); foreach ($cart->get_cart() as $cart_item_key => $cart_item) { $product_id = $cart_item['product_id']; $national_free_shipping = get_field('shipping_national_free_shipping', $product_id); // Check if national free shipping is enabled ('enabled' in array) if (!empty($national_free_shipping) && in_array('enabled', (array)$national_free_shipping)) { continue; } $country_of_origin = get_field('shipping_country_of_origin', $product_id); if ($customer_shipping_country === $country_of_origin) { $shipping_national_one_item_cost = get_field('shipping_national_one_item_cost', $product_id); $shipping_national_additional_items_cost = get_field('shipping_national_additional_items_cost', $product_id); $quantity = $cart_item['quantity']; $additional_shipping_cost = 0; if (is_numeric($shipping_national_one_item_cost)) { if (empty($shipping_national_additional_items_cost)) { $additional_shipping_cost = $shipping_national_one_item_cost * $quantity; } else { $additional_shipping_cost = $shipping_national_one_item_cost + $shipping_national_additional_items_cost * ($quantity - 1); } } if ($additional_shipping_cost > 0) { $base_price = $cart_item['data']->get_price(); $new_price = ($base_price * $quantity) + $additional_shipping_cost; $cart_item['data']->set_price($new_price / $quantity); } } error_log("Final Additional Shipping Cost for Product $product_id: $additional_shipping_cost"); error_log("Final Price for Product $product_id: " . $cart_item['data']->get_price()); } } add_action('woocommerce_before_calculate_totals', 'kraftdeck_national_shipping_cost_logic'); function kraftdeck_get_full_country_name($country_code) { $countries = WC()->countries->get_countries(); $full_name = isset($countries[$country_code]) ? $countries[$country_code] : ''; // Remove any parentheses and content within them $full_name_cleaned = preg_replace('/\s+(.+)$/', '', $full_name); return $full_name_cleaned; } remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10); I've tried everything to get this working, even ChatGPT cannot help!
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.