laravelshoppingcart icon indicating copy to clipboard operation
laravelshoppingcart copied to clipboard

How to add Condition for Item without multiply

Open Doremidon opened this issue 4 years ago • 4 comments

I have some Items and it's need to add for them Condition like "Take Photo" (for example). So I try:

$saleCondition = new \Darryldecode\Cart\CartCondition(array(
    'name' => 'Take Photo',
    'type' => 'tax',
    'target' => 'price', // total / subtotal - doesn't work either
    'value' => '5',
));
\Cart::add(array(
    'id' => 1,
    'name' => 'Cake',
    'price' => 100,
    'quantity' => 10,
    'attributes' => array(),
    'conditions' => $saleCondition
    ));
$saleCondition = new \Darryldecode\Cart\CartCondition(array(
    'name' => 'Make Video',
    'type' => 'tax',
    'target' => 'subtotal', // total / subtotal - doesn't work either
    'value' => '10',
));
\Cart::add(array(
    'id' => 2,
    'name' => 'Brownie',
    'price' => 100,
    'quantity' => 10,
    'attributes' => array(),
    'conditions' => $saleCondition
    ));

So in the finish I have: \Cart::getTotal() == 2150 But I expect to have: \Cart::getTotal() == 2015

I do not need to apply Condition to all products, but only to add to the one item product.

Is there have any way to to it?

Doremidon avatar Aug 18 '20 09:08 Doremidon

\Cart::getTotal() == 2150 This is as expected because you have that much quantity to multiply when you added to cart.

To get total 2015 you should try like this..

// First add your item(s) in your cart 
\Cart::add(array(
    'id' => 1,
    'name' => 'Cake',
    'price' => 100,
    'quantity' => 10,
    'attributes' => array()
));

// Next update with condition at cart level.. 
$saleCondition1 = new \Darryldecode\Cart\CartCondition(array(
    'name' => 'Take Photo',
    'type' => 'tax',
    'target' => 'subtotal',
    'value' => '5',
	'attributes' => array()
));
$saleCondition2 = new \Darryldecode\Cart\CartCondition(array(
    'name' => 'Make Video',
    'type' => 'tax',
    'target' => 'subtotal',
    'value' => '10',
	'attributes' => array()
));
\Cart::condition([$saleCondition1, $saleCondition2]);

ilyaskazi avatar Jan 17 '21 18:01 ilyaskazi

\Cart::condition([$saleCondition1, $saleCondition2]);

Thank you Ilyaskazi for your answer :)

I solve this problem by making a fork of "laravelshoppingcart".

I have many products. For each of product I have various Conditions. This Conditions should be connected to product. Because User inside of the cart can change few Conditions as he want for each one's of the product.

Doremidon avatar Jan 17 '21 18:01 Doremidon

If your product item requires such multiple conditions (technically other items.. take photo or make video per item), you should create sub-items of the item and add them to cart. That should have solve your purpose. The sub-items can be determined using item id you create or by it's attributes.

ilyaskazi avatar Jan 17 '21 20:01 ilyaskazi

Thank you Ilyas Kazi :)

I decided to make it as Conditions connected to each product. It's work well for a project.

Sub-item is not what I want and it's not exist in "laravelshoppingcart".

In method "count()" I want to see only count of product but not count of (product + sub-tem).

Doremidon avatar Jan 17 '21 21:01 Doremidon