PHP - Magento - How To Restrict Shipping Method Based On Product In The Cart?
I have couple of shipping methods DHL & FedEx, if selected products exits in cart show only selected shipping method, how to archive using shopping cart pricing rule or any other way to do this?
Similar TutorialsI have one restricted product, here how to do selected single product should be purchased alone? No other products are eligible to add add-to-cart if this product available in the add-to-cart, same if other products available in add-to-cart this product not eligible to add add-to-cart. How to achieve this in Magento? Created module like No other products add to cart if restricted product available in cart and vice versa. My Module : app/etc/modules/Brst_Test.xml<?xml version="1.0"?> <config> <modules> <Brst_Test> <active>true</active> <codePool>community</codePool> </Brst_Test> </modules> </config> This is my observer file app/code/community/Brst/Test/Model/Observer.php<?php ini_set('display_errors', '1'); // Mage::log('Hy observer called', null, 'logfile.log'); class Brst_Test_Model_Observer { //Put any event as per your requirement public function logCartAdd() { $product = Mage::getModel('catalog/product') ->load(Mage::app()->getRequest()->getParam('product', 0)); $cart_qty = (int) Mage::getModel('checkout/cart')->getQuote()->getItemsQty(); if ($product->getId()==31588 && cart_qty > 0) { Mage::throwException("You can not add This special Product, empty cart before add it"); } // $quote = Mage::getSingleton('checkout/session')->getQuote(); // if ($quote->hasProductId(2)) //{ // Mage::getSingleton("core/session")->addError("Cart has Special Product you can not add another"); // return; // } $quote = Mage::getModel('checkout/cart')->getQuote(); foreach ($quote->getAllItems() as $item) { $productId = $item->getProductId(); if($productId==31588){ Mage::throwException("Cart has Special Product you can not add another"); } } } } ?> app/code/community/Brst/Test/etc/config.xml <?xml version="1.0"?> <config> <modules> <Brst_Test> <version>0.1.0</version> </Brst_Test> </modules> <global> <models> <brst_test> <class>Brst_Test_Model</class> </brst_test> </models> </global> <frontend> <events> <controller_action_predispatch_checkout_cart_add> <observers> <brst_test_log_cart_add> <class>brst_test/observer</class> <method>logCartAdd</method> </brst_test_log_cart_add> </observers> </controller_action_predispatch_checkout_cart_add> </events> </frontend> </config>
Not working, how to solve the error? Hi there, I am working on an e-commerce website on PHP and MySql. The website has products that suppliers can add and edit, which works fine. Now we are required to create and add a shipping profile to the products. That is, a supplier can set up a profile where he'll specify, for example: Setting Shipping charges of Product weight $10/kg for US shipping $20/Kg for Europe $25/Kg for Rest of the world. Once this profile is setup, I would need to attach this profile to a product. Well, I can create a table named ProductProfile and enter this weight info and how do I go about attaching this to a product. Any comment/feedback are always welcome! Thank you This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=357747.0 I am using extra fees extension, if customer chooses shipping method: Pick from the store how to remove Handling fees(extra fees extension).
How to solve by using observer? Edited August 12, 2019 by aveevaI got a problem guys, when I wanna add a product to my cart it wont add the product to my cart. I get send to my cart and than there is no product. http://www.esolarcharger.com/new Product.php Code: [Select] <?php // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); ?> <?php // Check to see the URL variable is set and that it exists in the database if (isset($_GET['id'])) { // Connect to the MySQL database include "storescripts/connect_to_mysql.php"; $id = preg_replace('#[^0-9]#i', '', $_GET['id']); // Use this var to check to see if this ID exists, if yes then get the product // details, if no then exit this script and give message why $sql = mysql_query("SELECT * FROM products WHERE id='$id' LIMIT 1"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { // get all the product details while($row = mysql_fetch_array($sql)){ $product_name = $row["product_name"]; $price = $row["price"]; $details = $row["details"]; $category = $row["category"]; $subcategory = $row["subcategory"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); } } else { echo "That item does not exist."; exit(); } } else { echo "Data to render this page is missing."; exit(); } mysql_close(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><?php echo $product_name; ?></title> <link rel="stylesheet" href="style/style.css" type="text/css" media="screen" /> </head> <body> <div align="center" id="mainWrapper"> <?php include_once("template_header.php");?> <div id="pageContent"> <table width="100%" border="0" cellspacing="0" cellpadding="15"> <tr> <td width="19%" valign="top"><img src="inventory_images/<?php echo $id; ?>.jpg" width="142" height="188" alt="<?php echo $product_name; ?>" /><br /> <a href="inventory_images/<?php echo $id; ?>.jpg">View Full Size Image</a></td> <td width="81%" valign="top"><h3><?php echo $product_name; ?></h3> <p><?php echo "$".$price; ?><br /> <br /> <?php echo $details; ?> <br /> </p> <form id="form1" name="form1" method="post" action="cart.php"> <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" /> <input type="submit" name="button" id="button" value="Add to Shopping Cart" /> </form> </td> </tr> </table> </div> <?php include_once("template_footer.php");?> </div> </body> </html> Cart.php Code: [Select] <?php ob_start(); // Start session first thing in script // Script Error Reporting error_reporting(E_ALL); ini_set('display_errors', '1'); // Connect to the MySQL database include "storescripts/connect_to_mysql.php"; ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 1 (if user attempts to add something to the cart from the product page) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['pid'])) { $pid = $_POST['pid']; $wasFound = false; $i = 0; // If the cart session variable is not set or cart array is empty if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { // RUN IF THE CART IS EMPTY OR NOT SET $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity" => 1)); } else { // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $pid) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1))); $wasFound = true; } // close if condition } // close while loop } // close foreach loop if ($wasFound == false) { array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1)); } } header("location: cart.php"); exit(); } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 2 (if user chooses to empty their shopping cart) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_GET['cmd']) && $_GET['cmd'] == "emptycart") { unset($_SESSION["cart_array"]); } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 3 (if user chooses to adjust item quantity) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['item_to_adjust']) && $_POST['item_to_adjust'] != "") { // execute some code $item_to_adjust = $_POST['item_to_adjust']; $quantity = $_POST['quantity']; $quantity = preg_replace('#[^0-9]#i', '', $quantity); // filter everything but numbers if ($quantity >= 100) { $quantity = 99; } if ($quantity < 1) { $quantity = 1; } if ($quantity == "") { $quantity = 1; } $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $i++; while (list($key, $value) = each($each_item)) { if ($key == "item_id" && $value == $item_to_adjust) { // That item is in cart already so let's adjust its quantity using array_splice() array_splice($_SESSION["cart_array"], $i-1, 1, array(array("item_id" => $item_to_adjust, "quantity" => $quantity))); } // close if condition } // close while loop } // close foreach loop } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 4 (if user wants to remove an item from cart) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") { // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 5 (render the cart for the user to view on the page) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $cartOutput = ""; $cartTotal = ""; $pp_checkout_btn = ''; $product_id_array = ''; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { // Start PayPal Checkout Button $pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="upload" value="1"> <input type="hidden" name="business" value="support@tranceprofile.com">'; // Start the For Each loop $i = 0; foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)) { $product_name = $row["product_name"]; $price = $row["price"]; $details = $row["details"]; } $pricetotal = $price * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; setlocale(LC_MONETARY, "en_US"); $pricetotal = money_format("%10.2n", $pricetotal); // Dynamic Checkout Btn Assembly $x = $i + 1; $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"> <input type="hidden" name="amount_' . $x . '" value="' . $price . '"> <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> '; // Create the product array variable $product_id_array .= "$item_id-".$each_item['quantity'].","; // Dynamic table row assembly $cartOutput .= "<tr>"; $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>'; $cartOutput .= '<td>' . $details . '</td>'; $cartOutput .= '<td>$' . $price . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"> <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> <input name="adjustBtn' . $item_id . '" type="submit" value="change" /> <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> </form></td>'; //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>'; $cartOutput .= '<td>' . $pricetotal . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $cartOutput .= '</tr>'; $i++; } setlocale(LC_MONETARY, "en_US"); $cartTotal = money_format("%10.2n", $cartTotal); $cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>"; // Finish the Paypal Checkout Btn $pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '"> <input type="hidden" name="notify_url" value="https://www.tranceprofile.com/storescripts/my_ipn.php"> <input type="hidden" name="return" value="https://www.tranceprofile.com/checkout_complete.php"> <input type="hidden" name="rm" value="2"> <input type="hidden" name="cbt" value="Return to The Store"> <input type="hidden" name="cancel_return" value="https://www.tranceprofile.com/paypal_cancel.php"> <input type="hidden" name="lc" value="US"> <input type="hidden" name="currency_code" value="USD"> <input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!"> </form>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Your Cart</title> <link rel="stylesheet" href="style/style.css" type="text/css" media="screen" /> </head> <body> <div align="center" id="mainWrapper"> <?php include_once("template_header.php");?> <div id="pageContent"> <div style="margin:24px; text-align:left;"> <br /> <table width="100%" border="1" cellspacing="0" cellpadding="6"> <tr> <td width="18%" bgcolor="#C5DFFA"><strong>Product</strong></td> <td width="45%" bgcolor="#C5DFFA"><strong>Product Description</strong></td> <td width="10%" bgcolor="#C5DFFA"><strong>Unit Price</strong></td> <td width="9%" bgcolor="#C5DFFA"><strong>Quantity</strong></td> <td width="9%" bgcolor="#C5DFFA"><strong>Total</strong></td> <td width="9%" bgcolor="#C5DFFA"><strong>Remove</strong></td> </tr> <?php echo $cartOutput; ?> <!-- <tr> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> --> </table> <?php echo $cartTotal; ?> <br /> <br /> <?php //echo $pp_checkout_btn; ?> <br /> <br /> <a href="cart.php?cmd=emptycart">Click Here to Empty Your Shopping Cart</a> </div> <br /> </div> <?php include_once("template_footer.php");?> </div> </body> </html> Observer not triggered and log file not created.
app/etc/modules/Gta_KolupadiRestrict.xml <?xml version="1.0" encoding="UTF-8"?> <config> <modules> <Gta_KolupadiRestrict> <active>true</active> <codepool>local</codepool> </Gta_KolupadiRestrict> </modules> </config>
app/code/local/Gta/KolupadiRestrict/etc/config.xml
<?xml version="1.0"?> <config> <modules> <Gta_KolupadiRestrict> <version>1.0.0</version> </Gta_KolupadiRestrict> </modules> <global> <models> <gta_kolupadirestrict> <class>Gta_KolupadiRestrict_Model</class> </gta_kolupadirestrict> </models> </global> <frontend> <events> <checkout_cart_product_add_after> <observers> <Gta_KolupadiRestrict_Model_Observer> <type>singleton</type> <class>Gta_KolupadiRestrict_Model_Observer</class> <method>cartevent</method> </Gta_KolupadiRestrict_Model_Observer> </observers> </checkout_cart_product_add_after> </events> </frontend> </config>
app/code/local/Gta/KolupadiRestrict/Model/Observer.php <?php // Mage::log('fine dude', null, 'logfile.log'); //create class class Gta_KolupadiRestrict_Model_Observer { //create function public function cartevent(Varien_Event_Observer $observer) {
$event = $observer->getEvent(); Mage::log($event->getName(),null,'event.log');
} } ?> Edited August 6, 2019 by aveevahi my first language is german my english is not good so kindly bear it i have uploaded my files on free server /hosting site when i run it it shows me my products now the problem i m facing is when i click on add to cart it generates me this this error or warning at the all above page it generates this error Warning: Cannot modify header information - headers already sent by (output started at /www/clanteam.com/a/z/e/azeempro/htdocs/cart/include/config.php:1) in /www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/index.php on line 33 below it it add products in cart array Shopping Cart Array ( [2] => 1 ) 1 Items $90.00 checkout below that it gives this warning Warning: include() [function.include]: Unable to access /www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/shop/add_to_cart.php in /www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/layouts/shop.php on line 24 Warning: include(/www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/shop/add_to_cart.php) [function.include]: failed to open stream: No such file or directory in /www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/layouts/shop.php on line 24 Warning: include() [function.include]: Failed opening '/www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/shop/add_to_cart.php' for inclusion (include_path='.:/usr/lib/php') in /www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/layouts/shop.php on line 24 kindly help me i think there is path problem kindly solve this out please i will be thankful regards this is my index page Code: [Select] <?php include('db_fns.php'); include("cart_functions.php"); if(!isset($_SESSION['cart'])) { //session_start(); //session_start($_SESSION['cart']); $_SESSION['cart']=array(); $_SESSION['total_items']=0; $_SESSION['total_price']=0.00; } $view=empty($_GET['view'])?'index':$_GET['view']; $controller='shop'; switch ($view){ case "index": $products = find_product(); break; case "add_to_cart": $id=$_GET['id']; $add_item=add_to_cart($id); $_SESSION['total_items']=total_items($_SESSION['cart']); $_SESSION['total_price']=total_price($_SESSION['cart']); header('Location: index.php'); break; case "update_cart": update_cart(); $_SESSION['total_items']=total_items($_SESSION['cart']); $_SESSION['total_price']=total_price($_SESSION['cart']); header('Location:index.php?view=checkout'); break; case "checkout": $shipping=0.00; if($_SESSION['total_price']<=100.00 || $_SESSION['total_price']<=200.00){ $shipping=$shipping + 30.00; }elseif($_SESSION['total_price']<=300.00 || $_SESSION['total_price']<=500.00){ $shipping=$shipping + 45.00; }elseif($_SESSION['total_price']<=600.00 || $_SESSION['total_price']<=1000.00){ $shipping=$shipping+ 70.00; }elseif($_SESSION['total_price']>1000.00){ $shipping=$shipping+ 100.00; }else{ $shipping=10.00; } break; } include ('/www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/layouts/'.$controller.'.php'); ?>shop.php is Code: [Select] <?php ?> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title> Shopping Cart</title> <link href="stylesheets/cool_style.css" media="screen" rel="Stylesheet" type="text/css" /> </head> <body> <h1> products</h1> <div class="cart"> <p><b> Shopping Cart</b></p> <p><?php print_r($_SESSION['cart']);?></p> <p><?php echo $_SESSION['total_items'];?> Items </p> <p>$<?php echo number_format($_SESSION['total_price'],2);?></p> <p><a href="index.php?view=checkout">checkout</a></p> </div> <hr/> <?php include('/www/clanteam.com/a/z/e/azeempro/htdocs/cart/public/views/'.$controller.'/'.$view.'.php');?> </body> </html> Hey guys, Lovely forum. So I'm building a simple shopping cart from php and mysql. Whenever I update the quantity of the item that I want, I don't want it to duplicate all of the information but instead want it to merge the quantity together. Below is an example of what happens. Note that Test Item has been added to the cart 3 separate times with different quantities (2, 1, and 1). How can I merge this information to show Test Item once with the total quantity? I know it's some kind of merge, but I'm a coding noob. Thanks! Here is some (maybe) relevant information: Code: [Select] $insert = "INSERT INTO new_cart (sku, quantity, username) VALUES ('".$ssku."', '".$squantity."', '".$username."')"; mysql_query($insert); } echo "<table>"; $select = "SELECT * FROM new_cart JOIN products ON new_cart.sku = products.sku where '".$username."' = new_cart.username"; What needs to be changed or updated? EDIT: If you need more information, feel free to let me know and I'll post it. I would like to customize my product-list page to display all my products in a grid with an ADD TO CART option for each product. That means i have two ADD TO CART button on the site, in productDetail.php there is a ADD TO CART button original from the tutorial, i need customize another ADD TO CART button on productList.php. Does anyone know how to accomplish this customization? . Please help because I am stuck with the coding for 3weeks Tutorial site: http://www.phpwebcommerce.com/plaincart/index.php productDetail.php: Code: [Select] <?php if (!defined('WEB_ROOT')) { exit; } $product = getProductDetail($pdId, $catId); // we have $pd_name, $pd_price, $pd_description, $pd_image, $cart_url extract($product); ?> <table width="100%" border="0" cellspacing="0" cellpadding="10"> <tr> <td align="center"><img src="<?php echo $pd_image; ?>" border="0" alt="<?php echo $pd_name; ?>" width="230" height="170"></td> <td valign="middle"> <strong><?php echo $pd_name; ?></strong><br> Price : <?php echo displayAmount($pd_price); ?><br> <?php // if we still have this product in stock // show the 'Add to cart' button if ($pd_qty > 0) { ?> <input type="button" name="btnAddToCart" value="Add To Cart >" onClick="window.location.href='<?php echo $cart_url; ?>';" class="addToCartButton"> <?php } else { echo 'Out Of Stock'; } ?> </td> </tr> <tr align="left"> <td colspan="2"><?php echo $pd_description; ?></td> </tr> </table> productList.php: Code: [Select] <?php if (!defined('WEB_ROOT')) { exit; } $productsPerRow = 2; $productsPerPage = 8; //$productList = getProductList($catId); $children = array_merge(array($catId), getChildCategories(NULL, $catId)); $children = ' (' . implode(', ', $children) . ')'; $sql = "SELECT pd_id, pd_name, pd_price, pd_thumbnail, pd_qty, c.cat_id FROM tbl_product pd, tbl_category c WHERE pd.cat_id = c.cat_id AND pd.cat_id IN $children ORDER BY pd_name"; $result = dbQuery(getPagingQuery($sql, $productsPerPage)); $pagingLink = getPagingLink($sql, $productsPerPage, "c=$catId"); $numProduct = dbNumRows($result); // the product images are arranged in a table. to make sure // each image gets equal space set the cell width here $columnWidth = (int)(100 / $productsPerRow); ?> <table width="100%" border="0" cellspacing="0" cellpadding="20"> <?php if ($numProduct > 0 ) { $i = 0; while ($row = dbFetchAssoc($result)) { extract($row); if ($pd_thumbnail) { $pd_thumbnail = WEB_ROOT . 'images/product/' . $pd_thumbnail; } else { $pd_thumbnail = WEB_ROOT . 'images/no-image-small.png'; } if ($i % $productsPerRow == 0) { echo '<tr>'; } // format how we display the price $pd_price = displayAmount($pd_price); echo "<td width=\"$columnWidth%\" align=\"center\"><a href=\"" . $_SERVER['PHP_SELF'] . "?c=$catId&p=$pd_id" . "\"><img src=\"$pd_thumbnail\" border=\"0\" width=\"230\" height=\"170\"><br>$pd_name</a><br>Price : $pd_price<br>"; // format display add to cart echo "<input type=\"button\" name=\"btnAddToCart\" value=\"Add To Cart >\" onClick=\"window.location.href='<?php echo $cart_url;?>';\" >"; // if the product is no longer in stock, tell the customer if ($pd_qty <= 0) { echo "<br>Out Of Stock"; } echo "</td>\r\n"; if ($i % $productsPerRow == $productsPerRow - 1) { echo '</tr>'; } $i += 1; } if ($i % $productsPerRow > 0) { echo '<td colspan="' . ($productsPerRow - ($i % $productsPerRow)) . '"> </td>'; } } else { ?> <tr><td width="100%" align="center" valign="center">No products in this category</td></tr> <?php } ?> </table> <p align="center"><?php echo $pagingLink; ?></p> Product-functions.php: Code: [Select] <?php require_once 'config.php'; /********************************************************* * PRODUCT FUNCTIONS **********************************************************/ /* Get detail information of a product */ function getProductDetail($pdId, $catId) { $_SESSION['shoppingReturnUrl'] = $_SERVER['REQUEST_URI']; // get the product information from database $sql = "SELECT pd_name, pd_description, pd_price, pd_image, pd_qty FROM tbl_product WHERE pd_id = $pdId"; $result = dbQuery($sql); $row = dbFetchAssoc($result); extract($row); $row['pd_description'] = nl2br($row['pd_description']); if ($row['pd_image']) { $row['pd_image'] = WEB_ROOT . 'images/product/' . $row['pd_image']; } else { $row['pd_image'] = WEB_ROOT . 'images/no-image-large.png'; } $row['cart_url'] = "cart.php?action=add&p=$pdId"; return $row; } ?> any help will be greatly appreciated This is the code that I'm using for a shopping cart in order to update the quantity if the same product id is chosen. $cart_items is a two-dimensional session array composed of the id session array and the quantity session array. While looping through $cart_items, I create a temporary array $temp_array so that I can execute some logic on the array without changing the normal output. $temp_array is the $cart_items array without the last items which would be the $_REQUEST id and the $_REQUEST quantity. So the two $_REQUESTs are popped off with array_pop and then the remaining $_SESSIONs are compared to the $_REQUEST id with
$j = array_search($_REQUEST["element_id_$i"], $temp_array); if( $j==$temp_array[$i]) If $j is the key of the item in $temp_array, then the $_REQUEST is unset and the quantities for the item are added together.
There are two problems with this code. First of all the var_dump of the $temp_array is always an empty array but I am expecting to get an array with just the last element popped off. Secondly, foreach($cart_items as $item) { foreach($item["id"] as $key => $val) { foreach($item["quantity"] as $i => $value) { if ($key == $i){ $temp_array = $cart_items; $array = array_pop($temp_array); var_dump($temp_array); if (isset($_REQUEST["element_id_$i"]) && isset($_REQUEST["quantity_$i"])&& isset($value)){ $j = array_search($_REQUEST["element_id_$i"], $temp_array); if( $j==$temp_array[$i]) { echo "<b>SAME PRODUCT ADDED</b>"; $value += $_REQUEST["quantity_$i"]; unset($_REQUEST["element_id_$i"]); }}}}}}
I need to build a hierarchical product category table with data from another table. The original product info table has a basic structu ID | SKU | Category1 | Category2 | Category3 1 | 001 | Apparel | Boots | Work Boots 2 | 002 | Apparel | Shirts | Long Sleeve The categories table this is going into: ID | Name | Parent_ID 1 | Apparel | 0 2 | Boots | 1 3 | Work Boots | 2 4 | Shirts | 1 5 | Long Sleeve | 4 I've been trying to use PHP to increment ID's by: SELECT DISTINCT Category1 SELECT DISTINCT Category1,Category2 SELECT DISTINCT Category1, Category2, Category3 Code: [Select] $i='0'; while ($row = mysql_fetch_assoc($result)) { $i++; echo $i . " - ". $row['Category1'] . "<br>"; } One problem is that I can't get the parent ID that was generated in this method. Another is that Category2 and 3 are not uniquely named at all. So my question is, how do I distinctly select all three categories, and get each parent ID also? Hi all,
How can i auto populate the price of an item based on a chosen product and then auto calculate the price and quantity before hitting the ADD button. The Unit price should be auto populated based on valu from the database. The amount is the Unit Price times the Quantity of the Product Selected. Thanks My form <form action="" method="post" data-toggle="validator"> <div class="row"> <div class="col-md-4"> <div class="form-group"> <label>Item Name</label> <select id="username" name="item_name" class="form-control inputs" data-error="Select Item" required> <option value="">Chose Item</option> <?php $stmt = $pdo->query(" SELECT item_name FROM stocks ORDER BY item_name ASC "); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $item_name = $row['item_name']; echo "<option value=\"$item_name\"> $item_name </option>"; } ?> </select> <div class="help-block with-errors"></div> </div> </div> <div class="col-md-2"> <div class="form-group"> <label>Unit Price</label> <input type="number" class="form-control" name="item_price"> <div class="help-block with-errors"></div> </div> </div> <div class="col-md-2"> <div class="form-group"> <label>Item Quantity</label> <input type="number" class="form-control" name="item_qty" required data-error="Stock Quantity is missing"> <div class="help-block with-errors"></div> </div> </div> <div class="col-md-2"> <div class="form-group"> <label>Amount</label> <input type="number" class="form-control" name="amount" > <div class="help-block with-errors"></div> </div> </div> </div> <br> <span class="input-group-btn"> <input name="send" type="submit" class="btn btn-danger btn-form display-4 rounded" value="ADD"> </span> </form>Edited May 4, 2019 by I-AM-OBODO Hi there, I'm trying to get the full Product Name in the <title> of each of the product pages - I can't seem to figure out what to put in the product_info.php... I've tried a few different options but I'm not great with php Here's what I have currently but it creates errors... <script language="javascript"><!-- document.write('<title><?php echo TITLE; ?> : <?php echo $product_info['products_name']; ?> </title>'); //--></script> Hope someone can help - thanks so much! Hey again, Still on the same project but now doing the cart page. I've been bringing my skills up to date a lot doing this project, however, having said that I now can't figure out why my cart is not adding the items to the cart, i've checked the post data and the quantity is set to 1. Would really appreciate someone having a look at it and hopefully can see what i'm missing. you can see what it should look like (well so far lol) at here in my sandbox site. thanks again. (p.s. The parts are all over the place on the page, so thought better put whole page, sorry if shouldn't have done that) <?php // Initialize the session session_start(); // Include config file require_once "dbcontroller.php"; $db_handle = new DBController(); if(!empty($_GET["action"])) { switch($_GET["action"]) { case "add": if(!empty($_POST["quantity"])) { $productByCode = $db_handle->runQuery("SELECT * FROM products WHERE product_code='" . $_GET["product_code"] . "'"); $itemArray = array($productByCode[0]["product_code"]=>array('product_name'=>$productByCode[0]["product_name"], 'product_code'=>$productByCode[0]["product_code"], 'quantity'=>$_POST["quantity"], 'price'=>$productByCode[0]["price"], 'image'=>$productByCode[0]["img1"])); if(!empty($_SESSION["cart_item"])) { if(in_array($productByCode[0]["product_code"],array_keys($_SESSION["cart_item"]))) { foreach($_SESSION["cart_item"] as $k => $v) { if($productByCode[0]["product_code"] == $k) { if(empty($_SESSION["cart_item"][$k]["quantity"])) { $_SESSION["cart_item"][$k]["quantity"] = 0; } $_SESSION["cart_item"][$k]["quantity"] += $_POST["quantity"]; } } } else { $_SESSION["cart_item"] = array_merge($_SESSION["cart_item"],$itemArray); } } else { $_SESSION["cart_item"] = $itemArray; } } break; case "remove": if(!empty($_SESSION["cart_item"])) { foreach($_SESSION["cart_item"] as $k => $v) { if($_GET["product_code"] == $k) unset($_SESSION["cart_item"][$k]); if(empty($_SESSION["cart_item"])) unset($_SESSION["cart_item"]); } } break; case "empty": unset($_SESSION["cart_item"]); break; } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>LeaversHoodies.ie</title> <!-- Bootstrap --> <link href="css/bootstrap-4.4.1.css" rel="stylesheet"> <link href="css/accordian.css" rel="stylesheet"> </head> <body> <?php include 'header_student.php'; ?> <br /> <?php $school_page = "ravenswell"; $sql = "SELECT * FROM schools WHERE school_page = '$school_page'"; if($result = mysqli_query($conn, $sql)) while($row = mysqli_fetch_array($result)) { ?> <h3 class="text-center">Student Ordering Page For</h3> <h2 class="text-center" style="text-transform:uppercase;"><?php echo $row['school_name']; ?></h2> <hr style="width: 50%; text-align:center; border: 2px solid #00aeef; border-radius: 5px; margin: 0 auto;"> <br /> <div class="container"> <div class="row"> <div class="col"> <?php $path = "images/schools/"; $file = $row["logo"]; if(!empty($row['logo'])) { echo '<img src="'.$path.$file.'" style="width:95%; height:auto; margin-top:-130px;"><br /><br />'; } else { echo '<img src="images/schools/140x140.gif" style="width:95%; height:auto; margin-top:-130px;"><br /><br />'; } ?></div> <div class="col-6"> <h5>These are the garments your school has choosen :</h5><br /> <?php $school_id = $row["school_id"]; } var_dump($_SESSION); var_dump($_POST); $product_array = $db_handle->runQuery("SELECT * FROM choices INNER JOIN products USING (product_code) INNER JOIN colours USING (colour_id) WHERE school_id = '$school_id'"); if (!empty($product_array)) { foreach($product_array as $key=>$value){ ?> <div class="container"> <div class="row"> <div class="col-5" style="text-align:left;"> <img src="images/products/<?php echo $product_array[$key]["img1"]; ?>" alt="<?php echo $product_array[$key]["product_code"]; ?>" style="position:relative; top:0; width:200px; display:block;"> </div> <div class="col-7"> <h5><?php echo $product_array[$key]["product_name"]; ?></h5><p> in <?php echo $product_array[$key]["colour_name"]; ?></p> <p style="font-size:12px;"><?php echo $product_array[$key]["description"]; ?></p> <?php $comment = $product_array[$key]["comment"]; if (empty($comment)) { echo ""; } else { ?> <p style="font-size:12px;"><b>A note from your teacher:</b> <br /> <?php echo $product_array[$key]["comment"]; ?></p> <?php }; ?> <form action="student_order.php?schoolname=<?php echo $school_page; ?>?action=add&product_code=<?php echo $product_array[$key]["product_code"]; ?>" method="post"> <?php $product = $product_array[$key]["product_code"]; ?> Please select your size : <select id="size" name="size"> <?php $sql1 = "SELECT DISTINCT * FROM sizes WHERE product_code = '$product'"; if($result1 = mysqli_query($conn, $sql1)) while($row3 = mysqli_fetch_array($result1)){ echo "<option value='" . $row3['size'] . "'>" . $row3['size'] . "</option>"; } else { echo "nothing to see here"; } ?> </select> <br /><br /> <div class="number">How many do you want: <input type="number" style="font-size:12px;" id="quantity" name="quantity" value="1" min="1" max="5"><br /> Price : <?php echo "€".$product_array[$key]["price"]; ?> </div> <input type="hidden" id="product_code" value="<?php echo $product; ?>"><br /> <input type="submit" style="font-size:12px;" value="Add to Order" class="btnAddAction"> </form> </div> </div> <br /><hr style="width: 90%; text-align:center; border: 1px solid #00aeef; border-radius: 5px; margin: 0 auto;"><br /> </div> <?php } } else { echo "No Schools by that name registered."; } ?> </div> <div class="col-3"> <div style="border: 1px solid #d3d3d3; padding: 10px; border-radius: 5px; margin-top:30px;"> Your Order: </div> </div> </div> <div class="txt-heading">Shopping Cart</div> <a id="btnEmpty" href="student_order.php?schoolname=<?php echo $school_page; ?>?action=empty">Empty Cart</a> <?php if(isset($_SESSION["cart_item"])){ $total_quantity = 0; $total_price = 0; ?> <table class="tbl-cart" cellpadding="10" cellspacing="1"> <tbody> <tr> <th style="text-align:left;">Name</th> <th style="text-align:left;">Code</th> <th style="text-align:right;" width="5%">Quantity</th> <th style="text-align:right;" width="10%">Unit Price</th> <th style="text-align:right;" width="10%">Price</th> <th style="text-align:center;" width="5%">Remove</th> </tr> <?php foreach ($_SESSION["cart_item"] as $item){ $item_price = $item["quantity"]*$item["price"]; ?> <tr> <td><img src="images/products/<?php echo $item["img1"]; ?>" class="cart-item-image" /><?php echo $item["product_name"]; ?></td> <td><?php echo $item["product_code"]; ?></td> <td style="text-align:right;"><?php echo $item["quantity"]; ?></td> <td style="text-align:right;"><?php echo "$ ".$item["price"]; ?></td> <td style="text-align:right;"><?php echo "$ ". number_format($item_price,2); ?></td> <td style="text-align:center;"><a href="student_order.php?schoolname=<?php echo $school_page; ?>?action=remove&product_code=<?php echo $item["product_code"]; ?>" class="btnRemoveAction"><img src="icon-delete.png" alt="Remove Item" /></a></td> </tr> <?php $total_quantity += $item["quantity"]; $total_price += ($item["price"]*$item["quantity"]); } ?> <tr> <td colspan="2" align="right">Total:</td> <td align="right"><?php echo $total_quantity; ?></td> <td align="right" colspan="2"><strong><?php echo "$ ".number_format($total_price, 2); ?></strong></td> <td></td> </tr> </tbody> </table> <?php } else { ?> <div class="no-records">Your Cart is Empty</div> <?php } ?> </div> </div> <br /><br /> <?php include 'footer_student.php'; ?> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="js/jquery-3.4.1.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="js/popper.min.js"></script> <script src="js/bootstrap-4.4.1.js"></script> </body> </html>
How can I make index.php?view=cart work in place of cart.php in the following code? Works: header("Location: cart.php?msg=Thank you. Check your e-mail for Login details."); die; } else header("Location: cart.php?msg=Account with given email does not exist."); die; Doesn't work: header("Location: index.php?view=cart?msg=Thank you. Check your e-mail for Login details."); die; } else header("Location: index.php?view=cart?msg=Account with given email does not exist."); die; Forgot Your Password Not working at the customer end, how to solve this error? Reset Password Email not sending to the customer mail account. Edited June 13, 2019 by aveeva
My script has 3 classes (that are relevant to this discussion): DB, User and Validate. They are all in independent files and loaded automatically, when required, by an autoloader.
The error messages I am getting a Any pointers as to what I am doing wrong, or what I should be doing, would be most welcome. Hey everyone, Just want to say please move this thread if this is the wrong place. I have made a shopping cart for a client from scratch, and now the ONLY thing stopping me from finishing it is a SHIPPING calculator. ALL the stupid API's from these companies like UPS make absolutely no sense, half of them required PHP extensions to be installed, and I got many errors with that departement too... Now how does osCommerce or Ebay do this? Is there anywhere out there where there is a tutorial on completing this task? I have a list of id's between 1 - 8 which represents a print size that I need to use to calculate shipping costs, but the calculation that I need to perform is complicated. I will try to explain to the best of my abilities. I also need to query a database table to make sure the shipping rates are always up-to-date id's <= 4 will ship for $4.95 now matter how many id's are listed. Eg: 1,2,3,4,3,2,4,2,4 will ship for a total of $4.95 will query price db as 1 id's <= 5 will ship for $6.00 now mater how many id's are listed. Eg: 1,3,3,1,2,3,5,5,5,4,3,3 will ship for a total of $6 will query price db as 2 id's <= 6 will ship for $8.00 now mater how many id's are listed. Eg: 1,3,6,1,6,3,5,5,5,4,3,3 will ship for a total of $8.00 will query price db as 3 id 7 will ship for $5 * Quanity + any of the examples above will query price db as 4 id 8 will ship for $15 * Quantity + any of the examples above will query price db as 5 table structure for shiprates is id, price |