Code Snippet for Applying the Price Adjustment on the TM Extra Product Addon Options

We have made ELEX WooCommerce Catalog Mode, Wholesale & Role Based Pricing with compatible with WooCommerce Extra Product Options as mentioned on our product page. The following code snippet will help you to apply the discount under the price adjustment section on the extra options created in the products by third party TM extra product addon:

add_action( 'woocommerce_checkout_order_processed',  'elex_checkout_order_content' ,999,3 );
add_filter('wc_epo_discounted_price', 'elex_cart_option_discount',999,2);
add_filter('wc_epo_apply_discount','elex_custom_extra_option_discount',999);
function elex_cart_option_discount($price,$product) {
    $prdct_id =0;
    $temp_data = '';
    if(!empty($product) && !is_numeric($product)){
        $prdct_id     = $product->get_ID();
        $temp_data = $product->get_type();
    }elseif(is_numeric($product) && !empty($product)){
        $prdct_id = $product;
        $product_data = wc_get_product( $prdct_id  );
        $temp_data = $product_data->get_type();
    }
    $current_user_role = wp_get_current_user()->roles;
    $current_user_id = get_current_user_id();
    $rule_satisfied = false;
    $index = 0;
    $adjustment_value = 0;
    if( get_post_meta($prdct_id, 'product_based_price_adjustment', true)  == 'yes'  ){
        $adjustment_value =  apply_filters('elex_rp_tm_extra_option_individual_discount',$prdct_id,$price);
        if($adjustment_value == 'no_amount'){
            $adjustment_value = 0;
        }
        $price +=$adjustment_value;
    }
    if($adjustment_value == 0){     
        $price = apply_filters('elex_rp_tm_extra_option_global_discount',$price, $prdct_id, $temp_data, $adjustment_value);
    }
    return $price;
}
function elex_custom_extra_option_discount($option_price){
    global $_POST,$product;
    if(!empty($option_price)){
        foreach($option_price as $key => $price_value){
            $prdct_id = 0;
            $temp_data = '';
            if(!empty($product)){
                $prdct_id = $product->get_ID();
                $temp_data = $product->get_type();
            }else if(!empty($_POST) && isset($_POST['tcaddtocart'])){
                $prdct_id = $_POST['tcaddtocart'];
                $product = wc_get_product( $prdct_id )
                $temp_data = $product->get_type();
                if(isset($_POST['variation_id'])){
                    $temp_data ='variation';    
                }
            }    
            $current_user_role = wp_get_current_user()->roles;
            $current_user_id = get_current_user_id();
            $rule_satisfied = false;
            $index = 0;
            $adjustment_value = 0;
            if( get_post_meta($prdct_id, 'product_based_price_adjustment', true)  == 'yes'  ){
                $adjustment_value =  apply_filters('elex_rp_tm_extra_option_individual_discount',$prdct_id,$price_value);
                if($adjustment_value == 'no_amount'){
                    $adjustment_value = 0;
                }
                $price = $price_value + $adjustment_value;
            }
            if($adjustment_value == 0){ 
                $price = apply_filters('elex_rp_tm_extra_option_global_discount',$price_value, $prdct_id, $temp_data, $adjustment_value);
            }
            $price_option[$key]=$price;
        }
        return $price_option;
    }
}
function elex_checkout_order_content($order_id){
    $order = new WC_Order( $order_id );
    $items = $order->get_items();
    foreach($items as $item ){
        $pid = $item->get_product_id();
        $epo_data = $item->get_meta('_tmcartepo_data',true);
        foreach($epo_data as $epo_option_key => $epo_option){
            if(isset($epo_option['price_per_currency'])){
                foreach($epo_option['price_per_currency'] as $currency => $price){
                    $option_price = elex_cart_option_discount($price,$pid);
                    $epo_data[$epo_option_key]['price_per_currency'][$currency] = $option_price;        
                }
            }
        }
    $item->update_meta_data('_tmcartepo_data',$epo_data);
    $item->save_meta_data();
    }   
 }

You can simply add the above code in the activated theme’s functions.php file on your site and save the changes for implementing the code. Check the below screenshot to understand this better:

Code Snippet for Applying the Price Adjustment on the TM Extra Product Addon Options

You may need to set up a discount rule in our plugin global settings. In the below screenshot, you can see we have set up a discount rule of 50% for the administrator rule.

Product page visibility for the Administrator user role before implementing the code snippet:

Product Page after implementing the code snippet:

To know more about the product, check out the ELEX WooCommerce Catalog Mode, Wholesale & Role Based Pricing Plugin

Or check out the documentation section for more related articles.

For more creative snippets of information visit elex-snippets.

Previous Code Snippet for Applying the Role Based Pricing to all the Product Variations
Next How To Override The Role-based Price When The Sale Price Is Lower?

Leave a Reply

WordPress Image Lightbox Plugin