We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Anyone with a sample cart implemented using phalcon?

I have this code that displays a list of items on a UI. In have a create function that saves data to a database. However, only one item is saved. I need to update the entire list of user selction. Any help. Will really appreciate? I am new in this. Thanks. UI <table class="order-list"> <thead> <tr> <td></td> <td></td> <td>Qty</td> <td>Total</td> </tr> </thead>

<tbody>

    <?php
    if(isset($page->items)){
        foreach($page->items as $products){ ?>

     <tr>
        <?php echo \Phalcon\Tag::form(array("shoppingcart/create", "autocomplete" => "off")) ?>   

        <td>

        <input type="text" name="product_name" value="<?php echo $products->brand." ".$products->sku ?>"/> </td>
        <td><input type="text" name="price" value="<?php echo $products->retail_price ?>"/></td>
        <td><input type="text" name="qty" placeholder="Enter Quantity Here"/></td>
        <td><input type="text" name="linetotal"/></td>
        <td><input type="hidden" size="5" readonly="readonly" name="product_id" value="<?php echo $products->id?>"/></td>
        <td align="right"><?php echo \Phalcon\Tag::submitButton(array("Submit", "class" => "btn btn-success")) ?></td> 

             </tr>
                    <?php }
    } ?>
     <!--td align="right"><?php echo \Phalcon\Tag::submitButton(array("SubmitOrder", "class" => "btn btn-success")) ?></td--> 
</tbody>

Controller
public function createAction()
{

    if (!$this->request->isPost()) {
        return $this->dispatcher->forward(array(
            "controller" => "shoppingcart",
            "action" => "index"
        ));
    }

    $dateCreated = $this->dateCreated = date('Y-m-d H:i:s');
    $auth = $this->session->get('auth');
    $customer_phone = $this->session->get('phone_number');
    $product_id = $this->request->getPost("product_id");

    $order = new orders();
    //for ($i=0; $i < $product_id; $i++) { 
        # code...
    foreach($order as $order->product_id) {
        # code...
   // }

    $order->agent_id = $auth;
    $order->price = $this->request->getPost("price");
    $order->phone = $customer_phone;
    $order->product_id = $this->request->getPost("product_id");
    $order->product_name = $this->request->getPost("product_name");
    $order->quantity = $this->request->getPost("qty");       
    $order->dateCreated = $dateCreated;

    //if (!$order->save(array("data" => $order)))
        if(!$order->save()) 

    {
        foreach ($order->getMessages() as $message) {
            $this->flash->error((string) $message);

             $this->flash->error((string) $customer_phone);
        }
        return $this->dispatcher->forward(array(
            "controller" => "shoppingcart",
            "action" => "index"
        ));

    } else {

        $this->flash->success("Order Created Successfully");
        return $this->dispatcher->forward(array(
            "controller" => "shoppingcart",
            "action" => "index"
        ));

}

}

}


33.8k

I only see saving one orders there. You need to do a foreach to get every item in the UI and save it.

Use manual transactions to do that, so you ensure that all data is saved, and not just a few items.



1.3k

Kindly provide some guidance on the same. I am still new in this



33.8k

Inside a foreach, do the piece of code that starts in $order->agent_id = X; $order->price = X; .... Then, at the end of the loop, call save().