<?php
require_once  plugin_dir_path( __FILE__ ) . 'vendor/phpqrcode/qrlib.php';

class Wt_Bulk_Products_Order_Form {

    public function __construct() {
        $this->init();
    }

    public function init() {
        add_shortcode('Order-Form', array($this, 'orderFormDisplay'));        
        add_action('wp_ajax_bulkorderform_ajax_submit', array($this, 'bulkOrderFormSubmit'));
        add_action('wp_ajax_nopriv_bulkorderform_ajax_submit', array($this, 'bulkOrderFormSubmit'));

        add_action('wp_ajax_bulkorderformconfirmation_ajax_submit', array($this, 'bulkOrderConfirmationFormSubmit'));
        add_action('wp_ajax_nopriv_bulkorderformconfirmation_ajax_submit', array($this, 'bulkOrderConfirmationFormSubmit'));

        add_action('add_meta_boxes', array($this, 'bulkOrderMetaData'));

        add_action( 'save_post', array($this, 'bulkProductsOrdersMetaBoxSave'), 10, 2);

        add_action( 'transition_post_status', array($this, 'sendOrderStatusMail'), 10, 3 );
    }
    public function bulkProductsOrdersMetaBoxSave($post_id, $post){
        $slug = "bulk_order";
        if($slug != $post->post_type)
            return $post_id;
        if(isset($_POST["order_taken_by"])){
            update_post_meta($post_id, 'order_taken_by', $_POST["order_taken_by"]);
        }
        if(isset($_POST["order_printed_by"])){
            update_post_meta($post_id, 'order_printed_by', $_POST["order_printed_by"]);
        }
        if(isset($_POST["order_finished_by"])){
            update_post_meta($post_id, 'order_finished_by', $_POST["order_finished_by"]);
        }
        if(isset($_POST["order_tracking_number"])){
            update_post_meta($post_id, 'order_tracking_number', $_POST["order_tracking_number"]);
        }
    }

    public function sendOrderStatusMail($new_status, $old_status, $post){
        $orderid= $post->ID;
        $user_id = get_post_meta($orderid, 'order_user_id', true);
        $mailbody = 'Your order #'.$orderid.' has been changed. ';

        if($new_status!=$old_status){

            if($new_status == 'ready'){
                $mailbody .= 'Your order is in <b>Ready</b> status.';
            }elseif($new_status == 'process'){
                $mailbody .= 'Your order is in the <b>Process</b> status.';
            }else{
                $mailbody .= 'Your order is in <b>Delivery</b> status.';
            }
            
            $adminemail  = pods_field_display('bulk_products_settings', false, 'bulk_order_admin_email');
            $userdata = get_userdata($user_id);
            $to  = $userdata->user_email;
            $subject = 'Order status changed - Order #'. $orderid;
            $headers = 'From: dinesh-test@web-trendz.com'."\r\n" .
                'Reply-To: ' . $adminemail . "\r\n".
                'Content-Type: text/html'."\r\n";
            $userBody = '<p>Dear '.$userdata->first_name.' '.$userdata->last_name.',</p>';
            $userBody .= '<p>'.$mailbody.'</p>';
            $userBody .= '<p>Thanks & Regards,</p>';
            $userBody .= '<p>Superidcards</p>';
            wp_mail($to, $subject, $userBody, $headers);
            
        }

    }

    public function bulkOrderMetaData(){
        add_meta_box('bulk-order-data-id', 'Order Details', array($this,'orderDetailsMetabox'),'bulk_order');

    }

    function orderDetailsMetabox($post){
        $address = '';
        $userid = get_post_meta($post->ID, 'order_user_id', true);
        $user_info = get_userdata($userid);
        $shippingaddress = get_user_meta( $userid, 'shipping_address_1', true );
        $address .= get_user_meta( $userid, 'first_name', true ).' '.get_user_meta( $userid, 'last_name', true ).'<br/>';
        if($shippingaddress){
            $address .= get_user_meta( $userid, 'shipping_address_1', true ).',<br/>';
            $address .= get_user_meta( $userid, 'shipping_city', true ).',<br/>';
            $address .= get_user_meta( $userid, 'shipping_state', true ).',<br/>';
            $address .= get_user_meta( $userid, 'shipping_postcode', true ).',<br/>';
        }else{
            $address .= get_user_meta( $userid, 'billing_address_1', true ).',<br/>';
            $address .= get_user_meta( $userid, 'billing_city', true ).',<br/>';
            $address .= get_user_meta( $userid, 'billing_state', true ).'<br/>';
            $address .= get_user_meta( $userid, 'billing_postcode', true ).',<br/>';
        }
        $address .= 'Ph.'.get_user_meta( $userid, 'billing_phone', true ).'<br/>';
        //$address .= $user_info->user_email.'<br/>';
        echo '<div class=""><button id="print_btn" class="button button-primary button-large" style="float: right;margin-bottom: 20px;margin-top: 15px;"><span style="padding-top: 5px;padding-right: 5px;" class="dashicons dashicons-printer"></span>Print</button></div>';
        $imageurl = plugin_dir_url( __FILE__ )."qrcode/$post->ID.png";
        ?>
        <div id="print_order" style="display:none">
        <style>
            .widefat.fixed p{margin:0}
            
            table.print_table,table.widefat.fixed {border-collapse: collapse;}
            table.print_table,table.print_table td,table.print_table th {border: 1px solid;}
            table.print_table td{height:26px;font-family: sans-serif;font-size: 14px;padding: 5px 5px 5px 8px;font-weight: bold;}
            .print_table table.widefat.fixed td:first-child{width: 150px;}
            table.widefat.fixed{width: 100%;}
         

        </style>
        <table class="print_table">
        
            <tbody>
                <tr>
                    <td style="border:0;padding:0px 10px;vertical-align: baseline;padding-top: 8px;">
                      <?php echo get_post_meta($post->ID, 'order_data', true); ?>
                        <table style="width:100%;border-collapse: collapse;">
                            <tr>
                                <td style="width:150px"><b>User Message:</b></td>
                                <td><b><?php echo get_post_meta($post->ID, 'user_message', true); ?></b></td>
                            </tr>
                        </table>
                        <table style="width:100%;border-collapse: collapse;">
                            <tr>
                                <td style="text-align:right">
                                <?php echo "<img width='92px' src='".$imageurl."'>"; ?></td>
                            </tr>
                        </table>                        
                    </td>
                    <td style="border:0">
                        <table style="height:100%">
                            <tbody>
                                <tr>
                                    <td><?php echo $userid.'<br>'. $address; ?></td>
                                </tr>
                                <tr><td style="border:0px;height:10px"></td></tr>
                                <tr>
                                    <td><?php echo $userid.'<br>'. $address; ?></td>
                                </tr>
                                <tr><td style="border:0px;height:10px"></td></tr>
                                <tr>
                                    <td style="border:0;padding:0">
                                        <table style="border-spacing: 0;height:100%">
                                            <tr>
                                                <td><span style="display:block">Date:</span><?php echo get_post_meta($post->ID, 'ordered_date', true); ?></td>
                                                <td><span style="display:block">Order no:</span><?php echo $post->ID; ?></td>
                                            </tr>
                                            <tr>
                                                <td>Shipping:</td>
                                                <td><?php echo get_post_meta($post->ID, 'shipping_info', true); ?></td>
                                            </tr>
                                            <tr>
                                                <td>Sub Total:</td>
                                                <td><?php echo get_post_meta($post->ID, 'order_total', true); ?></td>
                                            </tr>                                            
                                            <tr>
                                                <td>Shipping Cost:</td>
                                                <td><?php echo get_post_meta($post->ID, 'shipping_cost', true); ?></td>
                                            </tr>
                                            <?php if(get_post_meta($post->ID, 'order_adjustment_cost', true)): ?>
                                            <tr>
                                                <td>Adjustment Cost:</td>
                                                <td><?php echo get_post_meta($post->ID, 'order_adjustment_cost', true); ?></td>
                                            </tr>
                                            <?php endif; ?>
                                            <tr>
                                                <td>Total:</td>
                                                <td>
                                                    <?php 
                                                    $total_order = get_post_meta($post->ID, 'shipping_cost', true)+get_post_meta($post->ID, 'order_total', true);
                                                    echo $total_order; ?>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>Order Taken By:</td>
                                                <td><?php echo get_post_meta( $post->ID, 'order_taken_by', true );; ?></td>
                                            </tr>
                                            <tr>
                                                <td>Order Finished By:</td>
                                                <td><?php echo get_post_meta($post->ID, 'order_finished_by', true); ?></td>
                                            </tr>
                                        </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
            </tbody>
        </table>
        
        </div>
        <?php
        $text = $post->ID;
        if(!file_exists(plugin_dir_path( __FILE__ )."qrcode/$post->ID.png")):
            //file path
            $file = plugin_dir_path( __FILE__ )."qrcode/$post->ID.png";
            //other parameters
            $ecc = 'H';
            $pixel_size = 4;
            $frame_size = 1;        
            // Generates QR Code and Save as PNG
            QRcode::png($text, $file, $ecc, $pixel_size, $frame_size);
            $imageurl = plugin_dir_url( __FILE__ )."qrcode/$post->ID.png";
        endif;
        ?>
        <?php
        echo '<div id="print_orders">';
        echo get_post_meta($post->ID, 'order_data', true);
        
        echo '<table class="widefat fixed" style="margin-top: -1px;position: relative;border-top: 0;">';
        echo '<tr><td><b>User Message:</b></td><td><b>'.get_post_meta($post->ID, 'user_message', true).'</b></td></tr>';
        echo '<tr><td><b>Date:</b></td><td><b>'.get_post_meta($post->ID, 'ordered_date', true).'</b></td></tr>';
        echo '<tr><td><b>Shipping:</b></td><td><b>'.get_post_meta($post->ID, 'shipping_info', true).'</b></td></tr>';
        echo '<tr><td><b>Sub Total:</b></td><td><b>Rs. '.get_post_meta($post->ID, 'order_total', true).'</b></td></tr>';
        if(get_post_meta($post->ID, 'order_adjustment_cost', true)):
            echo '<tr><td><b>Adjustment Cost:</b></td><td><b>Rs. '.get_post_meta($post->ID, 'order_adjustment_cost', true).'</b></td></tr>';
        endif;
        echo '<tr><td><b>Shipping Cost:</b></td><td><b>Rs. '.get_post_meta($post->ID, 'shipping_cost', true).'</b></td></tr>';
        if (is_numeric(get_post_meta($post->ID, 'order_total', true)) && is_numeric(get_post_meta($post->ID, 'shipping_cost', true))) {
            $total_order = get_post_meta($post->ID, 'shipping_cost', true)+get_post_meta($post->ID, 'order_total', true);
            echo '<tr><td><b>Total:</b></td><td><b>Rs. '.$total_order.'</b></td></tr>';
        }
        echo '<tr><td><b>User ID:</b></td><td><b>'.$userid.'</b></td></tr>';
        echo '<tr><td><b>Address:</b></td><td><b>'.$address.'</b></td></tr>';
        echo '</table>';
        ?>
        <div class="form_field" style="display: flex;align-items: center;margin-bottom: 25px;margin-top:20px">
        <?php $user_meta = get_userdata(get_current_user_id());
        $user_roles = $user_meta->roles;
        $otherstaffs = [];
        //print_R($user_roles);
        if ( in_array( 'supercards-staffs', $user_roles, true ) ) {
            $first_name = get_user_meta( get_current_user_id(), 'first_name', true );
            $last_name = get_user_meta( get_current_user_id(), 'last_name', true );
            $staffname = $first_name . ' '. $last_name;
        }
        if ( in_array( 'website_developer', $user_roles, true ) || in_array( 'administrator', $user_roles, true )) {
            $first_name = get_user_meta( get_current_user_id(), 'first_name', true );
            $last_name = get_user_meta( get_current_user_id(), 'last_name', true );
            $staffname = $first_name . ' '. $last_name;
            $args = array(
                'role'    => 'supercards-staffs',
                'orderby' => 'user_nicename',
                'order'   => 'ASC'
            );
            $users = get_users( $args );
            foreach ($users as $user){
                $otherstaffs[] = get_user_meta( $user->ID, 'first_name', true ) .' ' . get_user_meta( $user->ID, 'last_name', true );
            }
        }
         ?>
            <div style="width: 33%;margin-right:1%">
                <label><b>Order Taken By:</b></label>
                <?php if(get_post_meta($post->ID, 'order_taken_by', true)) :?>
                    <input type="text" value="<?php echo get_post_meta($post->ID, 'order_taken_by', true); ?>" readonly  style="width: 100%"/>
                <?php else: ?>
                <select name="order_taken_by" style="width: 100%">
                    <option></option>
                    <option value="<?php echo $staffname; ?>"><?php echo $staffname; ?></option>
                    <?php if(count($otherstaffs)): ?>
                    <?php foreach($otherstaffs as $staff){
                        echo '<option value="'.$staff.'">'.$staff.'</option>';
                    } ?>
                    <?php endif; ?>
                </select>
                <?php endif; ?>
            </div> 
             <!--<div style="width: 33%;margin-right:1%">
                <label><b>Printed By:</b></label>
                <?php //if(get_post_meta($post->ID, 'order_printed_by', true)) :?>
                    <input type="text" value="<?php //echo get_post_meta($post->ID, 'order_printed_by', true); ?>" readonly  style="width: 100%"/>
                <?php //else: ?>
                <select name="order_printed_by" style="width: 100%">
                    <option></option>
                    <option value="<?php //echo $staffname; ?>"><?php //echo $staffname; ?></option>
                    <?php //if(count($otherstaffs)): ?>
                    <?php //foreach($otherstaffs as $staff){
                        //echo '<option value="'.$staff.'">'.$staff.'</option>';
                    //} ?>
                    <?php //endif; ?>
                </select>
                <?php //endif; ?>
            </div>
            <div style="width: 33%;">
                <label><b>Finished By:</b></label>
                <?php //if(get_post_meta($post->ID, 'order_finished_by', true)) :?>
                    <input type="text" value="<?php //echo get_post_meta($post->ID, 'order_finished_by', true); ?>" readonly  style="width: 100%"/>
                <?php //else: ?>
                <select name="order_finished_by" style="width: 100%">
                    <option></option>
                    <option value="<?php //echo $staffname; ?>"><?php //echo $staffname; ?></option>
                    <?php //if(count($otherstaffs)): ?>
                    <?php //foreach($otherstaffs as $staff){
                        //echo '<option value="'.$staff.'">'.$staff.'</option>';
                    //} ?>
                    <?php //endif; ?>
                </select>
                <?php //endif; ?>
            </div>
            <div style="width: 33%;margin-left:1%">
                <label><b>Work Shop Production Staff Name:</b></label>               
                    <input type="text" value="<?php //echo get_post_meta($post->ID, 'work_shop_production_staff_name', true); ?>" readonly  style="width: 100%"/>
            </div>-->
        </div>
        <!-- <div class="">
            <label><b>Tracking Number:</b></label>
            <input type="text" name="order_tracking_number" value="<?php // echo get_post_meta($post->ID, 'order_tracking_number', true); ?>" style="width: 100%"/>
        </div> -->
        </div>
        <?php
    }


    public function orderFormDisplay(){
        $user = wp_get_current_user();
        $allowed_roles = ['website_developer','dealer','administrator','supercards-staffs'];
        $role_exist = !empty(array_intersect($allowed_roles, $user->roles));
        if($user->exists() && $role_exist):
            ob_start();
            include_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/order-form.php';
            return ob_get_clean();
        elseif($user->exists() && in_array( 'customer', $user->roles, true )):
            return '<h4>Welcome to super cards family. Contact to <a href="tel:8903038811">8903038811</a>, <a href="tel:8903038822">8903038822</a>, <a href="tel:8903038877">8903038877</a>, <a href="tel:8903038844">8903038844</a> </h4>';
        else:
            return '<h4>You need to login to view the Order Form <a style="margin-top: 20px;margin-left: 20px" class="button" href="'.get_permalink( get_option('woocommerce_myaccount_page_id') ).'">Login</a></h4>';
        endif;
    }
    public function bulkOrderConfirmationFormSubmit(){
        update_post_meta( $_POST['orderid'], 'order_confirmation', 1);
        $order_total = get_post_meta($_POST['orderid'], 'order_total', true );
        update_post_meta( $_POST['orderid'], 'order_total', $order_total + $_POST['adjustmentcost']);  
        wp_update_post(array(
            'ID'    =>  $_POST['orderid'],
            'post_status'   =>  'new-order'
        ));
        update_post_meta( $_POST['orderid'], 'order_adjustment_cost', $_POST['adjustmentcost']);
        echo json_encode(array('status' => true, 'message' => 'Your order has been placed successfully.', 'error' => false));
        die();
    }

    public function bulkOrderFormSubmit(){
        $thickness_price_size_arr =[];
        $product_price = $total_product_price = $product_weight = $product_extra_price = $total_product_extra_price = 0;
        $product_data = $full_product_data = [];
        foreach ($_POST as $key => $value) {
            if(count($_POST[$key])>1){
                if(is_array($_POST[$key])){
                    if(!empty($_POST[$key] && $key!='name_on_rope')){
                        $full_product_data[$key] = $value;
                    }
                    if(!empty($_POST[$key]) && $key!='lanyards' && $key!='tie' && $key!='belt' && $key!='pvc-fitting' && $key!='sticker-fittings'  && $key!='sticker-card-holder' && $key!='pvc-holders' && $key!='multicolor-lanyards' && $key!='rope-fittings-only' && $key!='satin-lanyard-material'){
                        //print_r(ucfirst($key));
                        $product_data[$key] = $value;
                    }
                    
                }
            }
        }
        $reduced_product_arr = array_slice( $product_data, 0, -1 );
        /*foreach ($reduced_product_arr as $key => $value) {
            $product_price_data = get_post_meta($reduced_product_arr[$key][1], 'product_price',true);
            //print_r($_POST['product_quantity_count']);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $product_price = $price['price'];
                        break;
                    }
                }
                if(!$product_price){
                    $product_price = end($product_price_data)['price'];
                }
                
            }else{
                $product_price = $product_price_data[0]['price'];
            }
        
            $total_product_price += $_POST['product_quantity_count'] * $product_price;
            $product_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $reduced_product_arr[$key][1], 'bulk_product_weight')[0];
            $orderdata = '<tr><td><b>'.ucfirst($key).':</b></td><td>'.$reduced_product_arr[$key][0].'</td></tr>';
            
            print_r($orderdata);
            //print_r('-------');
            //print_r($product_weight);
            print_r('-------');
        }*/
        if(isset($_POST['order_user_id'])&&!empty($_POST['order_user_id'])):
            $current_user_id = $_POST['order_user_id'];
        else:
            $current_user_id = get_current_user_id();
        endif;
        $total_price = '';
        $product_price = $total_product_price = $button_badges_price = $total_button_badges_price = 
        $tie_price = $total_tie_price = 
        $belt_price = $total_belt_price = 
        $sticker_card_price = $total_sticker_card_price = 
        $fusing_card_price = $total_fusing_card_price = 
        $pvc_holder_price = $total_pvc_holder_price = 
        $sticker_card_holder_price = $total_sticker_card_holder_price =
        $fittings_price = $total_fittings_price =         
        $sticker_fittings_price = $total_sticker_fittings_price =  $sticker_fittings_weight = 
        $multicolor_fittings_price = $total_multicolor_fittings_price =  $multicolor_fittings_weight = 
        $rope_fittings_only_price = $total_rope_fittings_only_price =  $rope_fittings_only_weight = 
        $lanyards_price = $total_lanyards_price = 
        $button_badges_weight = $tie_weight = $belt_weight = $fusing_card_weight = 
        $sticker_card_weight = $pvc_holder_weight = $sticker_card_holder_weight = 
        $fittings_weight = $lanyards_weight = $satin_size_price = $satin_material_price = $satin_material_weight = 0;

        $designurl = $multicolor_design = '';
        $shippingaddress = get_user_meta( $current_user_id, 'shipping_address_1', true ); 
        if($shippingaddress){
            $user_state = get_user_meta( $current_user_id, 'shipping_state', true );
        }else{
            $user_state = get_user_meta( $current_user_id, 'billing_state', true );
        }
        
        $shipping_cost = 0;

        if(isset($_POST['delivery_through']) && !empty($_POST['delivery_through']) && $_POST['delivery_through']=='Courier'){
            $shippingid = $_POST['courier_shipping'];
            if($user_state!='TN' && $user_state!='PY' && $user_state!='KA' && $user_state!='KL' && $user_state!='AP'){
                $shipping_by = $_POST['shipping_by'];
            }else{
                $shipping_by = '';
            }
            if($user_state == 'TN' || $user_state == 'PY'){
                $shipping_cost = get_post_meta($shippingid,'price_for_tn',true);
            }elseif($user_state=='KA' || $user_state=='KL' || $user_state=='AP'){
                $shipping_cost = get_post_meta($shippingid,'price_for_andhra_kerala_karnataka',true);
            }else{
                if($shipping_by=='Surface'){
                    $shipping_cost = get_post_meta($shippingid,'price_for_north_india_surface',true);
                }else{
                    $shipping_cost = get_post_meta($shippingid,'price_for_north_india_air',true);
                }
            }
        }

        if(isset($_POST['delivery_through']) && !empty($_POST['delivery_through']) && $_POST['delivery_through']=='Transport'){
            $shippingid = $_POST['transport_shipping'];
            if($_POST['transport_payment_type']=='Pay'){
                $shipping_cost = get_post_meta($shippingid,'pay_price',true);
            }else{
                $shipping_cost = get_post_meta($shippingid,'to_pay_price',true);
            }
        }
        //print_r($shipping_cost);
        if((isset($_POST['delivery_through']) && !empty($_POST['delivery_through']) && $_POST['delivery_through']=='Direct') || (isset($_POST['delivery_through']) && !empty($_POST['delivery_through']) && $_POST['delivery_through']=='Bus')){
            $shipping_cost = 0;
        }
        $shipping_info = get_post($shippingid);
        //print_r($shipping_info->post_title);
        //exit;

        if(isset($_FILES['multicolor_design']) && !empty( $_FILES["multicolor_design"]["name"] )){
            $upload = wp_handle_upload( $_FILES["multicolor_design"] , array( 'test_form' => false ) );
            $multicolor_design = $upload['url'];
        }
        if(isset($_FILES['design_upload']) && !empty( $_FILES["design_upload"]["name"] )){
            $upload = wp_handle_upload( $_FILES["design_upload"] , array( 'test_form' => false ) );
            $designurl = $upload['url'];
        }
        $ordervalue = [];
        $orderdata = '<table class="widefat fixed">';
        $user_message = '';
        $ropedata = $beltdata = $tiedata = $mulitcolorbeltdata = $polyesterbeltdata = $polyesterspecialbeltdata ='';
        if(isset($_POST['product_quantity_count'])&&!empty($_POST['product_quantity_count'])){
            $ordervalue['Product Quantity'] = $_POST['product_quantity_count'];
            $orderdata .= '<tr><td style="width:150px"><b>Product Quantity:</b></td><td><b>'.$_POST['product_quantity_count'].'</td></tr>';
        }
        if(isset($_POST['product_group']) && !empty($_POST['product_group'])){
            $ordervalue['Product Type'] = $_POST['product_group'];
            $orderdata .= '<tr><td width:150px><b>Product Type:</b></td><td><b>'.$_POST['product_group'].'</b></td></tr>';
        }
       // print_r($reduced_product_arr['lanyards_thickness']);
        //$int = (int) filter_var($reduced_product_arr['lanyards_thickness'][0], FILTER_SANITIZE_NUMBER_INT);
        $lanyard_thickness = 0;
        if($reduced_product_arr['lanyards_thickness']){
            $lanyard_thickness = preg_replace('/[^0-9]/', '', $reduced_product_arr['lanyards_thickness'][0]);
        }
        $pvc_fittings_size_price = $sticker_fittings_size_price =  $multicolor_fittings_size_price = $rope_only_fittings_size_price = $pvc_fittings_size_price1 = $sticker_fittings_size_price1 =  $multicolor_fittings_size_price1 = $rope_only_fittings_size_price1 = 0;
        if(isset($_POST['pvc-fitting']) && !empty($_POST['pvc-fitting'])){
            foreach(get_option('bulk_product_sizes_pvc_fittings_sizes') as $sizes):
                $price_sizes = explode(',',$sizes);
                if(preg_replace('/[^0-9]/', '', $price_sizes[0])==$lanyard_thickness){
                    $pvc_fittings_size_price = $price_sizes[1];
                }
                
            endforeach;
        }
        
        if(isset($_POST['sticker-fittings']) && !empty($_POST['sticker-fittings'])){
            foreach(get_option('bulk_product_sizes_sticker_fittings_sizes') as $sizes):
                $price_sizes = explode(',',$sizes);
                if(preg_replace('/[^0-9]/', '', $price_sizes[0])==$lanyard_thickness){
                    $sticker_fittings_size_price = $price_sizes[1];
                }
            endforeach;
        }
        if(isset($_POST['multicolor-fittings']) && !empty($_POST['multicolor-fittings'])){
            foreach(get_option('bulk_product_sizes_multi_color_fittings_sizes') as $sizes):
                $price_sizes = explode(',',$sizes);
                if(preg_replace('/[^0-9]/', '', $price_sizes[0])==$lanyard_thickness){
                    $multicolor_fittings_size_price = $price_sizes[1];
                }
            endforeach;
        }
        if(isset($_POST['rope-fittings-only']) && !empty($_POST['rope-fittings-only'])){
            foreach(get_option('bulk_product_sizes_rope_fittings_only_sizes') as $sizes):
                $price_sizes = explode(',',$sizes);
                if(preg_replace('/[^0-9]/', '', $price_sizes[0])==$lanyard_thickness){
                    $rope_only_fittings_size_price = $price_sizes[1];
                }
            endforeach;
        }
        
        $thickness_price_size_arr = [];
        $thickness_price_cost_arr = [];
        
        foreach ($reduced_product_arr as $key => $value) {
            
            $product_price_data = get_post_meta($reduced_product_arr[$key][1], 'product_price',true);
            $thickness_data = pods_field('bulk_order_products', $reduced_product_arr[$key][1], 'bulk_product_thickness');
            
            $thickness_arr = [];
            $thickness_price_arr = [];
            foreach($thickness_data as $thick){
                $thickness_arr[] = explode(',',$thick)[0];
                $thickness_price_arr[] = explode(',',$thick)[1];
            }
            $offset = array_search($lanyard_thickness, $thickness_arr);
           
            if($key=='lanyards_thickness'){
                foreach($full_product_data as $full_data){
                    $thickness_price_data[] = pods_field('bulk_order_products', $full_data[1], 'thickness_prices');
                }
                
                $reduced_product_arr = array_slice( $product_data, 0, -1 );
                foreach($thickness_price_data as $pricedata):
                    $thickness_price_size_arr = $thickness_price_cost_arr = [];
                    if(count($pricedata)>1){
                        foreach($pricedata as $pdata):
                            $thickness_price_size_arr[] = explode(',',$pdata)[0];
                            $thickness_price_cost_arr[] = explode(',',$pdata)[1];
                        endforeach;
                        $arr_res = array_search($lanyard_thickness, $thickness_price_size_arr);
                        if(!empty($_POST['totalresult'])){
                            $product_extra_price += $_POST['totalresult']  * $thickness_price_cost_arr[$arr_res];
                        }
                    }
                endforeach;
                foreach ($product_price_data as $price){
                    $product_price = $thickness_price_arr[$offset];
                    /*if($lanyard_thickness == $price['quantity']){
                        
                        $product_price = $price['price'];
                    }*/
                }
                $satin_size_price = $product_price;
                if(count($product_price_data)>1){
                    foreach ($product_price_data as $price){
                        if($price['quantity'] >= $_POST['totalresult']){
                            $product_price = $product_price + $price['price'];
                            break;
                        }
                    }
                    if(!$product_price){
                        $product_price = $product_price + end($product_price_data)['price'];
                    }
                    
                }else{
                    $product_price = $product_price + $product_price_data[0]['price'];
                }
            }else{
                if(count($product_price_data)>1){
                    foreach ($product_price_data as $price){
                        if($price['quantity'] >= $_POST['product_quantity_count']){
                            $product_price = $price['price'];
                            break;
                        }
                    }
                    if(!$product_price){
                        $product_price = end($product_price_data)['price'];
                    }
                    
                }else{
                    $product_price = $product_price_data[0]['price'];
                }
            }
            if(isset($reduced_product_arr['sticker-cards'])||isset($reduced_product_arr['fusing-cards'])){
                $total_product_price += $_POST['product_quantity_count'] * $product_price;
                    //$total_product_price += $_POST['product_quantity_count'] * $pvc_fittings_size_price + $_POST['product_quantity_count'] * $sticker_fittings_size_price + $_POST['product_quantity_count'] * $multicolor_fittings_size_price + $_POST['product_quantity_count'] * $rope_only_fittings_size_price;
                $product_weight += $_POST['product_quantity_count'] * pods_field('bulk_order_products', $reduced_product_arr[$key][1], 'bulk_product_weight')[0];
            }else{
                if(!empty($_POST['totalresult'])){
                    $total_product_price += $_POST['totalresult']  * $product_price;
                    //$total_product_price += $_POST['totalresult']  * $pvc_fittings_size_price + $_POST['totalresult']  * $sticker_fittings_size_price + $_POST['totalresult']  * $multicolor_fittings_size_price + $_POST['totalresult']  * $rope_only_fittings_size_price;
    
                    $product_weight += $_POST['totalresult'] * pods_field('bulk_order_products', $reduced_product_arr[$key][1], 'bulk_product_weight')[0];
                }else{
                    if(isset($_POST['dooming_sticker_size_width']) && isset($_POST['dooming_sticker_size_height']) && !empty($_POST['dooming_sticker_size_height']) && !empty($_POST['dooming_sticker_size_width'])){
                        $product_width = ceil($_POST['dooming_sticker_size_width']);                       
                        $product_height = ceil($_POST['dooming_sticker_size_height']);                       
                        $max_size = $product_width * $product_height;                       
                        $product_price = $product_price * $max_size;
                       
                    }
                    $total_product_price += $_POST['product_quantity_count'] * $product_price;
                    
                    //$total_product_price += $_POST['product_quantity_count'] * $pvc_fittings_size_price + $_POST['product_quantity_count'] * $sticker_fittings_size_price + $_POST['product_quantity_count'] * $multicolor_fittings_size_price + $_POST['product_quantity_count'] * $rope_only_fittings_size_price;
                    $product_weight += $_POST['product_quantity_count'] * pods_field('bulk_order_products', $reduced_product_arr[$key][1], 'bulk_product_weight')[0];
                }
            }
            /*if(!empty($_POST['totalresult'])){
                $total_product_price += $_POST['totalresult']  * $product_price;
                //$total_product_price += $_POST['totalresult']  * $pvc_fittings_size_price + $_POST['totalresult']  * $sticker_fittings_size_price + $_POST['totalresult']  * $multicolor_fittings_size_price + $_POST['totalresult']  * $rope_only_fittings_size_price;

                $product_weight += $_POST['totalresult'] * pods_field('bulk_order_products', $reduced_product_arr[$key][1], 'bulk_product_weight')[0];
            }else{
                $total_product_price += $_POST['product_quantity_count'] * $product_price;
                //$total_product_price += $_POST['product_quantity_count'] * $pvc_fittings_size_price + $_POST['product_quantity_count'] * $sticker_fittings_size_price + $_POST['product_quantity_count'] * $multicolor_fittings_size_price + $_POST['product_quantity_count'] * $rope_only_fittings_size_price;
                $product_weight += $_POST['product_quantity_count'] * pods_field('bulk_order_products', $reduced_product_arr[$key][1], 'bulk_product_weight')[0];
            }*/
            //$total_product_price += $_POST['product_quantity_count'] * $product_price;
            //$product_weight += $_POST['product_quantity_count'] * pods_field('bulk_order_products', $reduced_product_arr[$key][1], 'bulk_product_weight')[0];
            $ordervalue[ucfirst($key)] = $reduced_product_arr[$key][0];
            
            $orderdata .= '<tr><td><b>'.ucfirst($key).':</b></td><td><b>'.$reduced_product_arr[$key][0].'</b></td></tr>';
        }
        
        $pricekey = array_search($lanyard_thickness, $thickness_price_size_arr);
        //$product_extra_price = $thickness_price_cost_arr[$pricekey];
        /*
        $product_type_slug = $_POST['product_type_slug'];
        if(isset($_POST["$product_type_slug"]) && !empty($product_type_slug)){
            $orderdata .= '<tr><td><b>'.$_POST['product_group'].':</b></td><td>'.$_POST['button-badges'][0].'</td></tr>';
            $product_price_data = get_post_meta($_POST["$product_type_slug"][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $product_price = $price['price'];
                        break;
                    }
                }
                if(!$product_price){
                    $product_price = end($product_price_data)['price'];
                }
            }else{
                $product_price = $product_price_data[0]['price'];
                
            }
            $total_product_price = $_POST['product_quantity_count'] * $product_price;
        }
        
        print_r($product_price_data);
        exit;*/
        /*if(isset($_POST['button-badges']) && !empty($_POST['button-badges'])){

            $orderdata .= '<tr><td><b>'.$_POST['product_group'].':</b></td><td>'.$_POST['button-badges'][0].'</td></tr>';

            $product_price_data = get_post_meta($_POST['button-badges'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $button_badges_price = $price['price'];
                        break;
                    }
                }
                if(!$button_badges_price){
                    $button_badges_price = end($product_price_data)['price'];
                }
            }else{
                $button_badges_price = $product_price_data[0]['price'];
                
            }
            $total_button_badges_price = $_POST['product_quantity_count'] * $button_badges_price;
            $button_badges_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['button-badges'][1], 'bulk_product_weight')[0];

        }

        if(isset($_POST['tie']) && !empty($_POST['tie'])){

            $orderdata .= '<tr><td><b>'.$_POST['product_group'].':</b></td><td>'.$_POST['tie'][0].'</td></tr>';

            $product_price_data = get_post_meta($_POST['tie'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $tie_price = $price['price'];
                        break;
                    }
                }
                if(!$tie_price){
                    $tie_price = end($product_price_data)['price'];
                }
            }else{
                $tie_price = $product_price_data[0]['price'];
                
            }
            $total_tie_price = $_POST['product_quantity_count'] * $tie_price;
            $tie_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['tie'][1], 'bulk_product_weight')[0];

        }

        if(isset($_POST['belt']) && !empty($_POST['belt'])){
            $orderdata .= '<tr><td><b>'.$_POST['product_group'].':</b></td><td>'.$_POST['belt'][0].'</td></tr>';
            $product_price_data = get_post_meta($_POST['belt'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $belt_price = $price['price'];
                        break;
                    }
                }
                if(!$belt_price){
                    $belt_price = end($product_price_data)['price'];
                }
            }else{
                $belt_price = $product_price_data[0]['price'];
                
            }
            $total_belt_price = $_POST['product_quantity_count'] * $belt_price;
            $belt_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['belt'][1], 'bulk_product_weight')[0];

        }


        if(isset($_POST['sticker-cards']) && !empty($_POST['sticker-cards'])){

            $orderdata .= '<tr><td><b>Sticker Card:</b></td><td>'.$_POST['sticker-cards'][0].'</td></tr>';

            $product_price_data = get_post_meta($_POST['sticker-cards'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $sticker_card_price = $price['price'];
                        break;
                    }
                }
                if(!$sticker_card_price){
                    $sticker_card_price = end($product_price_data)['price'];
                }
            }else{
                $sticker_card_price = $product_price_data[0]['price'];
                
            }
            $total_sticker_card_price = $_POST['product_quantity_count'] * $sticker_card_price;
            $sticker_card_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['sticker-cards'][1], 'bulk_product_weight')[0];

        }
        if(isset($_POST['fusing-cards']) && !empty($_POST['fusing-cards'])){
            //print_r($_POST['fusing-cards'][1]);
            $orderdata .= '<tr><td><b>Fusing Card:</b></td><td>'.$_POST['fusing-cards'][0].'</td></tr>';

            $product_price_data = get_post_meta($_POST['fusing-cards'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $fusing_card_price = $price['price'];
                        break;
                    }
                }
                if(!$fusing_card_price){
                    $fusing_card_price = end($product_price_data)['price'];
                }
            }else{
                $fusing_card_price = $product_price_data[0]['price'];
                
            }
            $total_fusing_card_price = $_POST['product_quantity_count'] * $fusing_card_price;
            $fusing_card_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['fusing-cards'][1], 'bulk_product_weight')[0];

        }
        if(isset($_POST['fittings']) && !empty($_POST['fittings'])){

            $orderdata .= '<tr><td><b>Fittings:</b></td><td>'.$_POST['fittings'][0].'</td></tr>';
            $product_price_data = get_post_meta($_POST['fittings'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $fittings_price = $price['price'];
                        break;
                    }
                }
                if(!$fittings_price){
                    $fittings_price = end($product_price_data)['price'];
                }
            }else{
                $fittings_price = $product_price_data[0]['price'];
                
            }            
            
            $total_fittings_price = $_POST['product_quantity_count'] * $fittings_price;
            $fittings_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['fittings'][1], 'bulk_product_weight')[0];
           // print_r($fittings_weight);
            //print_r($total_fittings_price);exit;
        }*/
        /*if(!empty($_POST['totalresult'])){
            $total_product_extra_price += $_POST['totalresult'] * $product_extra_price;            
        }else{
            $total_product_extra_price += $_POST['product_quantity_count'] * $product_extra_price;            
        }*/
        if(isset($_POST['pvc-fitting']) && !empty($_POST['pvc-fitting'])){
            $ordervalue['Fittings'] = $_POST['pvc-fitting'][0];
            $orderdata .= '<tr><td><b>Fittings:</b></td><td><b>'.$_POST['pvc-fitting'][0].'</b></td></tr>';
            $product_price_data = get_post_meta($_POST['pvc-fitting'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['totalresult']){
                        $fittings_price = $price['price'];
                        break;
                    }
                }
                if(!$fittings_price){
                    $fittings_price = end($product_price_data)['price'];
                }
            }else{
                $fittings_price = $product_price_data[0]['price'];
                
            }            
            if(!empty($_POST['totalresult'])){
                $total_fittings_price = $_POST['totalresult'] * $fittings_price;            
                $fittings_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['pvc-fitting'][1], 'bulk_product_weight')[0];
            }else{
                $total_fittings_price = $_POST['product_quantity_count'] * $fittings_price;            
                $fittings_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['pvc-fitting'][1], 'bulk_product_weight')[0];
            }
            //print_r($total_fittings_price);exit;
            
        }

        if(isset($_POST['multicolor-fittings']) && !empty($_POST['multicolor-fittings'])){
            $ordervalue['Fittings'] = $_POST['multicolor-fittings'][0];
          //  $orderdata .= '<tr><td><b>Fittings:</b></td><td>'.$_POST['multicolor-fittings'][0].'</td></tr>';
            $product_price_data = get_post_meta($_POST['multicolor-fittings'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['totalresult']){
                        $multicolor_fittings_price = $price['price'];
                        break;
                    }
                }
                if(!$multicolor_fittings_price){
                    $multicolor_fittings_price = end($product_price_data)['price'];
                }
            }else{
                $multicolor_fittings_price = $product_price_data[0]['price'];
                
            }            
            if(!empty($_POST['totalresult'])){
                $total_multicolor_fittings_price = $_POST['totalresult'] * $multicolor_fittings_price;            
                $multicolor_fittings_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['multicolor-fittings'][1], 'bulk_product_weight')[0];
            }else{
                $total_multicolor_fittings_price = $_POST['product_quantity_count'] * $multicolor_fittings_price;            
                $multicolor_fittings_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['multicolor-fittings'][1], 'bulk_product_weight')[0];
            }           
            
        }

        if(isset($_POST['rope-fittings-only']) && !empty($_POST['rope-fittings-only'])){
            $ordervalue['Fittings'] = $_POST['rope-fittings-only'][0];
            $orderdata .= '<tr><td><b>Fittings:</b></td><td><b>'.$_POST['rope-fittings-only'][0].'</b></td></tr>';
            $product_price_data = get_post_meta($_POST['rope-fittings-only'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['totalresult']){
                        $rope_fittings_only_price = $price['price'];
                        break;
                    }
                }
                if(!$rope_fittings_only_price){
                    $rope_fittings_only_price = end($product_price_data)['price'];
                }
            }else{
                $rope_fittings_only_price = $product_price_data[0]['price'];
                
            }            
            if(!empty($_POST['totalresult'])){
                $total_rope_fittings_only_price = $_POST['totalresult'] * $rope_fittings_only_price;
                $rope_fittings_only_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['rope-fittings-only'][1], 'bulk_product_weight')[0];
            }else{
                $total_rope_fittings_only_price = $_POST['product_quantity_count'] * $rope_fittings_only_price;
                $rope_fittings_only_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['rope-fittings-only'][1], 'bulk_product_weight')[0];
            }
            
        }
        
        
        if(isset($_POST['sticker-fittings']) && !empty($_POST['sticker-fittings'])){
            $ordervalue['Fittings'] = $_POST['sticker-fittings'][0];
            $orderdata .= '<tr><td><b>Fittings:</b></td><td><b>'.$_POST['sticker-fittings'][0].'</b></td></tr>';
            $product_price_data = get_post_meta($_POST['sticker-fittings'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['totalresult']){
                        $sticker_fittings_price = $price['price'];
                        break;
                    }
                }
                if(!$fittings_price){
                    $sticker_fittings_price = end($product_price_data)['price'];
                }
            }else{
                $sticker_fittings_price = $product_price_data[0]['price'];
                
            }            
            if(!empty($_POST['totalresult'])){
                $total_sticker_fittings_price = $_POST['totalresult'] * $sticker_fittings_price;            
                $sticker_fittings_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['sticker-fittings'][1], 'bulk_product_weight')[0]; 
            }else{
                $total_sticker_fittings_price = $_POST['product_quantity_count'] * $sticker_fittings_price;            
                $sticker_fittings_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['sticker-fittings'][1], 'bulk_product_weight')[0];
                
            }
            
        }

        if(isset($_POST['sticker-card-holder']) && !empty($_POST['sticker-card-holder'])){
            $ordervalue['Sticker Card Holder'] = $_POST['sticker-card-holder'][0];
            $orderdata .= '<tr><td><b>Sticker Card Holder:</b></td><td><b>'.$_POST['sticker-card-holder'][0].'</b></td></tr>';
            $product_price_data = get_post_meta($_POST['sticker-card-holder'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['totalresult']){
                        $sticker_card_price = $price['price'];
                        break;
                    }
                }
                if(!$sticker_card_price){
                    $sticker_card_price = end($product_price_data)['price'];
                }
            }else{
                $sticker_card_price = $product_price_data[0]['price'];
                
            }            
            if(!empty($_POST['totalresult'])){
                $total_sticker_card_price = $_POST['totalresult'] * $sticker_card_price;
                $sticker_card_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['sticker-card-holder'][1], 'bulk_product_weight')[0];
            }else{
                $total_sticker_card_price = $_POST['product_quantity_count'] * $sticker_card_price;
                $sticker_card_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['sticker-card-holder'][1], 'bulk_product_weight')[0];
            }

            //$total_sticker_card_price = $_POST['totalresult'] * $sticker_card_price;
            //$sticker_card_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['sticker-card-holder'][1], 'bulk_product_weight')[0];
        }
       
        if(isset($_POST['pvc-holders']) && !empty($_POST['pvc-holders'])){
            
            $ordervalue['PVC Holder'] = $_POST['pvc-holders'][0];
            $orderdata .= '<tr><td><b>PVC Holder:</b></td><td><b>'.$_POST['pvc-holders'][0].'</b></td></tr>';
            $product_price_data = get_post_meta($_POST['pvc-holders'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['totalresult']){
                        $pvc_holder_price = $price['price'];
                        break;
                    }
                }
                if(!$pvc_holder_price){
                    $pvc_holder_price = end($product_price_data)['price'];
                }
            }else{
                $pvc_holder_price = $product_price_data[0]['price'];
                
            }   
            if(!empty($_POST['totalresult'])){
                $total_pvc_holder_price = $_POST['totalresult'] * $pvc_holder_price;
                $pvc_holder_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['pvc-holders'][1], 'bulk_product_weight')[0];
            }else{
                $total_pvc_holder_price = $_POST['product_quantity_count'] * $pvc_holder_price;
                $pvc_holder_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['pvc-holders'][1], 'bulk_product_weight')[0];
            }
            
            //$pvc_holder_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['pvc-holders'][1], 'bulk_product_weight')[0];
        }
        
        $multilanyard_thickness = 0;
        if(isset($_POST['lanyards']) && !empty($_POST['lanyards'])){
            if (str_contains($_POST['lanyards'][0], 'Multicolor')) {
                $multilanyard_thickness = preg_replace('/[^0-9]/', '', $_POST['lanyards'][0]);
            }
            
            foreach($full_product_data as $full_data){
                
                $thickness_price_data[] = pods_field('bulk_order_products', $full_data[1], 'thickness_prices');
            }
            
            $reduced_product_arr = array_slice( $product_data, 0, -1 );
            if($multilanyard_thickness):
            foreach($thickness_price_data as $pricedata):
                $thickness_price_size_arr = $thickness_price_cost_arr = [];
                if(count($pricedata)>1){
                    foreach($pricedata as $pdata):
                        //print_R($pdata);
                        $thickness_price_size_arr[] = explode(',',$pdata)[0];
                        $thickness_price_cost_arr[] = explode(',',$pdata)[1];
                        $arr_res = array_search($multilanyard_thickness, $thickness_price_size_arr);
                    endforeach;
                    if(!empty($_POST['totalresult'])){
                        $product_extra_price += $_POST['totalresult']  * $thickness_price_cost_arr[$arr_res];
                    }
                    //print_r('chjckckdsd');
                    //print_r($product_extra_price);
                }
            endforeach;
            endif;
            $ordervalue['Lanyard'] = $_POST['lanyards'][0];
            $orderdata .= '<tr><td><b>Lanyard:</b></td><td><b>'.$_POST['lanyards'][0].'</b></td></tr>';
            $product_price_data = get_post_meta($_POST['lanyards'][1], 'product_price',true);
            if($lanyard_thickness==0){

                if(count($product_price_data)>1){
                    foreach ($product_price_data as $price){
                        if($multilanyard_thickness){
                            if($lanyard_thickness == $price['quantity']){
                                $lanyards_price = $price['price'];
                            }
                        }else{
                            if($price['quantity']>=$_POST['product_quantity_count']){
                                $lanyards_price = $price['price'];
                                break;
                            }
                        }
                        
                    }
                    if(!$lanyards_price){
                        $lanyards_price = end($product_price_data)['price'];
                    }
                }else{
                    $lanyards_price = $product_price_data[0]['price'];
                    
                }            
            }
            
            if(isset($_POST['totalresult']) && !empty($_POST['totalresult'])){
                if(isset($_POST['variation_count1']) && !empty($_POST['variation_count1'])){
                    $ropedata .= '<p>LKG-2 Std2(28 inches) - '.$_POST['variation_count1'].'</p>';
                }
                if(isset($_POST['variation_count2']) && !empty($_POST['variation_count2'])){
                    $ropedata .= '<p>3 - 5 Std(30 inches) - '.$_POST['variation_count2'].'</p>';
                }

                if(isset($_POST['variation_count3']) && !empty($_POST['variation_count3'])){
                    $ropedata .= '<p>6 - 9 Std(32 inches) - '.$_POST['variation_count3'].'</p>';
                }
                if(isset($_POST['variation_count4']) && !empty($_POST['variation_count4'])){
                    $ropedata .= '<p>10 -12 Std(34 inches) - '.$_POST['variation_count4'].'</p>';
                }
                if(isset($_POST['variation_count5']) && !empty($_POST['variation_count5'])){
                    $ropedata .= '<p>Adult(36 inches) - '.$_POST['variation_count5'].'</p>';
                }
                
            }
            $fittings_size_price = 0;
            if($multilanyard_thickness){
                if(isset($_POST['pvc-fitting']) && !empty($_POST['pvc-fitting'])){
                    foreach(get_option('bulk_product_sizes_pvc_fittings_sizes') as $sizes):
                        $price_sizes = explode(',',$sizes);
                        if(preg_replace('/[^0-9]/', '', $price_sizes[0])==$multilanyard_thickness){
                            $pvc_fittings_size_price1 = $price_sizes[1];
                        }
                    endforeach;
                }
                if(isset($_POST['sticker-fittings']) && !empty($_POST['sticker-fittings'])){
                    foreach(get_option('bulk_product_sizes_sticker_fittings_sizes') as $sizes):
                        $price_sizes = explode(',',$sizes);
                        if(preg_replace('/[^0-9]/', '', $price_sizes[0])==$multilanyard_thickness){
                            $sticker_fittings_size_price1 = $price_sizes[1];
                        }
                    endforeach;
                }
                if(isset($_POST['multicolor-fittings']) && !empty($_POST['multicolor-fittings'])){
                    foreach(get_option('bulk_product_sizes_multi_color_fittings_sizes') as $sizes):
                        $price_sizes = explode(',',$sizes);
                        if(preg_replace('/[^0-9]/', '', $price_sizes[0])==$multilanyard_thickness){
                            $multicolor_fittings_size_price1 = $price_sizes[1];
                        }
                    endforeach;
                }
                if(isset($_POST['rope-fittings-only']) && !empty($_POST['rope-fittings-only'])){
                    foreach(get_option('bulk_product_sizes_rope_fittings_only_sizes') as $sizes):
                        $price_sizes = explode(',',$sizes);
                        if(preg_replace('/[^0-9]/', '', $price_sizes[0])==$multilanyard_thickness){
                            $rope_only_fittings_size_price1 = $price_sizes[1];
                        }
                    endforeach;
                }
            }
            $total_lanyards_price = $_POST['totalresult'] * $lanyards_price;
            $fittings_size_price = $_POST['totalresult']  * $pvc_fittings_size_price + $_POST['totalresult']  * $sticker_fittings_size_price + $_POST['totalresult']  * $multicolor_fittings_size_price + $_POST['totalresult']  * $rope_only_fittings_size_price + $_POST['totalresult'] * $pvc_fittings_size_price1 + $_POST['totalresult'] * $sticker_fittings_size_price1 + $_POST['totalresult'] * $multicolor_fittings_size_price1 +  $_POST['totalresult'] * $rope_only_fittings_size_price1;
            $lanyards_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['lanyards'][1], 'bulk_product_weight')[0];
            
        }
        if(isset($_POST['dooming_sticker_size_width']) && isset($_POST['dooming_sticker_size_height']) && !empty($_POST['dooming_sticker_size_height']) && !empty($_POST['dooming_sticker_size_width'])){
            $orderdata .= '<tr><td><b>Size:</b></td><td><b>'.$_POST['dooming_sticker_size_width'].'x'.$_POST['dooming_sticker_size_height'].'</b></td></tr>';
        }
        $multicolor_belt_price = 0;
        if(isset($_POST['multicolor-belt']) && !empty($_POST['multicolor-belt'])){
            $ordervalue['Multicolor Belt'] = $_POST['multicolor-belt'][0];
            //$orderdata .= '<tr><td><b>Multicolor Belt:</b></td><td>'.$_POST['multicolor-belt'][0].'</td></tr>';
            $product_price_data = get_post_meta($_POST['multicolor-belt'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $multicolor_belt_price = $price['price'];
                        break;
                    }
                }
                if(!$multicolor_belt_price){
                    $multicolor_belt_price = end($product_price_data)['price'];
                }
            }else{
                $multicolor_belt_price = $product_price_data[0]['price']; 
            }

            $mulitcolor_belt_sizes_price = 0;
            if(isset($_POST['totalresult']) && !empty($_POST['totalresult'])){
                $multicolorbeltcount = 1;
                foreach(get_option('bulk_product_sizes_multicolor_belt_sizes') as $sizes):
                    if(isset($_POST['variation_multicolorbelt'.$multicolorbeltcount]) && !empty($_POST['variation_multicolorbelt'.$multicolorbeltcount])){
                        $mulitcolorbeltdata .= '<p>'.explode(',',$sizes)[0].' - '.$_POST['variation_multicolorbelt'.$multicolorbeltcount].'</p>';
                        $mulitcolor_belt_sizes_price = $mulitcolor_belt_sizes_price + $_POST['variation_multicolorbelt'.$multicolorbeltcount]*explode(',',$sizes)[1];
                    }
                $multicolorbeltcount++; 
                endforeach;
            }

            $total_multicolorbelt_price = $_POST['totalresult'] * $belt_price + $mulitcolor_belt_sizes_price;
            $multicolorbelt_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['belt'][1], 'bulk_product_weight')[0];
        }
  //Todo
        $polyester_belt_price = 0;
        if(isset($_POST['belt-fitting-polyester']) && !empty($_POST['belt-fitting-polyester'])){
            $ordervalue['Belt Fitting Polyester'] = $_POST['belt-fitting-polyester'][0];
            //$orderdata .= '<tr><td><b>Multicolor Belt:</b></td><td>'.$_POST['multicolor-belt'][0].'</td></tr>';
            $product_price_data = get_post_meta($_POST['belt-fitting-polyester'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $polyester_belt_price = $price['price'];
                        break;
                    }
                }
                if(!$polyester_belt_price){
                    $polyester_belt_price = end($product_price_data)['price'];
                }
            }else{
                $polyester_belt_price = $product_price_data[0]['price']; 
            }

            $polyester_belt_sizes_price = 0;
            if(isset($_POST['totalresult']) && !empty($_POST['totalresult'])){
                $polyesterbeltcount = 1;
                foreach(get_option('bulk_product_sizes_product_belt_sizes') as $sizes):
                    if(isset($_POST['variation_belt'.$polyesterbeltcount]) && !empty($_POST['variation_belt'.$polyesterbeltcount])){
                        $polyesterbeltdata .= '<p>'.explode(',',$sizes)[0].' - '.$_POST['variation_belt'.$polyesterbeltcount].'</p>';
                        $polyester_belt_sizes_price = $polyester_belt_sizes_price + $_POST['variation_belt'.$polyesterbeltcount]*explode(',',$sizes)[1];
                    }
                $polyesterbeltcount++; 
                endforeach;
            }

            $total_polyesterbelt_price = $_POST['totalresult'] * $belt_price + $polyester_belt_sizes_price;
            $polyesterbelt_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['belt'][1], 'bulk_product_weight')[0];
        }
        //
        $polyester_special_belt_price = 0;
        if(isset($_POST['belt-fitting-special']) && !empty($_POST['belt-fitting-special'])){
            $ordervalue['Belt Fitting special'] = $_POST['belt-fitting-special'][0];
            //$orderdata .= '<tr><td><b>Multicolor Belt:</b></td><td>'.$_POST['multicolor-belt'][0].'</td></tr>';
            $product_price_data = get_post_meta($_POST['belt-fitting-special'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $polyester_special_belt_price = $price['price'];
                        break;
                    }
                }
                if(!$polyester_special_belt_price){
                    $polyester_special_belt_price = end($product_price_data)['price'];
                }
            }else{
                $polyester_special_belt_price = $product_price_data[0]['price']; 
            }

            $polyesterspecial_belt_sizes_price = 0;
            if(isset($_POST['totalresult']) && !empty($_POST['totalresult'])){
                $polyesterspecialbeltcount = 1;
                foreach(get_option('bulk_product_sizes_product_belt_sizes') as $sizes):
                    if(isset($_POST['variation_belt'.$polyesterspecialbeltcount]) && !empty($_POST['variation_belt'.$polyesterspecialbeltcount])){
                        $polyesterspecialbeltdata .= '<p>'.explode(',',$sizes)[0].' - '.$_POST['variation_belt'.$polyesterspecialbeltcount].'</p>';
                        $polyesterspecial_belt_sizes_price = $polyesterspecial_belt_sizes_price + $_POST['variation_belt'.$polyesterspecialbeltcount]*explode(',',$sizes)[1];
                    }
                $polyesterspecialbeltcount++; 
                endforeach;
            }

            $total_polyesterspecialbelt_price = $_POST['totalresult'] * $belt_price + $polyesterspecial_belt_sizes_price;
            $polyesterbeltspecial_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['belt'][1], 'bulk_product_weight')[0];
        }
        

        if(isset($_POST['satin-lanyard-material']) && !empty($_POST['satin-lanyard-material'])){
            $satin_material_size_price = $satinmaterial_price = 0;
             $orderdata .= '<tr><td><b>Color </b></td><td><b>'.$_POST['satin-lanyard-material'][0].'</b></td></tr>';
            $ordervalue['Satin Lanyard Material'] = $_POST['satin-lanyard-material'][0];
            $product_price_data = get_post_meta($_POST['satin-lanyard-material'][1], 'product_price',true);
            $satinmaterial_price = $product_price_data[0]['price'];
            $selected_satin_size = $_POST['satin_material_lanyards'][0];
            foreach(get_option('bulk_product_sizes_satin_lanyard_material_sizes') as $sizes):
                if(isset($_POST['satin_material_lanyards']) && !empty($_POST['satin_material_lanyards'])){
                    $available_satin_size = explode(',',$sizes);
                    if($selected_satin_size==$available_satin_size[0]){
                       $satin_material_size_price = $available_satin_size[1];
                    }
                }
            endforeach;
            $satin_material_price = $_POST['product_quantity_count'] * ($satinmaterial_price + $satin_material_size_price);
            $satin_material_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['satin-lanyard-material'][1], 'bulk_product_weight')[0];
        }
         if(isset($_POST['satin-material-lanyard']) && !empty($_POST['satin-material-lanyard'])){
           
        }
        if(isset($_POST['belt-normal']) && !empty($_POST['belt-normal'])){
            
            $ordervalue['Belt'] = $_POST['belt-normal)'][0];
            $orderdata .= '<tr><td><b>Belt:</b></td><td><b>'.$_POST['belt-normal'][0].'</b></td></tr>';
            $product_price_data = get_post_meta($_POST['belt-normal'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $belt_price = $price['price'];
                        break;
                    }
                }
                if(!$belt_price){
                    $belt_price = end($product_price_data)['price'];
                }
            }else{
                $belt_price = $product_price_data[0]['price']; 
            }

            $belt_sizes_price = 0;
            if(isset($_POST['totalresult']) && !empty($_POST['totalresult'])){
                $beltcount = 1;
                foreach(get_option('bulk_product_sizes_product_belt_sizes') as $sizes):
                    if(isset($_POST['variation_belt'.$beltcount]) && !empty($_POST['variation_belt'.$beltcount])){
                        $beltdata .= '<p>'.explode(',',$sizes)[0].' - '.$_POST['variation_belt'.$beltcount].'</p>';
                        $belt_sizes_price = $belt_sizes_price + $_POST['variation_belt'.$beltcount]*explode(',',$sizes)[1];
                    }
                $beltcount++; 
                endforeach;
                //print_r($beltdata);exit;
                /*if(isset($_POST['variation_belt1']) && !empty($_POST['variation_belt1'])){
                    $beltdata .= '<p>LKG to 5th Std(75 CM) - '.$_POST['variation_belt1'].'</p>';
                }
                if(isset($_POST['variation_belt2']) && !empty($_POST['variation_belt2'])){
                    $beltdata .= '<p>6th Std to 10th(85 CM) - '.$_POST['variation_belt2'].'</p>';
                }

                if(isset($_POST['variation_belt3']) && !empty($_POST['variation_belt3'])){
                    $beltdata .= '<p>11th Std to 12th(95 CM)  - '.$_POST['variation_belt3'].'</p>';
                }
                if(isset($_POST['variation_belt4']) && !empty($_POST['variation_belt4'])){
                    $beltdata .= '<p>Above(100 CM)  - '.$_POST['variation_belt4'].'</p>';
                }*/
            }

            $total_belt_price = $_POST['totalresult'] * $belt_price + $belt_sizes_price;
            $belt_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['belt'][1], 'bulk_product_weight')[0];
        }
        
        if(isset($_POST['tie']) && !empty($_POST['tie'])){
            $ordervalue['Tie'] = $_POST['tie'][0];
            $orderdata .= '<tr><td><b>Tie:</b></td><td><b>'.$_POST['tie'][0].'</b></td></tr>';
            $product_price_data = get_post_meta($_POST['tie'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $tie_price = $price['price'];
                        break;
                    }
                }
                if(!$tie_price){
                    $tie_price = end($product_price_data)['price'];
                }
            }else{
                $tie_price = $product_price_data[0]['price'];
                
            }
            $tie_sizes_price = 0;
            if(isset($_POST['totalresult']) && !empty($_POST['totalresult'])){
                $tiecount = 1;
                foreach(get_option('bulk_product_sizes_product_tie_sizes') as $sizes):
                    if(isset($_POST['variation_tie'.$tiecount]) && !empty($_POST['variation_tie'.$tiecount])){
                        $tiedata .= '<p>'.explode(',',$sizes)[0].' - '.$_POST['variation_tie'.$tiecount].'</p>';
                        $tie_sizes_price = $tie_sizes_price + $_POST['variation_tie'.$tiecount]*explode(',',$sizes)[1];
                    }
                $tiecount++; 
                endforeach;

                /*if(isset($_POST['variation_tie1']) && !empty($_POST['variation_tie1'])){
                    $tiedata .= '<p>LKG to 4th Std(10 in) - '.$_POST['variation_tie1'].'</p>';
                }
                if(isset($_POST['variation_tie2']) && !empty($_POST['variation_tie2'])){
                    $tiedata .= '<p>5th Std to 8th(12 in) - '.$_POST['variation_tie2'].'</p>';
                }

                if(isset($_POST['variation_tie3']) && !empty($_POST['variation_tie3'])){
                    $tiedata .= '<p>9th Std to 10th(14 in)  - '.$_POST['variation_tie3'].'</p>';
                }
                if(isset($_POST['variation_tie4']) && !empty($_POST['variation_tie4'])){
                    $tiedata .= '<p>11th Std to 12th(Long 48 in)  - '.$_POST['variation_tie4'].'</p>';
                }*/ 
            }
            
            $total_tie_price = $_POST['totalresult'] * $tie_price + $tie_sizes_price;
            $tie_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['tie'][1], 'bulk_product_weight')[0];
        }
        
         
        $multicolor_tie_price =0;
        $multicolor_tie_sizes_price = $total_multicolor_tie_price = $multicolor_tie_weight = 0;
        if(isset($_POST['tie-multicolor']) && !empty($_POST['tie-multicolor'])){
            
            $ordervalue['Tie'] = $_POST['tie-multicolor'][0];
            //$orderdata .= '<tr><td><b>Tie Multicolor:</b></td><td>'.$_POST['tie-multicolor'][0].'</td></tr>';
            $product_price_data = get_post_meta($_POST['tie-multicolor'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $multicolor_tie_price = $price['price'];
                        break;
                    }
                }
                if(!$multicolor_tie_price){
                    $multicolor_tie_price = end($product_price_data)['price'];
                }
            }else{
                $multicolor_tie_price = $product_price_data[0]['price'];
                
            }
            if(isset($_POST['totalresult']) && !empty($_POST['totalresult'])){
                $tiecount = 1;
                foreach(get_option('bulk_product_sizes_tie_sizes') as $sizes):
                    
                    if(isset($_POST['multi_tie'.$tiecount]) && !empty($_POST['multi_tie'.$tiecount])){
                       
                        $tiedata .= '<p>'.explode(',',$sizes)[0].' - '.$_POST['multi_tie'.$tiecount].'</p>';
                        $multicolor_tie_sizes_price = $multicolor_tie_sizes_price + $_POST['multi_tie'.$tiecount]*explode(',',$sizes)[1];
                    }
                $tiecount++; 
                endforeach;
            }
            
            $total_multicolor_tie_price = $_POST['totalresult'] * $multicolor_tie_sizes_price;
            $multicolor_tie_weight = $_POST['totalresult'] * pods_field('bulk_order_products', $_POST['tie'][1], 'bulk_product_weight')[0];
        }


        $name_on_rope = '';
        $screen_print_price = 0;
        if(isset($_POST['name_on_rope']) && !empty($_POST['name_on_rope'])){
            foreach($_POST['name_on_rope'] as $name){
                if(!empty($name)){
                    $name_on_rope = $name;
                    if($_POST['totalresult']>get_option('screenprintgreaterquenty')){
                        $screen_cost = get_option('screenprint');
                        $screen_print_price = $_POST['totalresult'] * $screen_cost;
                    }else{
                        $screen_print_price = get_option('screenprintgreater');
                    }
                    
                }
            }
        }
        
        if($name_on_rope){
            $ordervalue['Name on Rope'] = $name_on_rope;
            $orderdata .= '<tr><td><b>Name on Rope:</b></td><td><b>'.$name_on_rope.'</b></td></tr>';
        }
        if($ropedata){
            $orderdata .= '<tr><td><b>Rope Information:</b></td><td><b>'.$ropedata.'</b></td></tr>';
            $orderdata .= '<tr><td><b>Total Rope Count:</b></td><td><b>'.$_POST['totalresult'].'</b></td></tr>';
        }
        if($beltdata){
            $orderdata .= '<tr><td><b>Belt Information:</b></td><td><b>'.$beltdata.'</b></td></tr>';
            $orderdata .= '<tr><td><b>Total Belt Count:</b></td><td><b>'.$_POST['totalresult'].'</b></td></tr>';
        }
        if($mulitcolorbeltdata){
            $orderdata .= '<tr><td><b>Multicolor Belt Information:</b></td><td><b>'.$mulitcolorbeltdata.'</b></td></tr>';
            $orderdata .= '<tr><td><b>Total Belt Count:</b></td><td><b>'.$_POST['totalresult'].'</b></td></tr>';
        }
        if($tiedata){
            $orderdata .= '<tr><td><b>Tie Information:</b></td><td><b>'.$tiedata.'</b></td></tr>';
            $orderdata .= '<tr><td><b>Total Tie Count:</b></td><td><b>'.$_POST['totalresult'].'</b></td></tr>';
        }
        /*if(isset($_POST['pvc-holders']) && !empty($_POST['pvc-holders'])){

            $orderdata .= '<tr><td><b>Holder:</b></td><td>'.$_POST['pvc-holders'][0].'</td></tr>';

            $product_price_data = get_post_meta($_POST['pvc-holders'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $pvc_holder_price = $price['price'];
                        break;
                    }
                }
                if(!$pvc_holder_price){
                    $pvc_holder_price = end($product_price_data)['price'];
                }
            }else{
                $pvc_holder_price = $product_price_data[0]['price'];   
            }
            
            $total_pvc_holder_price = $_POST['product_quantity_count'] * $pvc_holder_price;
            $pvc_holder_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['pvc-holders'][1], 'bulk_product_weight')[0];

        }
        if(isset($_POST['sticker-card-holder']) && !empty($_POST['sticker-card-holder'])){

            $orderdata .= '<tr><td><b>Holder:</b></td><td>'.$_POST['sticker-card-holder'][0].'</td></tr>';

            $product_price_data = get_post_meta($_POST['sticker-card-holder'][1], 'product_price',true);
            if(count($product_price_data)>1){
                foreach ($product_price_data as $price){
                    if($price['quantity']>=$_POST['product_quantity_count']){
                        $sticker_card_holder_price = $price['price'];
                        break;
                    }
                }
                if(!$sticker_card_holder_price){
                    $sticker_card_holder_price = end($product_price_data)['price'];
                }
            }else{
                $sticker_card_holder_price = $product_price_data[0]['price'];
            }            
            
            $total_sticker_card_holder_price = $_POST['product_quantity_count'] * $sticker_card_holder_price;
            $sticker_card_holder_weight = $_POST['product_quantity_count'] * pods_field('bulk_order_products', $_POST['sticker-card-holder'][1], 'bulk_product_weight')[0];

        }*/

        //$order_total = $total_sticker_card_price + $total_fusing_card_price + $total_pvc_holder_price + $total_sticker_card_holder_price + $total_fittings_price + $total_lanyards_price + $total_belt_price + $total_button_badges_price + $total_tie_price;
        //$order_weight = $fusing_card_weight + $sticker_card_weight + $pvc_holder_weight + $sticker_card_holder_weight + $fittings_weight + $lanyards_weight + $belt_weight + $button_badges_weight + $tie_weight;
        
        //$orderdata .= '<tr><td><b>Order Total:</b></td><td>Rs. '.$order_total.'</td></tr>';
        /*print_R($total_product_price);
        print_R('--');
        print_R($screen_print_price);
        print_R('--');
        print_R($total_lanyards_price);
        print_R('--');
        print_R($total_belt_price);
        print_R('--');
        print_R($total_tie_price);
        print_R('--');
        print_R($total_fittings_price);
        print_R('--');
        print_R($total_sticker_card_price);
        print_R('--');
        print_R($total_pvc_holder_price);
        print_R('--');
        print_R($total_sticker_fittings_price);

        print_R('--');
        print_R($total_multicolor_fittings_price);
        print_R('--');
        print_R($total_rope_fittings_only_price);
        print_R('--1sasas');
        print_R($fittings_size_price);*/
        if($total_product_price>0){
            $total_multicolor_fittings_price = 0;
        }
        $order_total = $total_product_price + $screen_print_price + $total_lanyards_price + $total_belt_price + $total_tie_price + $total_fittings_price + 
        $total_sticker_card_price + $total_pvc_holder_price + $total_sticker_fittings_price + $total_multicolor_fittings_price + $total_rope_fittings_only_price + 
        $total_multicolorbelt_price + $total_polyesterbelt_price + $total_polyesterspecialbelt_price + $fittings_size_price + $product_extra_price + $total_multicolor_tie_price + $satin_material_price;
        if(isset($_POST['lanyards_thickness']) && !empty($_POST['lanyards_thickness']) && $_POST['product_type_slug']=='fusing-card-fullset'){
            if(!empty($_POST['totalresult']) && !empty($_POST['product_quantity_count'])){
                $order_total = ($_POST['totalresult'] > $_POST['product_quantity_count']) ? $order_total + ($_POST['totalresult'] - $_POST['product_quantity_count']) * $satin_size_price : $order_total - ($_POST['product_quantity_count'] - $_POST['totalresult']) * $satin_size_price;
            }
        }
        if(isset($_POST['lanyards_thickness']) && !empty($_POST['lanyards_thickness']) && $_POST['product_type_slug']=='sticker-card-fullset'){
            if(!empty($_POST['totalresult']) && !empty($_POST['product_quantity_count'])){
                $order_total = ($_POST['totalresult'] > $_POST['product_quantity_count']) ? $order_total + ($_POST['totalresult'] - $_POST['product_quantity_count']) * $satin_size_price : $order_total - ($_POST['product_quantity_count'] - $_POST['totalresult']) * $satin_size_price;
            }
        }
        if(isset($_POST['lanyards_thickness']) && !empty($_POST['lanyards_thickness']) && $_POST['product_type_slug']=='event-card'){
            //TODO
            $eventcard_price = 0;
            if(!empty($_POST['totalresult']) && !empty($_POST['product_quantity_count'])){
                $eventcard_price_data = get_post_meta($_POST['event-card-big-cards'][1], 'product_price',true);               
                if(count($eventcard_price_data)>1){
                    foreach ($eventcard_price_data as $price){
                        if($price['quantity']>=$_POST['product_quantity_count']){
                            $eventcard_price = $price['price'];
                            break;
                        }
                    }
                    if(!$eventcard_price){
                        $eventcard_price = end($eventcard_price_data)['price'];
                    }
                }else{
                    $eventcard_price = $eventcard_price_data[0]['price']; 
                }
                $order_total = ($_POST['totalresult'] > $_POST['product_quantity_count']) ?  $order_total - ($_POST['totalresult'] -$_POST['product_quantity_count'] ) * $eventcard_price : $order_total + ($_POST['product_quantity_count'] - $_POST['totalresult']) * $eventcard_price ;
            }
        }
       
        $order_weight = $product_weight + $lanyards_weight + $belt_weight + $tie_weight + $fittings_weight + $sticker_card_weight + $pvc_holder_weight + $sticker_fittings_weight + $multicolor_fittings_weight + $rope_fittings_only_weight + $multicolorbelt_weight +$multicolor_tie_weight + $satin_material_weight + $polyesterbeltspecial_weight + $polyesterbelt_weight;

        $reduced_weight = 0;
        if(isset($_POST['multicolor-fittings']) && !empty($_POST['multicolor-fittings'])){
            $product_wght = get_post_meta($_POST['multicolor-fittings'][1], 'bulk_product_weight',true); 
            $reduced_weight =  $product_wght * $_POST['totalresult'];             

        }
        $order_weight = $order_weight - $reduced_weight;

        $orderdata .= '<tr><td><b>Order Weight(In gms): </b></td><td><b>'.$order_weight.'g</b></td></tr>';
        
        if($multicolor_design){
            $orderdata .= '<tr><td><b>Multicolor Lanyard Design:</b></td><td><b><a href="'.$multicolor_design.'" target="_blank" class="button button-primary button-large">Download Design</b></td></tr>';
        }
       
        if($designurl){
            $orderdata .= '<tr><td><b>Uploaded Design:</b></td><td><b><a href="'.$designurl.'" target="_blank" class="button button-primary button-large">Download Design</b></td></tr>';
        }
        if(isset($_POST['design_upload_by_email']) && !empty($_POST['design_upload_by_email'])){
            $orderdata .= '<tr><td><b>Uploaded Design Email:</b></td><td><b>'.$_POST['design_upload_by_email'].'</b><td></tr>';
        }
        if(isset($_POST['design_upload_by_link']) && !empty($_POST['design_upload_by_link'])){
            $orderdata .= '<tr><td><b>Uploaded Design Link:</b></td><td><b>'.$_POST['design_upload_by_link'].'</b><td></tr>';
        }
        $orderdata .= '</table>';

        if(isset($_POST['user_message']) && !empty($_POST['user_message'])){
            $user_message = $_POST['user_message'];
        }
        $userdata = get_userdata($current_user_id);
        
        $my_cptpost_args = array(
            'post_title'    => ($userdata->first_name) ? 'Userid-'.$userdata->ID.' / '.$userdata->first_name.' '.$userdata->last_name : 'Userid-'.$userdata->ID.' / '.$userdata->user_email,           
            'post_status'   => 'draft',            
            'post_type' => 'bulk_order');

        $dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
        
        $postid = wp_insert_post( $my_cptpost_args);

        update_post_meta( $postid, 'order_data', $orderdata );
        update_post_meta( $postid, 'order_total', $order_total );
        if($user_message) update_post_meta( $postid, 'user_message', $user_message );
        update_post_meta( $postid, 'ordered_date', $dateTime->format("d/m/y  h:i A") );
        update_post_meta( $postid, 'order_user_id', $current_user_id );
        if($_POST['delivery_through']=='Courier'){
            $shipping_by ? update_post_meta( $postid, 'shipping_info',  $_POST['delivery_through'].' - '. $shipping_info->post_title . ' - '.$shipping_by) :update_post_meta( $postid, 'shipping_info',  $_POST['delivery_through'].' - '. $shipping_info->post_title);
            //update_post_meta( $postid, 'shipping_info',  $_POST['delivery_through'].' - '. $shipping_info->post_title . ' - '.$shipping_by);
            update_post_meta( $postid, 'shipping_cost',  ceil($order_weight/1000)*$shipping_cost);
        }elseif($_POST['delivery_through']=='Direct' || $_POST['delivery_through']=='Bus'){
            update_post_meta( $postid, 'shipping_info', $_POST['delivery_through']);
            update_post_meta( $postid, 'shipping_cost',  $shipping_cost);
        }else{
            update_post_meta( $postid, 'shipping_info', $_POST['delivery_through'].' - '. $shipping_info->post_title . ' - '.$_POST['transport_payment_type']);
            update_post_meta( $postid, 'shipping_cost',  $shipping_cost);
        }
        update_post_meta( $postid, 'bulk_order_product_type',  $_POST['product_group']);
        update_post_meta( $postid, 'order_confirmation',  0);
        
        $orderconfrim = '';
        $orderconfrim .= '<table class="widefat fixed" style="margin-top: -25px;position: relative;border-top: 0;">';
        $orderconfrim .= '<tr><td><b>User Message:</b></td><td><b>'.get_post_meta($postid, 'user_message', true).'</b></td></tr>';
        $orderconfrim .= '<tr><td><b>Shipping:</b></td><td><b>'.get_post_meta($postid, 'shipping_info', true).'</b></td></tr>';
        $orderconfrim .= '<tr><td><b>Sub Total:</b></td><td><b>'.get_post_meta($postid, 'order_total', true).'</b></td></tr>';
        $orderconfrim .= '<tr><td><b>Shipping Cost:</b></td><td><b>'.get_post_meta($postid, 'shipping_cost', true).'</b></td></tr>';
        if (is_numeric(get_post_meta($postid, 'order_total', true)) && is_numeric(get_post_meta($postid, 'shipping_cost', true))) {
            $total_order = get_post_meta($postid, 'shipping_cost', true)+get_post_meta($postid, 'order_total', true);
            $orderconfrim .= '<tr><td><b>Total:</b></td><td><b>Rs. '.$total_order.'</b></td></tr>';
        }
        $orderconfrim .= '</table>';
        
        $this->sendOrderAdminEmail($orderdata, $user_message, $postid, $current_user_id);
        $this->sendOrderUserEmail($orderdata, $postid, $current_user_id);
        
        if($postid){
            echo json_encode(array('status' => true, 'data' => ['orderid' => $postid,  'orderdata' => $orderdata.$orderconfrim], 'message' => 'Your order has been placed successfully.', 'error' => false));
            die();
        }else{
            echo json_encode(array('status' => true, 'message' => 'Your order has been not placed. Please try again.', 'error' => true));
            die();
        }
    }

    public function sendOrderAdminEmail($orderdata,$user_message,$orderid, $user_id){
        $userdata = get_userdata($user_id);
        $to  = pods_field_display('bulk_products_settings', false, 'bulk_order_admin_email');
        $subject = 'New Order Received - Order #'. $orderid;
        //$headers = 'From: info@'. $_SERVER['HTTP_HOST'] . "\r\n" .
        $headers = 'From: dinesh-test@web-trendz.com'."\r\n" .
            'Reply-To: ' . $userdata->user_email . "\r\n".
            'Content-Type: text/html'."\r\n";
        $adminBody = '<p>Dear Admin,</p>';
        $adminBody .= '<p>You have received the new Order #'. $orderid .'.</p>';
        $adminBody .= '<p><b>Order Details:</b></p>';
        $adminBody .= $orderdata;
        if($user_message)
        $adminBody .= '<p><b>User Message: </b>'.$user_message.'</p>';
        $adminBody .= '<p>Thanks & Regards,</p>';
        $adminBody .= '<p>Superidcards</p>';
        $sent = wp_mail($to, $subject, $adminBody, $headers);
    }

    public function sendOrderUserEmail($orderdata, $orderid, $user_id){
        $adminemail  = pods_field_display('bulk_products_settings', false, 'bulk_order_admin_email');
        $userdata = get_userdata($user_id);
        $to  = $userdata->user_email;
        $subject = 'New Order Received - Order #'. $orderid;
        $headers = 'From: dinesh-test@web-trendz.com'."\r\n" .
            'Reply-To: ' . $adminemail . "\r\n".
            'Content-Type: text/html'."\r\n";
        $userBody = '<p>Dear '.$userdata->first_name.' '.$userdata->last_name.',</p>';
        $userBody .= '<p>Your order has been received. Your order details are shown below:</p>';
        $userBody .= '<p style="font-size:20px"><b>Order #'.$orderid.' ('.date("d/m/y").')</b></p>';
        $userBody .= $orderdata;
        $userBody .= '<p>Thanks & Regards,</p>';
        $userBody .= '<p>Superidcards</p>';
        $sent = wp_mail($to, $subject, $userBody, $headers);
    }


}