Need help with Basket

174 views
Skip to first unread message

Peppe

unread,
Oct 21, 2014, 4:58:24 AM10/21/14
to f3-fra...@googlegroups.com
Hi,
I have a controller
 public function products_cart()
    {   
        $product = new Products( $this->db );
        $basket = new \Basket(  'shop_cart'  );
    
        if($this->f3->exists('POST.id'))
        {           
            $id = $this->f3->get('POST.id');
            $amount = $this->f3->get( 'POST.amount' );
            $basket->set( 'product_id',  $id );
            $basket->set( 'product_amount', $amount );
            //I get price from model
            $total_db_price = $product->GetBasketTotalPrice( $id, $amount );
            $basket->set( 'price', $total_db_price );
            $basket->save();
            $basket->reset();           
        }
       
        if(  $basket->count() > 0 )
        {           
            $result = $basket->find();
            print "<pre>";
                 var_dump($result);
            print "</pre>";

      This is the result
   
array(3) { [0]=> object(Basket)#10 (3) { ["key":protected]=> string(9) "shop_cart" ["id":protected]=> string(23) "54461bb610e9c5.00404527" ["item":protected]=> array(3) { ["product_id"]=> string(1) "1" ["product_amount"]=> string(1) "1" ["price"]=> float(255) } } [1]=> object(Basket)#11 (3) { ["key":protected]=> string(9) "shop_cart" ["id":protected]=> string(23) "54461bbccef307.95145353" ["item":protected]=> array(3) { ["product_id"]=> string(2) "14" ["product_amount"]=> string(1) "1" ["price"]=> float(479) } } [2]=> object(Basket)#12 (3) { ["key":protected]=> string(9) "shop_cart" ["id":protected]=> string(23) "54461c03913161.38059218" ["item":protected]=> array(3) { ["product_id"]=> string(1) "1" ["product_amount"]=> string(1) "1" ["price"]=> float(255) } } }

}

        die;
    }

I need help to make total price from 3 product or total amount of products or explanation how to loop through Basket object and the insert items in table basket.
Is this the right way to do?
Thank you in advance

ikkez

unread,
Oct 21, 2014, 7:59:40 AM10/21/14
to f3-fra...@googlegroups.com
yeah it is... to count the amout or price just foreach through them:

$total_amount = 0;
$total_price
= 0;

foreach($basket->find() as $prod) {
  $total_amount
+= $prod->product_amount;
  $total_price
+= ($prod->price * $prod->product_amount);
}

Peppe

unread,
Oct 21, 2014, 9:24:08 AM10/21/14
to


Op dinsdag 21 oktober 2014 13:59:40 UTC+2 schreef ikkez:





Hi ikkez,
Thank you for your response and time.
I'm getting this error Undefined property: Basket::$product_amount.

ikkez

unread,
Oct 21, 2014, 9:52:41 AM10/21/14
to f3-fra...@googlegroups.com
ah yeah, sorry... no magic methods here. use
$prod->get('product_amount');
instead


Am Dienstag, 21. Oktober 2014 15:24:08 UTC+2 schrieb Peppe:


Op dinsdag 21 oktober 2014 13:59:40 UTC+2 schreef ikkez:
yeah it is... to count the amout or price just foreach through them:

Peppe

unread,
Oct 21, 2014, 10:19:40 AM10/21/14
to f3-fra...@googlegroups.com
Thank you very much :). That works.
I have one more question.
Is it possible to save Basket in mysql table, when the user comes back later he can see what he orderd earlier. I have user_id  because I work with logged users.


Op dinsdag 21 oktober 2014 15:52:41 UTC+2 schreef ikkez:

ikkez

unread,
Oct 22, 2014, 11:51:41 AM10/22/14
to f3-fra...@googlegroups.com
no you need to copy the basket items into your mysql mapper yourself and save them.
But if you order that basket items, i guess you need to do that somewhere anyways.

karthick b

unread,
Sep 28, 2016, 3:53:37 PM9/28/16
to Fat-Free Framework
HI Ikkez,

I don't know if it is appropriate to continue in this thread. But anyway, i am posting to here to maintain the continuity. Please forgive me if I am wrong.

    $cart = new \Basket( 'cart' );
        $orderItems
= new DB\SQL\Mapper($this->db,'order_items');
       
foreach($cart->find() as $items) {
          $orderItems
->quantity = $items->get('quantity');
            $orderItems
->product_id = $items->get('id');
           
// this loop is to insert order table Pk
           
// into orderItems table as FK
            $orderItems
->order_id = $order_id;
            $orderItems
->save();
           
}
          $cart
->checkout();

I am trying to write the contents of my cart and one additional field (order_id) to the (order_Items) Table. However if there are multiple basket items I am able to save only one record. I think the Cursor is not moving after saving or the foreach loops only one time. Can you please help me to resolve this issue.

Thank you in anticipation.

karthick b

unread,
Sep 29, 2016, 5:56:17 AM9/29/16
to Fat-Free Framework
Hello Guys,

Feeling kind of struck here. Could anyone please look into this. Thank you guys.

Br
B Karthick

ikkez

unread,
Sep 29, 2016, 6:11:31 AM9/29/16
to Fat-Free Framework
after you save in the foreach loop, you should call reset, otherwise you'll only update the existing record every time.

$orderItems
->save();
$orderItems->reset();

karthick b

unread,
Sep 29, 2016, 8:52:42 AM9/29/16
to Fat-Free Framework
Dear ikkez,

Thank you very much. It worked flawlessly.

Best Regards
Reply all
Reply to author
Forward
0 new messages