Comments (3)
hi @gravataLonga ,
Calculating shipping cost is out of scope of this package, its better to implement it yourself to fit your needs as most of the time we deal with it differently every e-commerce site we build.
I don't know much of your case but did you ever get any problem utilizing the "attributes" field if you need "weight " of each item?
from laravelshoppingcart.
from laravelshoppingcart.
Hi, I think weight is not in attribute array, it is property of order/cart item.
So as said above we can add your Cart class this method;
/**
* get total weight of items in the cart
*
* @return int
*/
public function getTotalWeight()
{
$items = $this->getContent();
if( $items->isEmpty() ) return 0;
$count = $items->sum(function($item)
{
return $item['weight'] * $item['quantity'];
});
return $count;
}
of course we must update add method too;
public function add($id, $name = null, $price = null, $quantity = null, $weight = null, $attributes = array(), $conditions = array())
{
// if the first argument is an array,
// we will need to call add again
if( is_array($id) )
{
// the first argument is an array, now we will need to check if it is a multi dimensional
// array, if so, we will iterate through each item and call add again
if( Helpers::isMultiArray($id) )
{
foreach($id as $item)
{
$this->add(
$item['id'],
$item['name'],
$item['price'],
$item['quantity'],
$item['weight'],
Helpers::issetAndHasValueOrAssignDefault($item['attributes'], array()),
Helpers::issetAndHasValueOrAssignDefault($item['conditions'], array())
);
}
}
else
{
$this->add(
$id['id'],
$id['name'],
$id['price'],
$id['quantity'],
$id['weight'],
Helpers::issetAndHasValueOrAssignDefault($id['attributes'], array()),
Helpers::issetAndHasValueOrAssignDefault($id['conditions'], array())
);
}
return $this;
}
// validate data
$item = $this->validate(array(
'id' => $id,
'name' => $name,
'price' => Helpers::normalizePrice($price),
'quantity' => $quantity,
'weight' => $weight,
'attributes' => new ItemAttributeCollection($attributes),
'conditions' => $conditions,
));
// get the cart
$cart = $this->getContent();
// if the item is already in the cart we will just update it
if( $cart->has($id) )
{
$this->events->fire($this->getInstanceName().'.updating', array($item, $this));
$this->update($id, $item);
$this->events->fire($this->getInstanceName().'.updated', array($item, $this));
}
else
{
$this->events->fire($this->getInstanceName().'.adding', array($item, $this));
$this->addRow($id, $item);
$this->events->fire($this->getInstanceName().'.added', array($item, $this));
}
return $this;
}
This solution work for my simple case.
Thanks
from laravelshoppingcart.
Related Issues (20)
- Please help with database errors HOT 1
- Apply cart condition once on cart item regardless of its quantity HOT 4
- Apply multiple taxes HOT 1
- New package with improved cart performance
- Shopping configuration is not working
- Content of cart didn't return
- Update Cart Error When Update Only Work First Item HOT 4
- issue in getcartcontent - Object's Key/Value
- getPriceSum() in getSubTotalWithoutConditions() errors when summing
- update readme for laravel 8 and 9
- How to update quantity with decimal points
- Laravel 10 HOT 3
- Method Darryldecode\Cart\ItemCollection::getOrder does not exist.
- Cart Conditions HOT 2
- How to get all carts for analysis
- when is support for laravel 10 HOT 9
- Demo app is broken
- ¿How can I prevent the items in the cart from changing position? HOT 3
- How can I check the best-selling products? HOT 1
- when add product to card return last item added only
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravelshoppingcart.