PageRenderTime 67ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/shortcodes/cart.php

https://github.com/scottpoulin/jigoshop
PHP | 320 lines | 255 code | 45 blank | 20 comment | 46 complexity | e32098b06ce258a4eaa1938e894e92a7 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Cart shortcode
  4. *
  5. * DISCLAIMER
  6. *
  7. * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  8. * versions in the future. If you wish to customise Jigoshop core for your needs,
  9. * please use our GitHub repository to publish essential changes for consideration.
  10. *
  11. * @package Jigoshop
  12. * @category Checkout
  13. * @author Jigoshop
  14. * @copyright Copyright Š 2011-2013 Jigoshop.
  15. * @license http://jigoshop.com/license/commercial-edition
  16. */
  17. function get_jigoshop_cart($atts) {
  18. return jigoshop_shortcode_wrapper('jigoshop_cart', $atts);
  19. }
  20. function jigoshop_cart($atts) {
  21. $errors = array();
  22. unset(jigoshop_session::instance()->selected_rate_id);
  23. // Process Discount Codes
  24. if (isset($_POST['apply_coupon']) && $_POST['apply_coupon'] && jigoshop::verify_nonce('cart')) :
  25. $coupon_code = sanitize_title( $_POST['coupon_code'] );
  26. jigoshop_cart::add_discount($coupon_code);
  27. // Update Shipping
  28. elseif (isset($_POST['calc_shipping']) && $_POST['calc_shipping'] && jigoshop::verify_nonce('cart')) :
  29. unset( jigoshop_session::instance()->chosen_shipping_method_id );
  30. $country = $_POST['calc_shipping_country'];
  31. $state = $_POST['calc_shipping_state'];
  32. $postcode = $_POST['calc_shipping_postcode'];
  33. if ($postcode && !jigoshop_validation::is_postcode($postcode, $country)) :
  34. jigoshop::add_error(__('Please enter a valid postcode/ZIP.', 'jigoshop'));
  35. $postcode = '';
  36. elseif ($postcode) :
  37. $postcode = jigoshop_validation::format_postcode($postcode, $country);
  38. endif;
  39. if ($country) :
  40. // Update customer location
  41. jigoshop_customer::set_location($country, $state, $postcode);
  42. jigoshop_customer::set_shipping_location($country, $state, $postcode);
  43. jigoshop::add_message(__('Shipping costs updated.', 'jigoshop'));
  44. else :
  45. jigoshop_customer::set_shipping_location('', '', '');
  46. jigoshop::add_message(__('Shipping costs updated.', 'jigoshop'));
  47. endif;
  48. elseif (isset($_POST['shipping_rates'])) :
  49. $rates_params = explode(":", $_POST['shipping_rates']);
  50. $available_methods = jigoshop_shipping::get_available_shipping_methods();
  51. $shipping_method = $available_methods[$rates_params[0]];
  52. if ($rates_params[1] != NULL) :
  53. jigoshop_session::instance()->selected_rate_id = $rates_params[1];
  54. endif;
  55. $shipping_method->choose(); // choses the method selected by user.
  56. endif;
  57. // Re-Calc prices. This needs to happen every time the cart page is loaded and after checking post results. It will happen twice for coupon.
  58. jigoshop_cart::calculate_totals();
  59. $result = jigoshop_cart::check_cart_item_stock();
  60. if (is_wp_error($result)) :
  61. jigoshop::add_error($result->get_error_message());
  62. endif;
  63. jigoshop::show_messages();
  64. if (sizeof(jigoshop_cart::$cart_contents) == 0) :
  65. echo '<p>' . __('Your cart is empty.', 'jigoshop') . '</p>';
  66. ?><p><a href="<?php echo esc_url( jigoshop_cart::get_shop_url() ); ?>" class="button"><?php _e('&larr; Return to Shop', 'jigoshop'); ?></a></p><?php
  67. return;
  68. endif;
  69. ?>
  70. <form action="<?php echo esc_url( jigoshop_cart::get_cart_url() ); ?>" method="post">
  71. <table class="shop_table cart" cellspacing="0">
  72. <thead>
  73. <tr>
  74. <th class="product-remove"></th>
  75. <th class="product-thumbnail"></th>
  76. <th class="product-name"><span class="nobr"><?php _e('Product Name', 'jigoshop'); ?></span></th>
  77. <th class="product-price"><span class="nobr"><?php _e('Unit Price', 'jigoshop'); ?></span></th>
  78. <th class="product-quantity"><?php _e('Quantity', 'jigoshop'); ?></th>
  79. <th class="product-subtotal"><?php _e('Price', 'jigoshop'); ?></th>
  80. </tr>
  81. <?php do_action('jigoshop_shop_table_cart_head'); ?>
  82. </thead>
  83. <tbody>
  84. <?php
  85. if (sizeof(jigoshop_cart::$cart_contents) > 0) :
  86. foreach (jigoshop_cart::$cart_contents as $cart_item_key => $values) :
  87. $_product = $values['data'];
  88. if ($_product->exists() && $values['quantity'] > 0) :
  89. $additional_description = jigoshop_cart::get_item_data($values);
  90. ?>
  91. <tr>
  92. <td class="product-remove"><a href="<?php echo esc_url( jigoshop_cart::get_remove_url($cart_item_key) ); ?>" class="remove" title="<?php echo esc_attr( __('Remove this item.', 'jigoshop') ); ?>">&times;</a></td>
  93. <td class="product-thumbnail"><a href="<?php echo esc_url( apply_filters('jigoshop_product_url_display_in_cart', get_permalink($values['product_id']), $cart_item_key) ); ?>">
  94. <?php
  95. if ($values['variation_id'] && has_post_thumbnail($values['variation_id'])) {
  96. echo get_the_post_thumbnail($values['variation_id'], 'shop_tiny');
  97. } else if (has_post_thumbnail($values['product_id'])) {
  98. echo get_the_post_thumbnail($values['product_id'], 'shop_tiny');
  99. } else {
  100. echo '<img src="' . jigoshop::assets_url() . '/assets/images/placeholder.png" alt="Placeholder" width="' . jigoshop::get_var('shop_tiny_w') . '" height="' . jigoshop::get_var('shop_tiny_h') . '" />';
  101. }
  102. ?></a>
  103. </td>
  104. <td class="product-name">
  105. <a href="<?php echo esc_url( apply_filters('jigoshop_product_url_display_in_cart', get_permalink($values['product_id']), $cart_item_key) ); ?>"><?php echo apply_filters('jigoshop_cart_product_title', $_product->get_title(), $_product); ?></a>
  106. <?php echo $additional_description; ?>
  107. <?php
  108. if ( !empty( $values['variation_id'] )) {
  109. $product_id = $values['variation_id'];
  110. } else {
  111. $product_id = $values['product_id'];
  112. }
  113. $custom_products = (array) jigoshop_session::instance()->customized_products;
  114. $custom = isset( $custom_products[$product_id] ) ? $custom_products[$product_id] : '';
  115. if ( ! empty( $custom_products[$product_id] ) ) :
  116. ?>
  117. <dl class="customization">
  118. <dt class="customized_product_label"><?php echo apply_filters('jigoshop_customized_product_label', __('Personal: ','jigoshop') ); ?></dt>
  119. <dd class="customized_product"><?php echo esc_textarea( $custom ); ?></dd>
  120. </dl>
  121. <?php
  122. endif;
  123. ?>
  124. </td>
  125. <td class="product-price"><?php echo apply_filters( 'jigoshop_product_price_display_in_cart', jigoshop_price($_product->get_price_excluding_tax()), $values['product_id'], $values ); ?></td>
  126. <td class="product-quantity">
  127. <?php ob_start(); ?>
  128. <div class="quantity"><input name="cart[<?php echo $cart_item_key ?>][qty]" value="<?php echo esc_attr( $values['quantity'] ); ?>" size="4" title="Qty" class="input-text qty text" maxlength="12" /></div>
  129. <?php
  130. $quantity_display = ob_get_contents();
  131. ob_end_clean();
  132. echo apply_filters( 'jigoshop_product_quantity_display_in_cart', $quantity_display, $values['product_id'], $values );
  133. ?>
  134. </td>
  135. <td class="product-subtotal"><?php echo apply_filters( 'jigoshop_product_subtotal_display_in_cart', jigoshop_price($_product->get_price_excluding_tax() * $values['quantity']), $values['product_id'], $values ); ?></td>
  136. </tr>
  137. <?php
  138. endif;
  139. endforeach;
  140. endif;
  141. do_action('jigoshop_shop_table_cart_body');
  142. ?>
  143. </tbody>
  144. <tfoot>
  145. <tr>
  146. <td colspan="6" class="actions">
  147. <?php $coupons = JS_Coupons::get_coupons(); if(!empty($coupons)): ?>
  148. <div class="coupon">
  149. <label for="coupon_code"><?php _e('Coupon', 'jigoshop'); ?>:</label> <input type="text" name="coupon_code" class="input-text" id="coupon_code" value="" />
  150. <input type="submit" class="button" name="apply_coupon" value="<?php _e('Apply Coupon', 'jigoshop'); ?>" />
  151. </div>
  152. <?php endif; ?>
  153. <?php jigoshop::nonce_field('cart') ?>
  154. <?php if ( Jigoshop_Base::get_options()->get_option( 'jigoshop_cart_shows_shop_button' ) == 'no' ) : ?>
  155. <input type="submit" class="button" name="update_cart" value="<?php _e('Update Shopping Cart', 'jigoshop'); ?>" /> <a href="<?php echo esc_url( jigoshop_cart::get_checkout_url() ); ?>" class="checkout-button button-alt"><?php _e('Proceed to Checkout &rarr;', 'jigoshop'); ?></a>
  156. <?php else : ?>
  157. <input type="submit" class="button" name="update_cart" value="<?php _e('Update Shopping Cart', 'jigoshop'); ?>" />
  158. <?php endif; ?>
  159. </td>
  160. </tr>
  161. <?php if (count(jigoshop_cart::$applied_coupons)) : ?>
  162. <tr>
  163. <td colspan="6" class="applied-coupons">
  164. <div>
  165. <span class="applied-coupons-label"><?php _e('Applied Coupons: ', 'jigoshop'); ?></span>
  166. <?php foreach ( jigoshop_cart::$applied_coupons as $code ) : ?>
  167. <a href="?unset_coupon=<?php echo $code; ?>" id="<?php echo $code; ?>" class="applied-coupons-values"><?php echo $code; ?>
  168. <span class="close">&times;</span>
  169. </a>
  170. <?php endforeach; ?>
  171. </div>
  172. </td>
  173. </tr>
  174. <?php endif; ?>
  175. <?php if ( Jigoshop_Base::get_options()->get_option( 'jigoshop_cart_shows_shop_button' ) == 'yes' ) : ?>
  176. <tr>
  177. <td colspan="6" class="actions">
  178. <a href="<?php echo esc_url( jigoshop_cart::get_shop_url() ); ?>" class="checkout-button button-alt" style="float:left;"><?php _e('&larr; Return to Shop', 'jigoshop'); ?></a>
  179. <a href="<?php echo esc_url( jigoshop_cart::get_checkout_url() ); ?>" class="checkout-button button-alt"><?php _e('Proceed to Checkout &rarr;', 'jigoshop'); ?></a>
  180. </td>
  181. </tr>
  182. <?php endif;
  183. do_action('jigoshop_shop_table_cart_foot');
  184. ?>
  185. </tfoot>
  186. <?php do_action('jigoshop_shop_table_cart'); ?>
  187. </table>
  188. </form>
  189. <div class="cart-collaterals">
  190. <?php do_action('cart-collaterals'); ?>
  191. <div class="cart_totals">
  192. <?php
  193. // Hide totals if customer has set location and there are no methods going there
  194. $available_methods = jigoshop_shipping::get_available_shipping_methods();
  195. $jigoshop_options = Jigoshop_Base::get_options();
  196. if ($available_methods || !jigoshop_customer::get_shipping_country() || !jigoshop_shipping::is_enabled()) :
  197. do_action( 'jigoshop_before_cart_totals' );
  198. ?>
  199. <h2><?php _e('Cart Totals', 'jigoshop'); ?></h2>
  200. <div class="cart_totals_table">
  201. <table cellspacing="0" cellpadding="0">
  202. <tbody>
  203. <tr>
  204. <?php $price_label = jigoshop_cart::show_retail_price() ? __('Retail Price', 'jigoshop') : __('Subtotal', 'jigoshop'); ?>
  205. <th class="cart-row-subtotal-title"><?php echo $price_label; ?></th>
  206. <td class="cart-row-subtotal"><?php echo jigoshop_cart::get_cart_subtotal(true,false,true); ?></td>
  207. </tr>
  208. <?php if (jigoshop_cart::get_cart_shipping_total()) : ?>
  209. <tr>
  210. <th class="cart-row-shipping-title"><?php _e('Shipping', 'jigoshop'); ?> <small><?php echo _x('To: ','shipping destination','jigoshop') . __(jigoshop_customer::get_shipping_country_or_state(), 'jigoshop'); ?></small></th>
  211. <td class="cart-row-shipping"><?php echo jigoshop_cart::get_cart_shipping_total(true,true); ?> <small><?php echo jigoshop_cart::get_cart_shipping_title(); ?></small></td>
  212. </tr>
  213. <?php endif; ?>
  214. <?php if ( jigoshop_cart::show_retail_price() && Jigoshop_Base::get_options()->get_option( 'jigoshop_prices_include_tax' ) == 'no') : ?>
  215. <tr>
  216. <th class="cart-row-subtotal-title"><?php _e('Subtotal', 'jigoshop'); ?></th>
  217. <td class="cart-row-subtotal"><?php echo jigoshop_cart::get_cart_subtotal(true, true); ?></td>
  218. </tr>
  219. <?php elseif ( jigoshop_cart::show_retail_price() ) : ?>
  220. <tr>
  221. <th class="cart-row-subtotal-title"><?php _e('Subtotal', 'jigoshop'); ?></th>
  222. <?php
  223. $price = jigoshop_cart::$cart_contents_total_ex_tax + jigoshop_cart::$shipping_total;
  224. $price = jigoshop_price($price, array('ex_tax_label' => 1));
  225. ?>
  226. <td class="cart-row-subtotal"><?php echo $price; ?></td>
  227. </tr>
  228. <?php endif; ?>
  229. <?php if ( jigoshop_cart::tax_after_coupon() ) : ?>
  230. <tr class="discount">
  231. <th class="cart-row-discount-title"><?php _e('Discount', 'jigoshop'); ?></th>
  232. <td class="cart-row-discount">-<?php echo jigoshop_cart::get_total_discount(); ?></td>
  233. </tr>
  234. <?php endif; ?>
  235. <?php if (Jigoshop_Base::get_options()->get_option('jigoshop_calc_taxes') == 'yes') :
  236. foreach (jigoshop_cart::get_applied_tax_classes() as $tax_class) :
  237. if (jigoshop_cart::get_tax_for_display($tax_class)) : ?>
  238. <tr>
  239. <th class="cart-row-tax-title"><?php echo jigoshop_cart::get_tax_for_display($tax_class) ?></th>
  240. <td class="cart-row-tax"><?php echo jigoshop_cart::get_tax_amount($tax_class) ?></td>
  241. </tr>
  242. <?php
  243. endif;
  244. endforeach;
  245. endif; ?>
  246. <?php if ( !jigoshop_cart::tax_after_coupon() && jigoshop_cart::get_total_discount() ) : ?>
  247. <tr class="discount">
  248. <th class="cart-row-discount-title"><?php _e('Discount', 'jigoshop'); ?></th>
  249. <td class="cart-row-discount">-<?php echo jigoshop_cart::get_total_discount(); ?></td>
  250. </tr>
  251. <?php endif; ?>
  252. <tr>
  253. <th class="cart-row-total-title"><strong><?php _e('Total', 'jigoshop'); ?></strong></th>
  254. <td class="cart-row-total"><strong><?php echo jigoshop_cart::get_total(); ?></strong></td>
  255. </tr>
  256. </tbody>
  257. </table>
  258. </div>
  259. <?php
  260. do_action( 'jigoshop_after_cart_totals' );
  261. else :
  262. echo '<p>' . __(jigoshop_shipping::get_shipping_error_message(), 'jigoshop') . '</p>';
  263. endif;
  264. ?>
  265. </div>
  266. <?php
  267. do_action( 'jigoshop_before_shipping_calculator' );
  268. ?>
  269. <?php jigoshop_shipping_calculator(); ?>
  270. <?php
  271. do_action( 'jigoshop_after_shipping_calculator' );
  272. ?>
  273. </div>
  274. <?php
  275. }