Last active
December 30, 2015 05:49
-
-
Save Ke-/7784863 to your computer and use it in GitHub Desktop.
Public DS Cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define cart_id => var(cart_id) || $cart_id := lasso_uniqueid | |
define current_cart => var(current_cart) || $current_cart := cart->oncreate('id' = cart_id) | |
define cart => type { | |
parent activerow | |
public ds => ds(::example.carts) | |
public created_column => 'created' | |
public modified_column => 'modified' | |
public items => .ds->table(::cart_items) | |
->where('cart_id' = .id,'status' = 0) | |
->rows(::cart_item) | |
public additem(...) => { | |
.isnew ? .save | |
return cart_item->save(::cart_id = .keyvalue,#rest) | |
} | |
// Over ride the delete method (we want to keep deleted items) | |
public delete => { | |
.update('status'=-1) | |
.items->foreach => {#1->delete} | |
} | |
// You can put extra hooks here | |
public save => { | |
..save | |
} | |
public asstring => '<div class="cart"><h3>Cart items</h3>' + .items->join() + '</div>' | |
} | |
define cart_item => type { | |
parent activerow | |
public ds => ds(::example.cart_items) | |
public created_column => 'created' | |
public modified_column => 'modified' | |
public description => .find(method_name) | |
public price => decimal(.find(method_name)) | |
public qty => .find(method_name) | |
public total => .price * .qty | |
// Over ride the delete method (always keep rows) | |
public delete => .update('status' = -1) | |
public asstring => .keyvalue + ': ' + .description + ' x ' + .qty + ' @ ' + .price + ' = ' + .total + '<br/>' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ---------------------------- | |
-- Table structure for `cart_items` | |
-- ---------------------------- | |
DROP TABLE IF EXISTS `cart_items`; | |
CREATE TABLE `cart_items` ( | |
`ID` bigint(20) NOT NULL AUTO_INCREMENT, | |
`cart_id` char(36) DEFAULT NULL, | |
`description` varchar(128) DEFAULT NULL, | |
`price` decimal(12,2) DEFAULT '0.00', | |
`qty` smallint(5) DEFAULT '0', | |
`status` tinyint(4) DEFAULT '0', | |
`created` datetime DEFAULT NULL, | |
`modified` datetime DEFAULT NULL, | |
PRIMARY KEY (`ID`) | |
) ENGINE=MyISAM AUTO_INCREMENT=738 DEFAULT CHARSET=utf8; | |
-- ---------------------------- | |
-- Table structure for `carts` | |
-- ---------------------------- | |
DROP TABLE IF EXISTS `carts`; | |
CREATE TABLE `carts` ( | |
`ID` char(36) NOT NULL DEFAULT '', | |
`created` datetime DEFAULT NULL, | |
`modified` datetime DEFAULT NULL, | |
PRIMARY KEY (`ID`) | |
) ENGINE=MyISAM DEFAULT CHARSET=utf8; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?lasso | |
session_start('ds_cart',-path='/') | |
session_addvar('ds_cart','cart_id') | |
current_cart->additem(::description='Example',::price = 9.95, ::qty = 1) | |
with item in current_cart->items do { | |
#item(::qty) = #item(::qty) + 1 | |
#item(::price) = #item(::price) * 2 | |
#item->qty > 5 ? #item->delete | |
} | |
current_cart | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Me thinks this is actually not working. When using the code as written here I get an error
Unable to create row
235:27 ds/activerow.lasso
255:5 ds/activerow.lasso
254:3 ds/activerow.lasso
29:5 //path/ds_cart.lasso
17:13 //path/ds_cart.lasso
6:15 //path/example.lasso
/Jolle