Add additional shipping fee based on country

add_filter('woocommerce_cart_calculate_fees', 'add_additional_shipping_fee'); function add_additional_shipping_fee() { $shipping_country = WC()->customer->get_shipping_country(); if ($shipping_country == 'US') { WC()->cart->add_fee('Additional shipping fee', 10.00); } }  

Read More

Add Custom Field for Products

add_action('woocommerce_product_options_general_product_data', 'add_custom_product_field'); function add_custom_product_field() { woocommerce_wp_text_input(array( 'id' => 'custom_field', 'label' => __('Custom Field', 'woocommerce'), 'placeholder' => __('Enter information here.'), 'desc_tip' => 'true', )); } add_action('woocommerce_process_product_meta', 'save_custom_product_field'); function save_custom_product_field($post_id) { $custom_field = $_POST['custom_field']; if (!empty($custom_field)) { update_post_meta($post_id, 'custom_field', esc_attr($custom_field)); } }  

Read More

Hide shipping options based on shopping cart contents

add_filter(‘woocommerce_package_rates’, ‘hide_shipping_based_on_cart_content’, 10, 2); function hide_shipping_based_on_cart_content($rates, $package) { $cart_contains_digital_products = false; foreach ($package[‘contents’] as $item) { $product = $item[‘data’]; if ($product->is_downloadable()) { $cart_contains_digital_products = true; break; } } if ($cart_contains_digital_products) { foreach ($rates as $rate_id => $rate) { if (‘flat_rate’ === $rate->method_id) { unset($rates[$rate_id]); // Flat Rate Versand entfernen } } } return $rates; } […]

Read More

Add additional fee based on the number of products

add_action('woocommerce_cart_calculate_fees', 'add_fee_based_on_product_count'); function add_fee_based_on_product_count() { $product_count = WC()->cart->get_cart_contents_count(); if ($product_count >= 5) { WC()->cart->add_fee('Additional fee', 5.00); } }  

Read More

Hide products from certain categories on the shop page

add_action('pre_get_posts', 'exclude_products_from_categories'); function exclude_products_from_categories($query) { if (is_shop() && $query->is_main_query()) { $excluded_categories = array(7, 8, 9); // Categories to exclude $query->set('tax_query', array( array( 'taxonomy' => 'product_cat', 'field' => 'id', 'terms' => $excluded_categories, 'operator' => 'NOT IN', ), )); } }  

Read More

Change product price based on user location

add_filter(‘woocommerce_product_get_price’, ‘adjust_price_by_user_location’, 10, 2); function adjust_price_by_user_location($price, $product) { $user_country = WC()->customer->get_billing_country(); if ($user_country == ‘US’) { $price = $price * 1.1; // 10% Aufschlag für US-Kunden } return $price; }  

Read More

Apply coupon code based on product category

add_action('woocommerce_before_cart', 'apply_coupon_based_on_category'); function apply_coupon_based_on_category() { $coupon_code = 'CATEGORYCODE'; // Coupon code for specific category $target_category = 'sale'; // Category name $cart = WC()->cart; $coupon_applied = false; foreach ($cart->get_cart() as $cart_item) { $product = $cart_item['data']; if (has_term($target_category, 'product_cat', $product->get_id())) { $coupon_applied = true; break; } } if (!$coupon_applied) { $cart->apply_coupon($coupon_code); } }  

Read More

Change shipping costs based on the weight of the products

add_filter('woocommerce_package_rates', 'adjust_shipping_costs_by_weight', 10, 2); function adjust_shipping_costs_by_weight($rates, $package) { $total_weight = WC()->cart->get_cart_contents_weight(); if ($total_weight > 10) { foreach ($rates as $rate_id => $rate) { $rates[$rate_id]->cost += 7.00; // Additional shipping costs for heavy packages } } return $rates; }  

Read More

Hide products in the “Sale” category

add_action('pre_get_posts', 'exclude_sale_category_products'); function exclude_sale_category_products($query) { if (is_shop() && $query->is_main_query()) { $query->set('tax_query', array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => 'sale', 'operator' => 'NOT IN', ), )); } }  

Read More

Free shipping for orders over a certain amount

add_filter('woocommerce_shipping_free_shipping_is_available', 'free_shipping_above_threshold', 10, 2); function free_shipping_above_threshold($is_available, $package) { $free_shipping_threshold = 75; // Amount from which shipping is free $cart_total = WC()->cart->get_cart_contents_total(); if ($cart_total >= $free_shipping_threshold) { $is_available = true; } return $is_available; }  

Read More