Pages

Tuesday, December 11, 2012

Oscommerce Add products to cart manually

In order to add products manually by Id or customize cart manually in OSCommerce

we need to first include the following file

require("includes/application_top.php")

What  this does is automatically includes the following class

require(DIR_WS_CLASSES . 'shopping_cart.php');(line 126)

and instantiates an object of class

if (!tep_session_is_registered('cart') || !is_object($cart)) {
    tep_session_register('cart');
    $cart = new shoppingCart; 
} 
or you can yourself include these files and create an object
But for any action in oscommerce I prefer to include application_top.php
If you are building up a new cart add
$cart->reset();
This will reset your cart which is only required if you are planning to add a new cart in my case I was sending all Items from another website to my oscommerce website so I decided to reset cart first

Now let us suppose we have item information in an array $items

foreach($items as $item)
{
       $attributes = isset($HTTP_POST_VARS['id']) ? $HTTP_POST_VARS['id'] : '';
       $cart->add_cart($item['id'],$item['quant'],$attributes);
      //In case cart is not empty and you want to add quantity to an item
      $cart->add_cart(
$item['id'], $cart->get_quantity('$item['id']')+1);
}

Then add the following lines
//Cleans any product with quantity zero
$cart->cleanup();
//Calculates the weight and total cost in all finalizes your cart
$cart->calculate();

That's All

1 comment:

  1. Good article

    https://wordpress-special-plugin.blogspot.co.id

    ReplyDelete