Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

src/Model/Modifiers/Shipping/Weight.php (2 issues)

1
<?php
2
3
namespace SilverShop\Model\Modifiers\Shipping;
4
5
/**
6
 * Calculates the shipping cost of an order, by taking the products
7
 * and calculating the shipping weight, based on an array set in _config
8
 *
9
 * ASSUMPTION: The total order weight can be at maximum the last item
10
 * in the $shippingCosts array.
11
 */
12
class Weight extends Base
13
{
14
    /**
15
     * Weight to price mapping.
16
     * Should be an associative array, with the weight as key (KG) and the corresponding price as value.
17
     * Can be set via Config API, eg.
18
     * <code>
19
     * WeightShippingModifier:
20
     *   weight_cost:
21
     *     '0.5': 12
22
     *     '1.0': 15
23
     *     '2.0': 20
24
     * </code>
25
     *
26
     * @config
27
     * @var    array
28
     */
29
    private static $weight_cost = [];
30
31
    protected $weight = 0;
32
33
    /**
34
     * Calculates shipping cost based on Product Weight.
35
     */
36
    public function value($subtotal = 0)
37
    {
38
        $totalWeight = $this->Weight();
39
        if (!$totalWeight) {
40
            return $this->Amount = 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like 0 of type integer is incompatible with the declared type SilverStripe\ORM\FieldType\DBCurrency of property $Amount.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
41
        }
42
        $amount = 0;
43
44
        $table = $this->config()->weight_cost;
45
        if (!empty($table) && is_array($table)) {
46
            // ensure table is sorted
47
            ksort($table, SORT_NUMERIC);
48
            // set the amount to the highest value. In case the weight is higher than the max value in
49
            // the table, use the highest shipping cost and not 0!
50
            $amount = end($table);
51
            reset($table);
52
            foreach ($table as $weight => $cost) {
53
                if ($totalWeight <= $weight) {
54
                    $amount = $cost;
55
                    break;
56
                }
57
            }
58
        }
59
        return $this->Amount = $amount;
60
    }
61
62
    public function TableTitle()
63
    {
64
        return _t(
65
            __CLASS__ . '.TableTitle',
66
            'Shipping ({Kilograms} kg)',
67
            '',
68
            ['Kilograms' => $this->Weight()]
69
        );
70
    }
71
72
    /**
73
     * Calculate the total weight of the order
74
     *
75
     * @return number
76
     */
77
    public function Weight()
78
    {
79
        if ($this->weight) {
80
            return $this->weight;
81
        }
82
        $weight = 0;
83
        $order = $this->Order();
84
        if ($order && $orderItems = $order->Items()) {
85
            foreach ($orderItems as $orderItem) {
86
                if ($product = $orderItem->Product()) {
0 ignored issues
show
The method Product() does not exist on SilverShop\Model\OrderItem. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
                if ($product = $orderItem->/** @scrutinizer ignore-call */ Product()) {
Loading history...
87
                    $weight = $weight + ($product->Weight * $orderItem->Quantity);
88
                }
89
            }
90
        }
91
        return $this->weight = $weight;
92
    }
93
}
94