Giter VIP home page Giter VIP logo

Comments (3)

darryldecode avatar darryldecode commented on June 18, 2024

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.

darryldecode avatar darryldecode commented on June 18, 2024

#36

from laravelshoppingcart.

azulkipli avatar azulkipli commented on June 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.