PageRenderTime 26ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/shortcodes/cart.php

https://github.com/billortell/jigoshop
PHP | 200 lines | 147 code | 33 blank | 20 comment | 29 complexity | cfcb47f9d3fb49ce01f9c5d80bb7eb42 MD5 | raw file
  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 Jigowatt
  14. * @copyright Copyright (c) 2011 Jigowatt Ltd.
  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. // Process Discount Codes
  23. if (isset($_POST['apply_coupon']) && $_POST['apply_coupon'] && jigoshop::verify_nonce('cart')) :
  24. $coupon_code = stripslashes(trim($_POST['coupon_code']));
  25. jigoshop_cart::add_discount($coupon_code);
  26. // Update Shipping
  27. elseif (isset($_POST['calc_shipping']) && $_POST['calc_shipping'] && jigoshop::verify_nonce('cart')) :
  28. unset($_SESSION['_chosen_method_id']);
  29. $country = $_POST['calc_shipping_country'];
  30. $state = $_POST['calc_shipping_state'];
  31. $postcode = $_POST['calc_shipping_postcode'];
  32. if ($postcode && !jigoshop_validation::is_postcode( $postcode, $country )) :
  33. jigoshop::add_error( __('Please enter a valid postcode/ZIP.','jigoshop') );
  34. $postcode = '';
  35. elseif ($postcode) :
  36. $postcode = jigoshop_validation::format_postcode( $postcode, $country );
  37. endif;
  38. if ($country) :
  39. // Update customer location
  40. jigoshop_customer::set_location( $country, $state, $postcode );
  41. jigoshop_customer::set_shipping_location( $country, $state, $postcode );
  42. // Re-calc price
  43. jigoshop_cart::calculate_totals();
  44. jigoshop::add_message( __('Shipping costs updated.', 'jigoshop') );
  45. else :
  46. jigoshop_customer::set_shipping_location( '', '', '' );
  47. jigoshop::add_message( __('Shipping costs updated.', 'jigoshop') );
  48. endif;
  49. endif;
  50. $result = jigoshop_cart::check_cart_item_stock();
  51. if (is_wp_error($result)) :
  52. jigoshop::add_error( $result->get_error_message() );
  53. endif;
  54. jigoshop::show_messages();
  55. if (sizeof(jigoshop_cart::$cart_contents)==0) :
  56. echo '<p>'.__('Your cart is empty.', 'jigoshop').'</p>';
  57. echo '<p><a class="button" href="'.get_permalink(get_option('jigoshop_shop_page_id')).'">'.__('&larr; Return To Shop', 'jigoshop').'</a></p>';
  58. return;
  59. endif;
  60. ?>
  61. <form action="<?php echo jigoshop_cart::get_cart_url(); ?>" method="post">
  62. <table class="shop_table cart" cellspacing="0">
  63. <thead>
  64. <tr>
  65. <th class="product-remove"></th>
  66. <th class="product-thumbnail"></th>
  67. <th class="product-name"><span class="nobr"><?php _e('Product Name', 'jigoshop'); ?></span></th>
  68. <th class="product-price"><span class="nobr"><?php _e('Unit Price', 'jigoshop'); ?></span></th>
  69. <th class="product-quantity"><?php _e('Quantity', 'jigoshop'); ?></th>
  70. <th class="product-subtotal"><?php _e('Price', 'jigoshop'); ?></th>
  71. </tr>
  72. </thead>
  73. <tbody>
  74. <?php
  75. if (sizeof(jigoshop_cart::$cart_contents)>0) :
  76. foreach (jigoshop_cart::$cart_contents as $cart_item_key => $values) :
  77. $_product = $values['data'];
  78. if ($_product->exists() && $values['quantity']>0) :
  79. $additional_description = '';
  80. if($_product instanceof jigoshop_product_variation && is_array($values['variation'])) {
  81. $additional_description = jigoshop_get_formatted_variation( $values['variation'] );
  82. }
  83. ?>
  84. <tr>
  85. <td class="product-remove"><a href="<?php echo jigoshop_cart::get_remove_url($cart_item_key); ?>" class="remove" title="<?php echo __('Remove this item.', 'jigoshop'); ?>">&times;</a></td>
  86. <td class="product-thumbnail"><a href="<?php get_permalink($values['product_id']); ?>">
  87. <?php
  88. if ($values['variation_id'] && has_post_thumbnail($values['variation_id'])) {
  89. echo get_the_post_thumbnail($values['variation_id'], 'shop_tiny');
  90. } else if (has_post_thumbnail($values['product_id'])) {
  91. echo get_the_post_thumbnail($values['product_id'], 'shop_tiny');
  92. } else {
  93. echo '<img src="'.jigoshop::plugin_url(). '/assets/images/placeholder.png" alt="Placeholder" width="'.jigoshop::get_var('shop_tiny_w').'" height="'.jigoshop::get_var('shop_tiny_h').'" />';
  94. }
  95. ?>
  96. </a></td>
  97. <td class="product-name">
  98. <a href="<?php echo get_permalink($values['product_id']); ?>"><?php echo apply_filters('jigoshop_cart_product_title', $_product->get_title(), $_product); ?></a>
  99. <?php echo $additional_description; ?>
  100. </td>
  101. <td class="product-price"><?php echo jigoshop_price($_product->get_price()); ?></td>
  102. <td class="product-quantity"><div class="quantity"><input name="cart[<?php echo $cart_item_key?>][qty]" value="<?php echo $values['quantity']; ?>" size="4" title="Qty" class="input-text qty text" maxlength="12" /></div></td>
  103. <td class="product-subtotal"><?php echo jigoshop_price($_product->get_price()*$values['quantity']); ?></td>
  104. </tr>
  105. <?php
  106. endif;
  107. endforeach;
  108. endif;
  109. do_action( 'jigoshop_shop_table_cart' );
  110. ?>
  111. <tr>
  112. <td colspan="6" class="actions">
  113. <div class="coupon">
  114. <label for="coupon_code"><?php _e('Coupon', 'jigoshop'); ?>:</label> <input name="coupon_code" class="input-text" id="coupon_code" value="" /> <input type="submit" class="button" name="apply_coupon" value="<?php _e('Apply Coupon', 'jigoshop'); ?>" />
  115. </div>
  116. <?php jigoshop::nonce_field('cart') ?>
  117. <input type="submit" class="button" name="update_cart" value="<?php _e('Update Shopping Cart', 'jigoshop'); ?>" /> <a href="<?php echo jigoshop_cart::get_checkout_url(); ?>" class="checkout-button button-alt"><?php _e('Proceed to Checkout &rarr;', 'jigoshop'); ?></a>
  118. </td>
  119. </tr>
  120. </tbody>
  121. </table>
  122. </form>
  123. <div class="cart-collaterals">
  124. <?php do_action('cart-collaterals'); ?>
  125. <div class="cart_totals">
  126. <?php
  127. // Hide totals if customer has set location and there are no methods going there
  128. $available_methods = jigoshop_shipping::get_available_shipping_methods();
  129. if ($available_methods || !jigoshop_customer::get_shipping_country() || !jigoshop_shipping::$enabled ) :
  130. ?>
  131. <h2><?php _e('Cart Totals', 'jigoshop'); ?></h2>
  132. <table cellspacing="0" cellpadding="0">
  133. <tbody>
  134. <tr>
  135. <th><?php _e('Subtotal', 'jigoshop'); ?></th>
  136. <td><?php echo jigoshop_cart::get_cart_subtotal(); ?></td>
  137. </tr>
  138. <?php if (jigoshop_cart::get_cart_shipping_total()) : ?><tr>
  139. <th><?php _e('Shipping', 'jigoshop'); ?> <small><?php echo jigoshop_countries::shipping_to_prefix().' '.jigoshop_countries::$countries[ jigoshop_customer::get_shipping_country() ]; ?></small></th>
  140. <td><?php echo jigoshop_cart::get_cart_shipping_total(); ?> <small><?php echo jigoshop_cart::get_cart_shipping_title(); ?></small></td>
  141. </tr><?php endif; ?>
  142. <?php if (jigoshop_cart::get_cart_tax()) : ?><tr>
  143. <th><?php _e('Tax', 'jigoshop'); ?> <?php if (jigoshop_customer::is_customer_outside_base()) : ?><small><?php echo sprintf(__('estimated for %s', 'jigoshop'), jigoshop_countries::estimated_for_prefix() . jigoshop_countries::$countries[ jigoshop_countries::get_base_country() ] ); ?></small><?php endif; ?></th>
  144. <td><?php
  145. echo jigoshop_cart::get_cart_tax();
  146. ?></td>
  147. </tr><?php endif; ?>
  148. <?php if (jigoshop_cart::get_total_discount()) : ?><tr class="discount">
  149. <th><?php _e('Discount', 'jigoshop'); ?></th>
  150. <td>-<?php echo jigoshop_cart::get_total_discount(); ?></td>
  151. </tr><?php endif; ?>
  152. <tr>
  153. <th><strong><?php _e('Total', 'jigoshop'); ?></strong></th>
  154. <td><strong><?php echo jigoshop_cart::get_total(); ?></strong></td>
  155. </tr>
  156. </tbody>
  157. </table>
  158. <?php
  159. else :
  160. echo '<p>'.__('Sorry, it seems that there are no available shipping methods to your location. Please contact us if you require assistance or wish to make alternate arrangements.', 'jigoshop').'</p>';
  161. endif;
  162. ?>
  163. </div>
  164. <?php jigoshop_shipping_calculator(); ?>
  165. </div>
  166. <?php
  167. }