A
A
Anton2021-07-08 17:26:31
Kohana
Anton, 2021-07-08 17:26:31

Cart, delivery method and price, post method, transfer to the database when ordering, how to transfer the delivery price?

Hello. (lack of knowledge).

There is a basket, it has a choice of delivery, delivery options have a price.
From the basket, I transfer the choice of delivery, but I can’t transfer the price of delivery yet.

Conclusion of delivery methods and selection.

<h2>Выберите способ доставки</h2>
          <ul class="ac-custom ac-radio crzsm">
            <?php foreach (ORM::factory('Delivery')->find_all() as $del) : ?>
              <li><input type="radio" name="delivery" id="radio<?= $del->id ?>" value="<?= $del->name ?>"><label for="radio<?= $del->id ?>"><?= $del->name ?>
                  <? if ($del->cost > 0) : ?>
                    - <b><?= $del->cost ?> </b> руб
                  <?php endif ?></label></li>
            <?php endforeach ?>
          </ul>


When ordering, I send and receive. Further, in the same place I try to pull out the price.
$delivery = Arr::get($_POST, 'delivery');


подключаюсь к таблице и прикрепляюсь к выбранному варианту доставки $delivery, чтоб с этого варианта вытянуть цену
$del = ORM::factory('Delivery', $delivery);

Вывожу цену
$del['cost'];


How to check whether the price is displayed or not? somehow through a print it is possible, but how?

Next, I want to record the received price together with the order in the orders table, so that later I can display this price in orders.
But the price does not come to the table, so I'm trying to understand whether I display the price higher or not, maybe it just doesn't transfer it to the table.
$order->set('deliveryСost', $del['cost']);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2021-07-11
Websaytovsky @ws17

How I solved the problem.

<ul class="ac-custom ac-radio crzsm">
            <input type="hidden" id="deliveryCost" name="deliveryCost" value="">

            <?php foreach (ORM::factory('Delivery')->find_all() as $del) : ?>
              <li><input type="radio" name="delivery" id="radio<?= $del->id ?>" data-price="<?= $del->cost ?>" value="<?= $del->name ?>"><label for="radio<?= $del->id ?>"><?= $del->name ?>
                  <?php if ($del->cost): ?>
                    - <b><?= $del->cost ?></b> руб
                  <?php endif ?></label>
              </li>
            <?php endforeach ?>
          </ul>

Added a hidden imput to which I pass the price.
Added data-price to the imput, where I display the price of this radio button selection.
Skipt, for pulling out the price from data-price and transferring it to a hidden imput, so that later this price can be pulled out through the post method.
<script>
  const $deliveryCost = data => document.querySelector('#deliveryCost').value = data;

  let $selectors = document.querySelectorAll('.crzsm input[type="radio"]');
  $selectors.forEach($radio => {
    $radio.addEventListener('change', function() {
      $deliveryCost(this.getAttribute('data-price'));
    });
  });
</script>

Next, I transfer the price from the hidden imput to the controller.
$deliveryCost = Arr::get($_POST, 'deliveryCost');
Well, below is where I make an entry in the database in the order table, added.
$order->set('deliveryCost', $deliveryCost);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question