PHP Mycart WordPress Extension

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).


  • Cart.php
  • The shopping cart system
  • @author
  • @version 1.1
  • @package mycart
  • @subpackage cart


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
 * @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
 * @since 1.1
 * @return void
function __wakeup () {

 * Listen for events to trigger cart functionality
 * @author
 * @since 1.1
 * @return void
function listeners () {

 * Processes cart requests and updates the cart data
 * @author
 * @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']);

		if (!empty($_REQUEST['shipping']['country']) || !empty($_REQUEST['shipping']['postcode']))


	if (!empty($_REQUEST['promocode'])) {
		$this->promocode = esc_attr($_REQUEST['promocode']);

	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);

		case "remove":
			if (!empty($this->contents)) $this->remove(current($_REQUEST['remove']));
		case "empty":
			if (isset($_REQUEST['item']) && isset($_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']);


I know it is very large but any help on the matter I can get would mean the world to me.

Well, I noted that many people have viewed your question and none have answered.
I thought I should explain why. First, you showed some us some PHP code that reads and totals items
in the “cart”. This appears to be working and is not really long as you thought. But, no database code.

It the section:

switch($_REQUEST[‘cart’]) {
case “add”:

You build info for an item added to your cart. But, there is no info item for shipping weights. Therefore,
we can not help you. We have no knowledge of your database or where the data is pulled from tables
in the database. You are using a “cart” template of some sort. In supporting files, you should find where
info about your products are loaded into variables. In this area, you should add the shipping weights into
variables. Then, in the “ADD” switch, add code to handle placing this info into your cart for that product.
Then, of course, you would have to add whatever code you need for totaling up your weights for your
shipping estimates. Also, your cart must have some “layout” of it’s own and somewhere in your other
code, there may be changes needed to handle the extra field for shipping weight.

So, there is a start to your process, but, of course without knowing a lot more about your pages, this is
just a “general” information post for you. When you get further along, ask more questions and we will
try to help. Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service