Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );
  2. function woocommerce_total_product_price() {
  3. global $woocommerce, $product;
  4. // let's setup our divs
  5. echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>',__('Product Total:','woocommerce'),'<span class="price">'.$product->get_price().'</span>');
  6. ?>
  7. <script>
  8. jQuery(function($){
  9. var price = <?php echo $product->get_price(); ?>,
  10. currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
  11.  
  12. $('[name=quantity]').change(function(){
  13. if (!(this.value < 1)) {
  14.  
  15. var product_total = parseFloat(price * this.value);
  16.  
  17. $('#product_total_price .price').html( currency + product_total.toFixed(2));
  18.  
  19. }
  20. });
  21. });
  22. </script>
  23. <?php
  24. }
  25.  
  26. function add_gift_wrap_field() {
  27. echo '<table class="variations" cellspacing="0">
  28. <tbody>
  29. <tr>
  30. <td class="label"><label>More price: </label></td>
  31. <td class="value">
  32. <input type="number" name="more_price"/>
  33. </td>
  34. </tr>
  35. </tbody>
  36. </table>';
  37. }
  38. add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' );
  39.  
  40. function save_gift_wrap_fee( $cart_item_data, $product_id ) {
  41.  
  42. if( isset( $_POST['more_price'] ) && $_POST['more_price'] != '' ) {
  43. $cart_item_data[ "more_price_fee" ] = $_POST['more_price'];
  44. }
  45. return $cart_item_data;
  46.  
  47. }
  48. add_filter( 'woocommerce_add_cart_item_data', 'save_gift_wrap_fee', 99, 2 );
  49.  
  50. function calculate_gift_wrap_fee( $cart_object ) {
  51. if( !WC()->session->__isset( "reload_checkout" )) {
  52. /* Gift wrap price */
  53. // $additionalPrice = 100;
  54. foreach ( $cart_object->cart_contents as $key => $value ) {
  55. if( isset( $value["more_price_fee"] ) ) {
  56. /* Woocommerce 3.0 + */
  57. $orgPrice = floatval( $value['data']->get_price() );
  58. $more = floatval($value["more_price_fee"]);
  59. $value['data']->set_price( $orgPrice + $more );
  60. }
  61. }
  62. }
  63. }
  64. add_action( 'woocommerce_before_calculate_totals', 'calculate_gift_wrap_fee', 99 );
  65.  
  66. function render_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
  67. $meta_items = array();
  68. /* Woo 2.4.2 updates */
  69. if( !empty( $cart_data ) ) {
  70. $meta_items = $cart_data;
  71. }
  72. if( isset( $cart_item["more_price_fee"] ) ) {
  73. $meta_items[] = array( "name" => "More", "value" => $cart_item['more_price_fee'] );
  74. }
  75. return $meta_items;
  76. }
  77. add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 99, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement