forked from viewup/up-wp-cart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WPCartItem.php
67 lines (57 loc) · 1.28 KB
/
WPCartItem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Cart Item class
*/
// fallback to cart content filter (eg: the plugin inserted on theme directly)
if ( ! defined( 'UPWPCART_CONTENT_FILTER' ) ) {
define( 'UPWPCART_CONTENT_FILTER', 'cart_content' );
}
if ( ! defined( 'UPWPCART_PRICE_FILTER' ) ) {
define( 'UPWPCART_PRICE_FILTER', 'cart_price' );
}
/**
* Class WPCartItem
*/
class WPCartItem {
public $ID = 0;
public $amount = 1;
public $price = 0;
public $total = 0;
public $content = null;
/**
* WPCartItem constructor.
*
* @param int $ID
* @param int [$price]
* @param int [$amount]
*/
public function __construct( $ID, $amount = 1, $price = 0 ) {
$this->ID = $ID;
if ( ! is_numeric( $price ) ) {
$price = 0;
}
$this->content = apply_filters( UPWPCART_CONTENT_FILTER, $this->ID, $price );
$this->price = apply_filters( UPWPCART_PRICE_FILTER, $this->ID, $price, $this->content );
$this->update( $amount );
}
/**
* Update cart's item
*
* @param int [$amount=null]
*
* @return $this
*/
public function update( $amount = null ) {
// if invalid amount
if ( ! is_numeric( $amount ) ) {
$amount = $this->amount;
}
// if lower than 0
if ( $amount < 0 ) {
$amount = 0;
}
$this->amount = $amount;
$this->total = $this->price * $this->amount;
return $this;
}
}