Advertisement
Guest User

Untitled

a guest
Aug 24th, 2014
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.96 KB | None | 0 0
  1. <?php
  2. //Display Fields
  3. add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
  4. //JS to add fields for new variations
  5. add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
  6. //Save variation fields
  7. add_action( 'woocommerce_process_product_meta_variable', 'save_variable_fields', 10, 1 );
  8.  
  9. /**
  10.  * For each role create new price field for variations
  11.  *
  12. */
  13. function variable_fields( $loop, $variation_data ) {
  14. $specific_prices = unserialize($variation_data['_specific_price'][0]);
  15.  
  16. $roles = get_editable_roles();
  17. echo '<tr><h3>Roles and prices</h3></tr>';
  18.  
  19. foreach ($roles as $key => $role) {
  20.  
  21. ?>
  22.     <tr>
  23.     <td><?php echo $role['name']?></td>
  24.         <td>
  25.    
  26.             <?php
  27.             // Text Field
  28.             woocommerce_wp_text_input(
  29.                 array(
  30.                     'id'          => '_specific_price['.$loop.']['.$key.']',
  31.                     'desc_tip'    => 'false',
  32.                     'value'       => $specific_prices[$key]
  33.                 )
  34.             );
  35.             ?>
  36.         </td>
  37.     </tr>
  38. <?php
  39. }
  40. }
  41.  
  42. /**
  43.  * Create new fields for new variations
  44.  *
  45. */
  46. function variable_fields_js() {
  47. $roles = get_editable_roles();
  48. echo '<h3>Ceny pro skupiny zákazníků</h3>';
  49. foreach ($roles as $key => $role) {
  50.  
  51. ?>
  52.  
  53.     <tr>
  54.     <td><?php echo $role['name']?></td>
  55.         <td>
  56.    
  57.             <?php
  58.             // Text Field
  59.             woocommerce_wp_text_input(
  60.                 array(
  61.                     'id'          => '_specific_price['.$loop.']['.$key.']',
  62.                     'desc_tip'    => 'false',
  63.                     'value'       => $specific_prices[$key]
  64.                 )
  65.             );
  66.             ?>
  67.         </td>
  68.     </tr>
  69. <?php
  70. }
  71.  
  72. }
  73.  
  74. /**
  75.  * Save new fields for variations
  76.  *
  77. */
  78. function save_variable_fields( $post_id ) {
  79.     if (isset( $_POST['variable_sku'] ) ) :
  80.  
  81.         $variable_sku          = $_POST['variable_sku'];
  82.         $variable_post_id      = $_POST['variable_post_id'];
  83.        
  84.         // Text Field
  85.         $_text_field = $_POST['_specific_price'];
  86.        
  87.     for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
  88.             $variation_id = (int) $variable_post_id[$i];
  89.            
  90.       if ( isset( $_text_field[$i] ) ) {
  91.             update_post_meta( $variation_id, '_specific_price', $_text_field[$i] );
  92.            
  93.        
  94.         }
  95.         endfor;
  96.     endif;
  97. }
  98.  
  99.  
  100. // display the role prices in frontend
  101. add_filter( 'woocommerce_variation_price_html', 'w4dev_woocommerce_variation_prices_html', 10, 2);
  102. add_filter( 'woocommerce_variation_sale_price_html', 'w4dev_woocommerce_variation_prices_html', 10, 2);
  103. add_filter( 'woocommerce_get_price', 'w4dev_woocommerce_variation_prices_html', 10, 2);
  104.  
  105. function w4dev_woocommerce_variation_prices_html( $price, $product )
  106. {
  107.    
  108.     $specific_prices = unserialize($product->product_custom_fields['_specific_price'][0]);
  109.     $role = get_user_role();
  110.     if (isset($specific_prices[$role]) && $specific_prices[$role] != '')    
  111.       $price = $specific_prices[$role];
  112.  
  113.     return $price;
  114. }
  115.  
  116.  
  117. // Get user role
  118. function get_user_role() {
  119.     global $current_user;
  120.     $user_roles = $current_user->roles;
  121.     $user_role = array_shift($user_roles);
  122.  
  123.     return $user_role;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement