Answer the question
In order to leave comments, you need to log in
Why is the variable defined as unknown?
Good day!
The telegram bot connected with the store, the task of the bot is to notify about new orders. Faced a problem, an unknown variable.
public function actionCheckout()
{
if (empty($_SESSION['cart'])) {
return $this->redirect(['site/error']);
}
$model = new Orders();
if ($model->load(Yii::$app->request->post())) {
$model->total_sum = $_SESSION['cart.sum'];
if ($model->validate() && $model->save()) {
$token = "1824070568:AAGZbzn4rlXr17YJWQkGWuWgJjyEXTE9gVU";
$chat_id = "-548130760";
$arr = array('<b><a href="https://asp-polimer.uz/admin/orders/view?id=' . $model->order_id . '"> Новый заказ #' . $model->order_id . '</a></b>' => '', '♂️ Имя:' => $model->costumer_name, '☎️ Телефон: ' => $model->costumer_phone, ' Дата: ' => date('d.m.Y в H:i'), ' Сумма: ' => number_format($model->total_sum, 2, '.', ' ') . ' сум',);
foreach ($_SESSION['cart'] as $item) {
$ar[' Товар: '][] = $item['name'] . ' ' . $item['qty'] . ' шт * ' . number_format(ceil($item['price']), 0, '.', ' ') . ' ₸';
$order_items = new OrdersItems();
$order_items->order_id = $model->order_id;
$order_items->product_id = $item['product_id'];
$order_items->product_code = $item['product_code'];
$order_items->product = $item['name'];
$order_items->qty = $item['qty'];
$order_items->price = $item['price'];
$order_items->total_sum = $item['price'] * $item['qty'];
$order_items->save();
}
foreach ($arr as $key => $value) {
$txt .= "<b>" . $key . "</b> " . $value . "\n";
};
foreach ($ar as $key => $value) {
if (is_array($value)) {
foreach ($value as $tovs) {
$txt .= "<b>" . $key . "</b> " . $tovs . "\n";
}
}
};
$txt = rawurlencode($txt);
$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}", "r");
$session = Yii::$app->session;
$session->open();
$session->remove('cart');
$session->remove('cart.qty');
$session->remove('cart.sum');
return $this->redirect(['cart/order-success']);
} else {
Yii::$app->session->setFlash('error', "Ошибка! Что-то пошло не так...");
}
}
return $this->render('checkout', ['model' => $model]);
}
Answer the question
In order to leave comments, you need to log in
Expand the record, and it will be clear:
$txt = $txt /* вот в этом месте $txt используется, но еще не существует */
. "<b>" . $key . "</b> " . $value . "\n";
$txt = ''; // нужно сначала инициализировать
foreach ($arr as $key => $value) {
$txt .= "<b>" . $key . "</b> " . $value . "\n";
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question