GET a coupon code via URL and apply it in WooCommerce Checkout page [closed]

Update 3: This can be done in a very simple way with the following 2 hooked functions: The first one will catch the coupon code in the Url and will set it in WC_Sessions. The second one will apply the coupon code from session in checkout page. Here is this code: add_action(‘init’, ‘get_custom_coupon_code_to_session’); function get_custom_coupon_code_to_session(){ … Read more

Set cart item price from a hidden input field custom price in Woocommerce 3

Update 2021 – Handling custom price item in mini cart First for testing purpose we add a price in the hidden input field as you don’t give the code that calculate the price: // Add a hidden input field (With a value of 20 for testing purpose) add_action( ‘woocommerce_before_add_to_cart_button’, ‘custom_hidden_product_field’, 11 ); function custom_hidden_product_field() { … Read more

Allow specific products to be purchased only if a coupon is applied in Woocommerce

For defined products, the following code will not allow checkout if a coupon is not applied, displaying an error message: add_action( ‘woocommerce_check_cart_items’, ‘mandatory_coupon_for_specific_items’ ); function mandatory_coupon_for_specific_items() { $targeted_ids = array(37); // The targeted product ids (in this array) $coupon_code=”summer2″; // The required coupon code $coupon_applied = in_array( strtolower($coupon_code), WC()->cart->get_applied_coupons() ); // Loop through cart items … Read more

Changing WooCommerce cart item names

The woocommerce_order_item_name filter hook is a front-end hook and is located in: 1) WooCommerce templates: emails/plain/email-order-items.php templates/order/order-details-item.php templates/checkout/form-pay.php templates/emails/email-order-items.php 2)WooCommerce Core file: includes/class-wc-structured-data.php Each of them has $item_name common first argument, and different for the other arguments. See Here for more details… You have 2 arguments set in your function (the second one is not … Read more

Remove attribute values from product variation title and show them on separate rows

UPDATED 1) Since WooCommerce 3+, to remove attribute values from Product variation title and to display them in a separate row will need to use this 2 dedicated simple hooks (in checkout page). Removing attribute values from Product variation title: add_filter( ‘woocommerce_product_variation_title_include_attributes’, ‘variation_title_not_include_attributes’ ); function variation_title_not_include_attributes( $boolean ){ if ( ! is_cart() ) $boolean = … Read more

Dynamic shipping fee based on custom radio buttons in Woocommerce

You should give all necessary related code in your question. Remember that “Questions seeking debugging help (“why isn’t this code working?”) must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself“. So, in the code below, you will find a complete working solution, … Read more

Disable add to cart button for an array of products IDs in WooCommerce

Updated for WooCommerce 3+ Use in_array() instead like: add_filter( ‘woocommerce_variation_is_purchasable’, ‘filter_is_purchasable’, 10, 2 ); add_filter(‘woocommerce_is_purchasable’, ‘filter_is_purchasable’, 10, 2); function filter_is_purchasable($is_purchasable, $product ) { if( in_array( $product->get_id(), not_purchasable_ids() ) { return false; } return is_purchasable; } Where not_purchasable_ids() is the function that returns an array of non purchasable products Ids (here simplified): function not_purchasable_ids() { return … Read more

Hide shipping methods for specific shipping class in WooCommerce

Update 2019: You should try instead this shorter, compact and effective way: add_filter( ‘woocommerce_package_rates’, ‘hide_shipping_method_based_on_shipping_class’, 10, 2 ); function hide_shipping_method_based_on_shipping_class( $rates, $package ) { if ( is_admin() && ! defined( ‘DOING_AJAX’ ) ) return; // HERE define your shipping class to find $class = 92; // HERE define the shipping method to hide $method_key_id = … Read more

Get in WooCommerce cart the product ID of a cart item

To get the product ID of each cart item in the foreach loop (for a simple product): foreach( WC()->cart->get_cart() as $cart_item ){ $product_id = $cart_item[‘product_id’]; } If it’s a variable product, to get the variation ID: foreach( WC()->cart->get_cart() as $cart_item ){ $variation_id = $cart_item[‘variation_id’]; } Or for both cases (where $cart_item[‘data’] is the WC_Product Object … Read more