Advertisement
olie480

Woocommerce Add Custom Data to Woocommerce orders II

Oct 24th, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.41 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Define the new custom meta product field
  5.  **/
  6. $product_addons = array(
  7.     array(
  8.         'name' => 'Deliverydate',
  9.         'description' => '',
  10.         'type' => 'custom',
  11.         'position' => 0,
  12.         'options' => array(
  13.             array(
  14.                 'label' => 'Deliverydate',
  15.                 'price' => ''
  16.             )
  17.         ),
  18.         'required' => 1
  19.     )
  20. );
  21.    
  22.    
  23.  
  24.  
  25.  
  26.  
  27. add_action('woocommerce_before_add_to_cart_button', 'product_custom_addons');
  28. /**
  29.  * Add custom input fields to the single product
  30.  **/
  31. function product_custom_addons() {
  32.     global $post, $product_addons;
  33.  
  34.     if (is_array($product_addons) && sizeof($product_addons)>0) foreach ($product_addons as $addon) :
  35.                
  36.         if (!isset($addon['name'])) continue;
  37.        
  38.         ?>
  39.         <div class="product-addon product-addon-<?php echo sanitize_title($addon['name']); ?>">
  40.             <?php if ($addon['name']) : ?><h3><?php echo wptexturize($addon['name']); ?> <?php if ($addon['type']=='file_upload') echo sprintf(__('(max size %s)', 'wc_product_addons'), $this->max_upload_size()); ?></h3><?php endif; ?>
  41.             <?php if ($addon['description']) : ?><p><?php echo wptexturize($addon['description']); ?></p><?php endif; ?>
  42.             <?php
  43.             switch ($addon['type']) :
  44.                
  45.                 case "custom" :
  46.                     foreach ($addon['options'] as $option) :
  47.                        
  48.                         $current_value = (isset($_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )])) ? $_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )] : '';
  49.                        
  50.                         $price = ($option['price']>0) ? ' (' . woocommerce_price($option['price']) . ')' : '';
  51.                         echo '<p class="form-row form-row-wide"><label>'. wptexturize($option['label']) . $price .': <input type="text" class="input-text" name="addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] ) .'" value="'.$current_value.'" /></label></p>';
  52.                    
  53.                     endforeach;
  54.                 break;
  55.                
  56.             endswitch;
  57.             ?>
  58.             <div class="clear"></div>
  59.         </div>
  60.         <?php
  61.     endforeach;
  62.  
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_custom_data', 10, 2 );
  72. /**
  73.  * Add custom input data to the cart item
  74.  **/
  75. function add_cart_item_custom_data( $cart_item_meta, $product_id ) {
  76.     global $woocommerce, $product_addons;
  77.    
  78.     $cart_item_meta['addons'] = array();
  79.    
  80.     if (is_array($product_addons) && sizeof($product_addons)>0) foreach ($product_addons as $addon) :
  81.                
  82.         if (!isset($addon['name'])) continue;
  83.        
  84.         switch ($addon['type']) :
  85.  
  86.             case "custom" :
  87.                
  88.                 // Posted var = label, value = custom
  89.                 foreach ($addon['options'] as $option) :
  90.                    
  91.                     $posted = (isset( $_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )] )) ? $_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )] : '';
  92.                    
  93.                     if (!$posted) continue;
  94.                    
  95.                     $cart_item_meta['addons'][] = array(
  96.                         'name'      => esc_attr( $option['label'] ),
  97.                         'value'     => esc_attr( stripslashes( trim( $posted ) ) ),
  98.                         'price'     => esc_attr( $option['price'] )
  99.                     );                             
  100.                    
  101.                 endforeach;
  102.                
  103.             break;
  104.            
  105.         endswitch;
  106.                                                        
  107.     endforeach;
  108.    
  109.     return $cart_item_meta;
  110.    
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. add_filter( 'woocommerce_get_cart_item_from_session', 'get_cart_custom_item_from_session', 10, 2 );
  119. /**
  120.  * Get custom meta data from session
  121.  **/
  122. function get_cart_custom_item_from_session( $cart_item, $values ) {
  123.                
  124.     if (isset($values['addons'])) :
  125.         $cart_item['addons'] = $values['addons'];
  126.    
  127.         $cart_item = add_custom_cart_item( $cart_item );
  128.     endif;
  129.    
  130.     return $cart_item;
  131.    
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139. add_filter( 'woocommerce_get_item_data', 'get_item_custom_data', 10, 2 );
  140. /**
  141.  * Get product item data
  142.  **/
  143. function get_item_custom_data( $other_data, $cart_item ) {
  144.                    
  145.     if (isset($cart_item['addons'])) :
  146.        
  147.         foreach ($cart_item['addons'] as $addon) :
  148.            
  149.             $name = $addon['name'];
  150.             if ($addon['price']>0) $name .= ' (' . woocommerce_price($addon['price']) . ')';
  151.            
  152.             $other_data[] = array(
  153.                 'name' => $name,
  154.                 'value' => $addon['value'],
  155.                 'display' => isset($addon['display']) ? $addon['display'] : ''
  156.             );
  157.            
  158.         endforeach;
  159.        
  160.     endif;
  161.    
  162.     return $other_data;
  163.        
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170. add_filter( 'woocommerce_add_cart_item', 'add_custom_cart_item', 10, 1 );
  171. /**
  172.  * Add cart item
  173.  **/
  174. function add_custom_cart_item( $cart_item ) {
  175.                
  176.     // Adjust price if addons are set
  177.     if (isset($cart_item['addons'])) :
  178.        
  179.         $extra_cost = 0;
  180.        
  181.         foreach ($cart_item['addons'] as $addon) :
  182.            
  183.             if ($addon['price']>0) $extra_cost += $addon['price'];
  184.            
  185.         endforeach;
  186.        
  187.         $cart_item['data']->adjust_price( $extra_cost );
  188.        
  189.     endif;
  190.    
  191.     return $cart_item;
  192.    
  193. }
  194.  
  195.  
  196.  
  197.  
  198.  
  199. add_action( 'woocommerce_order_item_meta', 'order_item_custom_meta', 10, 2 );
  200. /**
  201.  * Add custom meta data to order
  202.  **/
  203. function order_item_custom_meta( $item_meta, $cart_item ) {
  204.            
  205.     // Add the fields
  206.     if (isset($cart_item['addons'])) :
  207.        
  208.         foreach ($cart_item['addons'] as $addon) :
  209.            
  210.             $name = $addon['name'];
  211.             if ($addon['price']>0) $name .= ' (' . strip_tags(woocommerce_price($addon['price'])) . ')';
  212.            
  213.             $item_meta->add( $name, $addon['value'] );
  214.            
  215.         endforeach;
  216.  
  217.     endif;
  218.  
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. add_filter( 'woocommerce_add_to_cart_validation', 'validate_add_custom_cart_item', 10, 3 );
  227. /**
  228.  * Validate the custom meta field before adding to cart
  229.  **/
  230. function validate_add_custom_cart_item( $passed, $product_id, $qty ) {
  231.     global $woocommerce, $product_addons;
  232.    
  233.     if (is_array($product_addons) && sizeof($product_addons)>0) foreach ($product_addons as $addon) {
  234.                
  235.         if (!isset($addon['name'])) continue;
  236.         if (!isset($addon['required'])) continue;
  237.        
  238.         if ($addon['required']) {
  239.        
  240.             switch ($addon['type']) :
  241.                
  242.                 case "custom" :
  243.                    
  244.                     foreach ($addon['options'] as $option) {
  245.                        
  246.                         $posted = (isset( $_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )] )) ? $_POST['addon-' . sanitize_title( $addon['name'] ) . '-' . sanitize_title( $option['label'] )] : '';
  247.                        
  248.                         if (!$posted || sizeof($posted)==0) {
  249.                             $passed = false;
  250.                             break;
  251.                         }                          
  252.                     }
  253.                    
  254.                 break;
  255.  
  256.             endswitch;
  257.  
  258.             if (!$passed) {
  259.                 $woocommerce->add_error( sprintf( __('"%s" is a required field.', 'woocommerce'), $addon['name']) );
  260.                 break;
  261.             }
  262.         }
  263.        
  264.         do_action('woocommerce_validate_posted_addon_data', $addon);
  265.                                                        
  266.     }
  267.    
  268.     return $passed;
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement