Hello,
I’m trying to retrieve the weight of all products in the shopping cart (weights are stored in the SQL database) and sum them and store them into a variable. It is the StorePress WordPress theme with Mycart php plugin that is being used. I have attached the code for the shopping cart (just one piece to the large puzzle).
[php]<?php
/**
- Cart.php
- The shopping cart system
- @author Rockethemes.net
- @version 1.1
- @package mycart
-
@subpackage cart
**/
require(“Item.php”);
class Cart {
// properties
var $contents = array(); // The contents (Items) of the cart
var $shipped = array(); // Reference list of shipped Items
var $downloads = array(); // Reference list of digital Items
var $discounts = array(); // List of promotional discounts applied
var $promocodes = array(); // List of promotional codes applied
var $shipping = array(); // List of shipping options
// Object properties
var $Added = false; // Last Item added
var $Totals = false; // Cart Totals data structure
var $freeship = false;
var $showpostcode = false; // Flag to show postcode field in shipping estimator
var $noshipping = false; // Shipping calculates disabled
// Internal properties
var $changed = false; // Flag when Cart updates and needs retotaled
var $added = false; // The index of the last item added
var $runaway = 0;
var $retotal = false;
var $handlers = false;
/**
* Cart constructor
*
* @author Rockethemes.net
* @since 1.0
*
* @return void
**/
function __construct () {
$this->Totals = new CartTotals(); // Initialize aggregate total data
$this->listeners(); // Establish our command listeners
}
/**
* Restablish listeners after being loaded from the session
*
* @author Rockethemes.net
* @since 1.1
*
* @return void
**/
function __wakeup () {
$this->listeners();
}
/**
* Listen for events to trigger cart functionality
*
* @author Rockethemes.net
* @since 1.1
*
* @return void
**/
function listeners () {
add_action('parse_request',array(&$this,'totals'),99);
add_action('mycart_cart_request',array(&$this,'request'));
add_action('mycart_session_reset',array(&$this,'clear'));
}
/**
* Processes cart requests and updates the cart data
*
* @author Rockethemes.net
* @since 1.0
*
* @return void
**/
function request () {
global $Mycart;
if (isset($_REQUEST['checkout'])) mycart_redirect(mycarturl(false,'checkout',$Mycart->Order->security()));
if (isset($_REQUEST['shopping'])) mycart_redirect(mycarturl());
if (isset($_REQUEST['shipping'])) {
if (!empty($_REQUEST['shipping']['postcode'])) // Protect input field from XSS
$_REQUEST['shipping']['postcode'] = esc_attr($_REQUEST['shipping']['postcode']);
do_action_ref_array('mycart_update_destination',array($_REQUEST['shipping']));
if (!empty($_REQUEST['shipping']['country']) || !empty($_REQUEST['shipping']['postcode']))
$this->changed(true);
}
if (!empty($_REQUEST['promocode'])) {
$this->promocode = esc_attr($_REQUEST['promocode']);
$this->changed(true);
}
if (!isset($_REQUEST['cart'])) $_REQUEST['cart'] = false;
if (isset($_REQUEST['remove'])) $_REQUEST['cart'] = "remove";
if (isset($_REQUEST['update'])) $_REQUEST['cart'] = "update";
if (isset($_REQUEST['empty'])) $_REQUEST['cart'] = "empty";
if (!isset($_REQUEST['quantity'])) $_REQUEST['quantity'] = 1;
switch($_REQUEST['cart']) {
case "add":
$products = array(); // List of products to add
if (isset($_REQUEST['product'])) $products[] = $_REQUEST['product'];
if (!empty($_REQUEST['products']) && is_array($_REQUEST['products']))
$products = array_merge($products,$_REQUEST['products']);
if (empty($products)) break;
foreach ($products as $id => $product) {
if ( isset($product['quantity']) && $product['quantity'] == '0' ) continue;
$quantity = ! isset($product['quantity']) || empty($product['quantity']) ? 1 : $product['quantity']; // Add 1 by default
$Product = new Product($product['product']);
$pricing = false;
if (!empty($product['options'][0])) $pricing = $product['options'];
elseif (isset($product['price'])) $pricing = $product['price'];
$category = false;
if (!empty($product['category'])) $category = $product['category'];
$data = array();
if (!empty($product['data'])) $data = $product['data'];
$addons = array();
if (isset($product['addons'])) $addons = $product['addons'];
if (!empty($Product->id)) {
if (isset($product['item'])) $result = $this->change($product['item'],$Product,$pricing);
else $result = $this->add($quantity,$Product,$pricing,$category,$data,$addons);
}
}
break;
case "remove":
if (!empty($this->contents)) $this->remove(current($_REQUEST['remove']));
break;
case "empty":
$this->clear();
break;
default:
if (isset($_REQUEST['item']) && isset($_REQUEST['quantity'])) {
$this->update($_REQUEST['item'],$_REQUEST['quantity']);
} elseif (!empty($_REQUEST['items'])) {
foreach ($_REQUEST['items'] as $id => $item) {
if (isset($item['quantity'])) {
$item['quantity'] = ceil(preg_replace('/[^\d\.]+/','',$item['quantity']));
if (!empty($item['quantity'])) $this->update($id,$item['quantity']);
if (isset($_REQUEST['remove'][$id])) $this->remove($_REQUEST['remove'][$id]);
}
if (isset($item['product']) && isset($item['price']) &&
$item['product'] == $this->contents[$id]->product &&
$item['price'] != $this->contents[$id]->priceline) {
$Product = new Product($item['product']);
$this->change($id,$Product,$item['price']);
}
}
}
}
do_action('mycart_cart_updated',$this);
}[/php]
I know it is very large but any help on the matter I can get would mean the world to me.
Thanks,
Virat