Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //Display Fields
- add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
- //JS to add fields for new variations
- add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
- //Save variation fields
- add_action( 'woocommerce_process_product_meta_variable', 'save_variable_fields', 10, 1 );
- /**
- * For each role create new price field for variations
- *
- */
- function variable_fields( $loop, $variation_data ) {
- $specific_prices = unserialize($variation_data['_specific_price'][0]);
- $roles = get_editable_roles();
- echo '<tr><h3>Roles and prices</h3></tr>';
- foreach ($roles as $key => $role) {
- ?>
- <tr>
- <td><?php echo $role['name']?></td>
- <td>
- <?php
- // Text Field
- woocommerce_wp_text_input(
- array(
- 'id' => '_specific_price['.$loop.']['.$key.']',
- 'desc_tip' => 'false',
- 'value' => $specific_prices[$key]
- )
- );
- ?>
- </td>
- </tr>
- <?php
- }
- }
- /**
- * Create new fields for new variations
- *
- */
- function variable_fields_js() {
- $roles = get_editable_roles();
- echo '<h3>Ceny pro skupiny zákazníků</h3>';
- foreach ($roles as $key => $role) {
- ?>
- <tr>
- <td><?php echo $role['name']?></td>
- <td>
- <?php
- // Text Field
- woocommerce_wp_text_input(
- array(
- 'id' => '_specific_price['.$loop.']['.$key.']',
- 'desc_tip' => 'false',
- 'value' => $specific_prices[$key]
- )
- );
- ?>
- </td>
- </tr>
- <?php
- }
- }
- /**
- * Save new fields for variations
- *
- */
- function save_variable_fields( $post_id ) {
- if (isset( $_POST['variable_sku'] ) ) :
- $variable_sku = $_POST['variable_sku'];
- $variable_post_id = $_POST['variable_post_id'];
- // Text Field
- $_text_field = $_POST['_specific_price'];
- for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
- $variation_id = (int) $variable_post_id[$i];
- if ( isset( $_text_field[$i] ) ) {
- update_post_meta( $variation_id, '_specific_price', $_text_field[$i] );
- }
- endfor;
- endif;
- }
- // display the role prices in frontend
- add_filter( 'woocommerce_variation_price_html', 'w4dev_woocommerce_variation_prices_html', 10, 2);
- add_filter( 'woocommerce_variation_sale_price_html', 'w4dev_woocommerce_variation_prices_html', 10, 2);
- add_filter( 'woocommerce_get_price', 'w4dev_woocommerce_variation_prices_html', 10, 2);
- function w4dev_woocommerce_variation_prices_html( $price, $product )
- {
- $specific_prices = unserialize($product->product_custom_fields['_specific_price'][0]);
- $role = get_user_role();
- if (isset($specific_prices[$role]) && $specific_prices[$role] != '')
- $price = $specific_prices[$role];
- return $price;
- }
- // Get user role
- function get_user_role() {
- global $current_user;
- $user_roles = $current_user->roles;
- $user_role = array_shift($user_roles);
- return $user_role;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement