Completed
Push — master ( 7265c0...164033 )
by Justin
07:34
created

WPSC_Cart_Item_Table::cart_item_description()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
require_once( WPSC_TE_V2_CLASSES_PATH . '/table.php' );
4
5
class WPSC_Cart_Item_Table extends WPSC_Table {
6
	private static $instance;
7
8
	public static function get_instance() {
9
		if ( empty( self::$instance ) ) {
10
			self::$instance = new WPSC_Cart_Item_Table();
11
		}
12
13
		return self::$instance;
14
	}
15
16
	public $columns           = array();
17
	public $show_shipping     = true;
18
	public $show_tax          = true;
19
	public $show_total        = true;
20
	public $show_thumbnails   = true;
21
	public $show_coupon_field = true;
22
23
	public function __construct() {
24
		global $wpsc_cart;
25
26
		parent::__construct();
27
28
		if ( ! isset( $GLOBALS['wpsc_cart'] ) ) {
29
			$GLOBALS['wpsc_cart'] = new wpsc_cart();
30
		}
31
32
		$this->prepare_cache();
33
34
		$this->columns = array(
35
			'image'       => '',
36
			'items'       => __( 'Items'     , 'wp-e-commerce' ),
37
			'unit_price'  => __( 'Unit Price', 'wp-e-commerce' ),
38
			'quantity'    => __( 'Quantity'  , 'wp-e-commerce' ),
39
			'item_total'  => __( 'Item Total', 'wp-e-commerce' ),
40
		);
41
42
		$this->columns = apply_filters( 'wpsc_cart_item_table_columns', $this->columns, $this );
43
44
		$this->items = $wpsc_cart->cart_items;
45
	}
46
47
	private function prepare_cache() {
48
		$post_in = array();
49
50
		foreach ( $this->items as $item ) {
51
			$post_in[] = $item->product_id;
52
		}
53
54
		get_posts( array( 'post__in' => $post_in, 'post_type' => 'wpsc-product', 'post_status' => 'any' ) );
55
	}
56
57
	protected function get_table_classes() {
58
		$classes   = parent::get_table_classes();
59
		$classes[] = 'wpsc-cart-item-table';
60
		return $classes;
61
	}
62
63
	protected function get_columns() {}
64
65
	protected function column_default( $item, $key, $column ) {
66
		do_action( "wpsc_cart_item_table_column_{$column}", $item, $key );
67
	}
68
69
	protected function before_table() {
70
		do_action( 'wpsc_cart_item_table_before' );
71
	}
72
73
	public function display() {
74
		global $wpsc_cart;
75
76
		$this->before_table();
77
		include( WPSC_TE_V2_SNIPPETS_PATH . '/cart-item-table/display.php' );
78
		$this->after_table();
79
	}
80
81
	protected function get_total_shipping() {
82
		global $wpsc_cart;
83
		return $wpsc_cart->calculate_total_shipping();
84
	}
85
86
	protected function get_subtotal() {
87
		global $wpsc_cart;
88
		return $wpsc_cart->calculate_subtotal();
89
	}
90
91
	protected function get_total_price() {
92
		global $wpsc_cart;
93
		return $wpsc_cart->calculate_total_price();
94
	}
95
96
	protected function get_total_discount() {
97
		return wpsc_coupon_amount( false );
98
	}
99
100
	protected function get_tax() {
101
		return wpsc_cart_tax( false );
102
	}
103
104
	protected function tfoot_append() {
105
		do_action( 'wpsc_cart_item_table_tfoot' );
106
	}
107
108
	protected function after_table() {
109
		do_action( 'wpsc_cart_item_table_after' );
110
	}
111
112
	private function show_shipping_style() {
113
		if ( ! $this->show_shipping ) {
114
			echo 'style="display:none;"';
115
		}
116
	}
117
118
	private function show_tax_style() {
119
		if ( ! $this->show_tax ) {
120
			echo 'style="display:none;"';
121
		}
122
	}
123
124
	private function show_total_style() {
125
		if ( ! $this->show_total ) {
126
			echo 'style="display:none;"';
127
		}
128
	}
129
130
	protected function column_image( $item, $key ) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
		if ( $this->show_thumbnails ) : ?>
132
				<div class="wpsc-thumbnail wpsc-product-thumbnail">
133
					<?php if ( wpsc_has_product_thumbnail( $item->product_id ) ) : ?>
134
						<?php echo wpsc_get_product_thumbnail( $item->product_id, 'cart' ); ?>
135
					<?php else : ?>
136
						<?php wpsc_product_no_thumbnail_image( 'cart' ); ?>
137
					<?php endif; ?>
138
				</div>
139
			<?php endif;
140
	}
141
142
	protected function column_items( $item, $key ) {
143
		$product      = get_post( $item->product_id );
144
		$product_name = $item->product_name;
145
146
		if ( $product->post_parent ) {
147
			$permalink    = wpsc_get_product_permalink( $product->post_parent );
148
			$product_name = get_post_field( 'post_title', $product->post_parent );
149
		} else {
150
			$permalink = wpsc_get_product_permalink( $item->product_id );
151
		}
152
153
		$variations = array();
154
155
		if ( is_array( $item->variation_values ) ) {
156
			foreach ( $item->variation_values as $variation_set => $variation ) {
157
				$set_name       = get_term_field( 'name', $variation_set, 'wpsc-variation' );
158
				$variation_name = get_term_field( 'name', $variation    , 'wpsc-variation' );
159
160
				if ( ! is_wp_error( $set_name ) && ! is_wp_error( $variation_name ) ) {
161
					$variations[]   = '<span>' . esc_html( $set_name ) . ':</span> ' . esc_html( $variation_name );
162
				}
163
			}
164
		}
165
166
		$variations = implode( ', ', $variations );
167
168
		$separator = '';
169
170
		if ( ! empty( $variations ) && ! empty( $item->sku ) ) {
171
			$separator = ' | ';
172
		}
173
174
		?>
175
			<div class="wpsc-cart-item-description">
176
				<div class="wpsc-cart-item-title">
177
					<strong><a href="<?php echo $permalink; ?>"><?php echo esc_html( $product_name ); ?></a></strong>
178
				</div>
179
				<div class="wpsc-cart-item-details">
180
					<?php if ( ! empty( $item->sku ) ) : ?>
181
						<span class="wpsc-cart-item-sku"><span><?php esc_html_e( 'SKU', 'wp-e-commerce' ); ?>:</span> <?php echo esc_html( $item->sku ); ?></span>
182
					<?php endif ?>
183
184
					<?php if ( $separator ) : ?>
185
						<span class="separator"><?php echo $separator; ?></span>
186
					<?php endif ?>
187
188
					<?php if ( ! empty( $variations ) ) : ?>
189
						<span class="wpsc-cart-item-variations"><?php echo $variations; ?></span>
190
					<?php endif ?>
191
				</div>
192
				<?php $this->cart_item_description( $item, $key ); ?>
193
			</div>
194
		<?php
195
	}
196
197
	protected function cart_item_description( $item, $key ) {
198
		do_action( 'wpsc_cart_item_description', $item, $key );
199
	}
200
201
	protected function column_quantity( $item, $key ) {
202
		echo $item->quantity;
203
	}
204
205
	protected function column_unit_price( $item ) {
206
		echo wpsc_format_currency( $item->unit_price );
207
	}
208
209
	protected function column_item_total( $item ) {
210
		echo wpsc_format_currency( $item->unit_price * $item->quantity );
211
	}
212
}
213