Thursday, October 10, 2019

Single product page add custom file call in cart page woocommercewordpress

/* single product price pass to checkout*/

add_filter('woocommerce_add_cart_item_data','wdm_add_item_data',3,10);
function wdm_add_item_data($cart_item_data, $product_id) {

    global $woocommerce;
    $new_value = array();
    $new_value['_custom_options'] = $_POST['total_price_new'];
   // $new_value['_custom_options'] = $_POST['total_price_new'];
    $new_value1=$new_value['_custom_options']; //our price
    //print_r($new_value1);die('jjjjj');
    global $new_value1;
    /*echo "<pre>";
    print_r($cart_item_data);
    print_r($new_value);
    print_r($_POST); die('grgrge');
*/
    if(empty($cart_item_data)) {
        return $new_value;
    } else {
        return array_merge($cart_item_data, $new_value);
    }
      }


      Add this function in function.php file

add_action( 'woocommerce_before_calculate_totals', 'update_custom_price' );
function update_custom_price( $cart_object ) {
    foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {
        // Version 2.x
        //$value['data']->price = $value['_custom_options']['custom_price'];
        // Version 3.x / 4.x
        //$value['data']->set_price($value['_custom_options']['total_price_new']);
        $value['data']->set_price($value['_custom_options']);
    }
}      
   
   
  add_filter('woocommerce_cart_item_name','add_usr_custom_session',1,3); 
function add_usr_custom_session($product_name, $values, $cart_item_key ) {  
    $return_string = $product_name. "<br />" ."Start Date: " . $values['date_start']. "<br />" ."End Date: ". $values['date_end']. "<br />" ."Adult: ". $values['adult_count']. "<br />" ."Child with Bed: ". $values['adultbed_count']. "<br />" ."Child Without Bed: ". $values['adultwithoutbed_count'];// . "<br />" . print_r($values['_custom_options']);
    return $return_string;  

}  
    

1 comment: