V
V
Vladimir Terechov2018-11-30 15:05:03
OctoberCMS
Vladimir Terechov, 2018-11-30 15:05:03

How to render a fragment?

In general, the essence is as follows, there is a basket, there is a button to add to basket. On the button weighs data-request = "onAddToCart"
Below is onAddToCart itself

public function onAddToCart(){
      $product = \vladimir\catalog\Models\Product::find( Input::get('id') );

      if (Input::get('qty') !== null){
        $qty = Input::get('qty');
      }

      $item = new \stdClass();
      $item->id = $product->id;
      $item->name = $product->name;
      $item->price = $product->price;
      $item->img = $product->images[0]->path;
      $item->qty = count($this->getItems());

      Session::push('cart', $item);

      return [
        '@.cart-items' => $this->renderPartial('@cart-item.htm', ['item' => $item]),
        '.total-price' => $this->renderPartial('@cart-price.htm', ['item' => $this->Total()]),
        '.qty-cart'    => $this->renderPartial('@qty-cart.htm', ['qty' => count($this->getItems())])
      ];
    }

everything works fine, when I click on the button, a product object is added to the session, and everything is updated and rendered.
But I need to be able to delete one item from the cart by clicking on the Delete
button. There is a Delete button, on it data-request="onClearOneItemCart"
Below is onClearOneItemCart itself
public function onClearOneItemCart(){
       foreach ($this->getItems() as $key => $value) {
         if($value->id == Input::get('id')){
           Session::pull('cart.'.$key);
         }
       }
       return [
          '@.cart-items' => $this->renderPartial('@cart-item.htm')
       ];
     }

here I loop through the session and look for the desired object to be deleted by its id
. Everything also works, but it is deleted only after a reboot, and I need it to be deleted and rendered immediately.
Please tell me how to implement?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question