Add discount to Woocommerce cart item variation value?

I can created a woocommerce action hook to store variation value named “wcic_installation_charges” to store the installation charges of the product. This is dynamic, so sometimes this value is zero and sometimes there is cost included.

What I want to do is, when we add item to cart, it should check if there is more then one product having non-zero variation value for “wcic_installation_charges” then give 10% discount to this value. Later I am using this variation value in changing the total cart calculations.

Below is the action hook code

function save_installation_charges( $cart_item_data, $product_id ) {

        if( isset( $_REQUEST['wcic_installation_charges'] ) && !empty($_REQUEST['wcic_installation_charges']) ) {

            /* This is where the installation charges is stored with cart item as variation */
            $cart_item_data[ 'wcic_installation_charges' ] = $_REQUEST['wcic_installation_charges'];

            /* below statement make sure every add to cart action as unique line item */
            $cart_item_data['unique_key'] = md5( microtime().rand() );

    return $cart_item_data;
add_action( 'woocommerce_add_cart_item_data', 'save_installation_charges', 10, 2 );

Read more here: Add discount to Woocommerce cart item variation value?

Leave a Reply

Your email address will not be published. Required fields are marked *