N
N
noteblock2016-08-29 12:24:37
PHP
noteblock, 2016-08-29 12:24:37

How to update session variables on the fly?

Good afternoon.
I am writing a module in which it is required to store a variable in the session.
The variable is updated at the address of the form: sitename.ru/updatevar/var_value (at this address, the variable is assigned the value var_value)
Faced such a problem - the value is updated only when the page is refreshed again.
Those. if I go to the following addresses and display the value of the variable, I see:
1) sitename.ru/updatevar/1 // nothing
2) sitename.ru/updatevar/2 // 1
2) sitename.ru/updatevar/3 // 2
and so on .d.
Tell me where to dig, I spent a lot of time on independent searches for an answer, but nothing.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
noteblock, 2016-08-29
@noteblock

Module code:

<?php

function ajaxcartrefresh_menu(){
  $items = array();
  $items['updatecart/%/%'] = array(
    'page callback' => 'ajaxcartrefresh_updater', // Render HTML
    'page arguments' => array(1,2),
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),  
  );
  return $items;
}

function ajaxcartrefresh_updater($order,$cost){
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  commerce_cart_order_refresh($order_wrapper);
  $_SESSION['bxbr'] = $cost;
}

print $_SESSION['bxbr'];

function ajaxcartrefresh_commerce_order_presave($order) {
  $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
  $line_items_quantity = commerce_line_items_quantity($order_wrapper->commerce_line_items, commerce_product_line_item_types());
  
  $discount_decimal = $_SESSION['bxbr'];
  $discount_currency_code = 'RUB';
  
  $order_total = $order_wrapper->commerce_order_total->value();
  $discount_amount = commerce_currency_decimal_to_amount($discount_decimal, $discount_currency_code);
  $discount_amount = commerce_currency_convert($discount_amount, $discount_currency_code, $order_total['currency_code']);
  
  $order_wrapper->commerce_order_total->amount = $order_total['amount'] + $discount_amount;
  $order_wrapper->commerce_order_total->data = commerce_price_component_add($order_total, 'fee', array(
    'amount' => $discount_amount,
    'currency_code' => $order_total['currency_code'],
    'data' => array(),
  ), TRUE, FALSE);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question