Esempio n. 1
0
 public static function createCartFromContainer($productContainer)
 {
     $ShoppingCart = new ShoppingCart();
     if ($productContainer instanceof ProductContainer) {
         $products = $productContainer->getAllProducts();
         foreach ($products as $product) {
             $ShoppingCart->addProduct($product);
         }
         return $ShoppingCart;
     } else {
         throw new Exception("Error, unable to create cart from container. Container is not a Product Container");
     }
 }
Esempio n. 2
0
 case "product":
     $product_id = isset($_GET['product_id']) ? $_GET['product_id'] : "";
     $product_data = $products->loadProduct($product_id);
     if (empty($product_data)) {
         die("Invalid product ID!");
     }
     $smarty->assign("product", $product_data);
     $smarty->display("product.tpl");
     break;
     // add to shopping cart
 // add to shopping cart
 case "add_to_cart":
     if (isset($_POST['product_id'])) {
         // we always add one piece for now
         $qt = isset($_POST['qt']) ? $_POST['qt'] : 1;
         $shopping_cart->addProduct($_POST['product_id'], $qt);
     }
     // we don't have the break here on purpose so that the code continues
     // with displaying the contents of the shopping cart
     //break;
     // shopping cart
 // we don't have the break here on purpose so that the code continues
 // with displaying the contents of the shopping cart
 //break;
 // shopping cart
 case "show_cart":
     $product_list = $shopping_cart->getContents();
     $smarty->assign("products", $product_list);
     $smarty->assign("items", $products->listProducts());
     $smarty->display("cart.tpl");
     break;
Esempio n. 3
0
        echo "Adding " . $product_id . " x " . $qt . "<br>";
        $old_qt = 0;
        if (array_key_exists($product_id, $_SESSION['products'])) {
            $old_qt = $_SESSION['products'][$product_id];
        }
        $_SESSION['products'][$product_id] = $old_qt + $qt;
    }
    function removeProduct($product_id)
    {
        echo "Removing " . $product_id . "<br>";
        if (array_key_exists($product_id, $_SESSION['products'])) {
            unset($_SESSION['products'][$product_id]);
        }
    }
    function listProducts()
    {
        echo "Listing products" . "<br>";
        foreach ($_SESSION['products'] as $product_id => $qt) {
            echo $product_id . " x " . $qt . "<br>";
        }
    }
}
// init session
session_start();
// Testing
$sh = new ShoppingCart();
$sh->addProduct("123", 2);
$sh->addProduct("345", 1);
$sh->addProduct("678", 1);
$sh->removeProduct("345");
$sh->listProducts();
Esempio n. 4
0
    public function findProductByName($name)
    {
        $itemKey = null;
        foreach ($this->items as $key => $item) {
            if ($name == $item->getName()) {
                $itemKey = $key + 1;
                echo "{$name} is number {$itemKey} in the array";
                break;
            }
        }
        if ($itemKey == null) {
            throw new Exception("Item was not found in cart");
        }
    }
}
$describer = new ItemDescriber();
$kramericaTV = new Television("Giant TV", "Kramerica", 3900.9, "LED", "100in");
$shirt = new Clothing("Button Down Shirt", "J Peterman", '29.88', "shirt", "medium", "red", "male");
$cart = new ShoppingCart();
$cart->addProduct($kramericaTV);
$cart->addProduct($shirt);
$cart->provideDescription();
echo "</br>";
echo "The cart total price is \$" . $cart->getTotalPrice();
$cart->removeOne($shirt);
$cart->findProductByName("Giant TV");
$describer->outputDescription($cart);
?>
    </p>
  </body>
</html>
if (isset($_SESSION["username"])) {
    require_once "Class/ShoppingCart.php";
    /* load or initialize the ShoppingCart */
    if (isset($_SESSION["cart_product_id_array"])) {
        $shopping_cart = new ShoppingCart($_SESSION["username"], $_SESSION["cart_product_id_array"], $_SESSION["cart_product_amount_array"], $_SESSION["cart_product_hint_array"]);
    } else {
        $shopping_cart = new ShoppingCart($_SESSION["username"]);
    }
    /* if there are any shopping_cart actions, do it now */
    $form_action = var_get_post("action", "");
    if ($form_action == "add_product_to_cart") {
        $form_product_id = var_get_post("product_id", "");
        $form_product_amount = var_get_post("amount", "");
        $form_product_amount = str_replace(',', '.', $form_product_amount);
        if ($form_product_id != "" && $form_product_amount != "") {
            $shopping_cart->addProduct($form_product_id, $form_product_amount, "");
        }
    } elseif ($form_action == "remove_product_from_cart") {
        $form_product_id = var_get_post("product_id", "");
        if ($form_product_id != "") {
            $shopping_cart->removeProduct($form_product_id);
        }
    } elseif ($form_action == "edit_product_amount") {
        $form_product_id = var_get_post("product_id", "");
        $form_product_amount = var_get_post("product_amount", "");
        $form_product_amount = str_replace(',', '.', $form_product_amount);
        $form_product_hint = var_get_post("product_hint", "");
        if ($form_product_id != "" && $form_product_amount != "") {
            $shopping_cart->editProduct($form_product_id, $form_product_amount, $form_product_hint);
        }
    } elseif ($form_action == "create_bill") {
Esempio n. 6
0
        if (empty($this->size)) {
            throw new Exception("No size entered");
        } else {
            return $this->size;
        }
    }
    public function provideDescriptionForProductType()
    {
        return 'This is a ' . $this->getSize() . ' ' . $this->getBrand() . ' ' . $this->getname() . ' ' . $this->getDisplayType() . ' Television <br />';
    }
}
$itemDescriber = new ItemDescriber();
$cart = new ShoppingCart();
$hoodie = new Clothing("Hoodie", "Nike", 19.99, "large", "red", "shirt", "male");
$plasma = new Television("Plasma", "Sony", 1000.0, plasma, "50in");
$cart->addProduct($hoodie);
$cart->addProduct($plasma);
echo implode('<br />', $cart->provideDescription());
echo "<br />";
echo $cart->getTotalPrice();
echo "<br />";
$cart->removeProduct($hoodie);
foreach ($cart->provideDescription() as $productDescription) {
    echo $productDescription;
    echo '<br />';
}
var_dump($cart->findProductByName("Plasma"));
var_dump($itemDescriber->outputDescription($cart));
?>
    </p>
  </body>