Skip to content

Commit 32ccba7

Browse files
committed
Able to associated model when adding using array format
1 parent 8678a06 commit 32ccba7

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,39 @@ Here is an example:
702702
// add the item to the cart.
703703
$cartItem = Cart::add(455, 'Sample Item', 100.99, 2, array())->associate('Product');
704704

705+
// array format
706+
Cart::add(array(
707+
'id' => 456,
708+
'name' => 'Sample Item',
709+
'price' => 67.99,
710+
'quantity' => 4,
711+
'attributes' => array(),
712+
'associatedModel' => 'Product'
713+
));
714+
715+
// add multiple items at one time
716+
Cart::add(array(
717+
array(
718+
'id' => 456,
719+
'name' => 'Sample Item 1',
720+
'price' => 67.99,
721+
'quantity' => 4,
722+
'attributes' => array(),
723+
'associatedModel' => 'Product'
724+
),
725+
array(
726+
'id' => 568,
727+
'name' => 'Sample Item 2',
728+
'price' => 69.25,
729+
'quantity' => 4,
730+
'attributes' => array(
731+
'size' => 'L',
732+
'color' => 'blue'
733+
),
734+
'associatedModel' => 'Product'
735+
),
736+
));
737+
705738
// Now, when iterating over the content of the cart, you can access the model.
706739
foreach(Cart::getContent() as $row) {
707740
echo 'You have ' . $row->qty . ' items of ' . $row->model->name . ' with description: "' . $row->model->description . '" in your cart.';

src/Darryldecode/Cart/Cart.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,11 @@ public function has($itemId)
150150
* @param int $quantity
151151
* @param array $attributes
152152
* @param CartCondition|array $conditions
153+
* @param string $associatedModel
153154
* @return $this
154155
* @throws InvalidItemException
155156
*/
156-
public function add($id, $name = null, $price = null, $quantity = null, $attributes = array(), $conditions = array())
157+
public function add($id, $name = null, $price = null, $quantity = null, $attributes = array(), $conditions = array(), $associatedModel = null)
157158
{
158159
// if the first argument is an array,
159160
// we will need to call add again
@@ -168,7 +169,8 @@ public function add($id, $name = null, $price = null, $quantity = null, $attribu
168169
$item['price'],
169170
$item['quantity'],
170171
Helpers::issetAndHasValueOrAssignDefault($item['attributes'], array()),
171-
Helpers::issetAndHasValueOrAssignDefault($item['conditions'], array())
172+
Helpers::issetAndHasValueOrAssignDefault($item['conditions'], array()),
173+
Helpers::issetAndHasValueOrAssignDefault($item['associatedModel'], null)
172174
);
173175
}
174176
} else {
@@ -178,7 +180,8 @@ public function add($id, $name = null, $price = null, $quantity = null, $attribu
178180
$id['price'],
179181
$id['quantity'],
180182
Helpers::issetAndHasValueOrAssignDefault($id['attributes'], array()),
181-
Helpers::issetAndHasValueOrAssignDefault($id['conditions'], array())
183+
Helpers::issetAndHasValueOrAssignDefault($id['conditions'], array()),
184+
Helpers::issetAndHasValueOrAssignDefault($item['associatedModel'], null)
182185
);
183186
}
184187

@@ -193,6 +196,7 @@ public function add($id, $name = null, $price = null, $quantity = null, $attribu
193196
'quantity' => $quantity,
194197
'attributes' => new ItemAttributeCollection($attributes),
195198
'conditions' => $conditions,
199+
'associatedModel' => $associatedModel,
196200
));
197201

198202
// get the cart

0 commit comments

Comments
 (0)