To remove a custom field from checkout in WooCommerce when a specific product is in the cart, you can use the woocommerce_checkout_fields function and remove the field based on the product. Here is a sample code that can help you: add_filter( 'woocommerce_checkout_fields', 'remove_custom_field_based_on_product' ); function remove_custom_field_based_on_product( $fields ) { // Insert the product ID here that the […]
Category: WooCommerce
Hide certain payment methods in the checkout for certain products
In this example, you need to customize the hide_payment_method_for_specific_products function by changing the product IDs and the names of the payment methods you want to hide according to your requirements. Add this function to your theme's functions.php and it will hide the selected payment methods based on the product IDs. Please note that this is just an example and you […]
Activate free shipping for regular customers
add_filter('woocommerce_shipping_free_shipping_threshold', 'free_shipping_for_returning_customers'); function free_shipping_for_returning_customers($threshold) { if (is_user_logged_in() && current_user_can('customer_group')) { $threshold = 0; // Free shipping for regular customers } return $threshold; }
Automatically empty the shopping cart when a product is added
add_action('woocommerce_add_to_cart', 'empty_cart_on_add'); function empty_cart_on_add() { WC()->cart->empty_cart(); }
Create custom tabs on the product page
add_filter('woocommerce_product_tabs', 'custom_product_tabs'); function custom_product_tabs($tabs) { $tabs['custom_tab'] = array( 'title' => __('Additional Information', 'woocommerce'), 'priority' => 50, 'callback' => 'custom_product_tab_content' ); return $tabs; } function custom_product_tab_content() { echo '<h2>Additional Information</h2>'; echo '<p>Here you can find more information about this product.</p>'; }
Remove voucher code field from the shopping cart
add_action('init', 'remove_coupon_field_from_cart'); function remove_coupon_field_from_cart() { remove_action('woocommerce_before_cart', 'woocommerce_coupon_form', 10); }
Shipping costs vary depending on the number of items
add_filter('woocommerce_package_rates', 'adjust_shipping_based_on_item_count', 10, 2); function adjust_shipping_based_on_item_count($rates, $package) { $item_count = WC()->cart->get_cart_contents_count(); if ($item_count > 5) { foreach ($rates as $rate_id => $rate) { $rates[$rate_id]->cost += 2.00; // Additional shipping costs for 6 items or more } } return $rates; }
Discount for products in the “Sale” category
add_filter('woocommerce_product_get_price', 'discount_sale_category_products', 10, 2); function discount_sale_category_products($price, $product) { $sale_category = 'sale'; // Name of the sale category if (has_term($sale_category, 'product_cat', $product->get_id())) { $price = $price * 0.8; // 20% discount for products in the sale category } return $price; }
Custom message for sold out products
add_filter('woocommerce_get_availability', 'custom_out_of_stock_message', 1, 2); function custom_out_of_stock_message($availability, $product) { if (!$product->is_in_stock()) { $availability['availability'] = __('Unfortunately sold out - Please contact us for more information.', 'woocommerce'); } return $availability; }
Add custom text field to checkout form
add_action('woocommerce_after_checkout_billing_form', 'custom_checkout_form_field'); function custom_checkout_form_field($checkout) { echo '<div id="”custom_checkout_field”"><h2>' . __('Additional Information') . '</h2>'; woocommerce_form_field('custom_field', array( 'type' => 'text', 'class' => array('form-row-wide'), 'label' => __('Custom Field'), 'placeholder' => __('Enter your information here.'), 'required' => false, ), $checkout->get_value('custom_field')); echo '</div>'; }