Advertisement
sparkweb

Capability-Based Pricing

Apr 17th, 2012
831
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.34 KB | None | 0 0
  1. add_action( 'admin_init', 'jtd_add_theme_caps');
  2. function jtd_add_theme_caps() {
  3. /*
  4. $asia = add_role('asia_distributor', 'Asia Distributor', array(
  5.     'read'         => true,
  6.     'edit_posts'   => false,
  7.     'delete_posts' => false
  8. ));
  9. */ // only has to run once - or use the Members plugin to create these
  10.    
  11.     $retail = get_role( 'retailer' );
  12.     $whole  = get_role( 'wholesaler' );
  13.     $asia   = get_role( 'asia_distributor' );
  14.     $retail->add_cap( 'retail_pricing' ); // Actually called Distributor on back end
  15.     $whole->add_cap( 'wholesale_pricing' );
  16.     $asia->add_cap( 'asia_pricing' );
  17. }
  18.  
  19.  
  20.  
  21. //Manual Price Adjustment
  22. add_filter('foxyshop_setup_product_info', 'jtd_members_price', 10, 2);
  23. function jtd_members_price($product, $product_id) {
  24.  
  25.     if ( !function_exists('wp_get_current_user') || !is_user_logged_in() ) return $product;
  26.  
  27.     $current_user    = wp_get_current_user();
  28.     $current_user_id = $current_user->ID;
  29.     $new_price       = '';
  30.  
  31.     if ( current_user_can( 'retail_pricing' ) ) { // Retailer account
  32.  
  33.         // get Retailer pricing
  34.         $new_price  = get_post_meta( $product_id, '_jtd_retail_price', true );
  35.  
  36.     } elseif ( current_user_can( 'wholesale_pricing' ) ) { // Wholesaler Account
  37.  
  38.         // get Wholesale pricing
  39.         $new_price  = get_post_meta( $product_id, '_jtd_wholesale_price', true );
  40.  
  41.     } elseif ( current_user_can( 'asia_pricing' ) ) { // Asia Account
  42.  
  43.         // get Wholesale pricing
  44.         $new_price  = get_post_meta( $product_id, '_jtd_asia_price', true );
  45.  
  46.     } else {
  47.  
  48.         return $product;
  49.  
  50.     }
  51.  
  52.     if ( !empty( $new_price ) ) {
  53.  
  54.         $newprice         = $new_price;
  55.         $product['price'] = $newprice;
  56.  
  57.     }
  58.  
  59.     return $product;
  60.  
  61. }
  62.  
  63.  
  64.  
  65. //Show New Field in Admin
  66. add_action( 'foxyshop_admin_product_details', 'jtd_custom_pricing_levels_meta' );
  67. function jtd_custom_pricing_levels_meta($post_id) {
  68.     $retail_price    = get_post_meta( $post_id, '_jtd_retail_price',    true );
  69.     $asia_price      = get_post_meta( $post_id, '_jtd_asia_price',      true );
  70.     $wholesale_price = get_post_meta( $post_id, '_jtd_wholesale_price', true );
  71.  
  72.     ?>
  73.         <h4>Distributor/Wholesale Pricing</h4>
  74.         <div class="foxyshop_field_control">
  75.             <label for="_jtd_retail_price">Distributor</label>
  76.             <input type="text" name="_jtd_retail_price" id="_jtd_retail_price" value="<?php echo $retail_price ?>" />
  77.         </div>
  78.         <div class="foxyshop_field_control">
  79.             <label for="_jtd_asia_price">Asia Distrib</label>
  80.             <input type="text" name="_jtd_asia_price" id="_jtd_asia_price" value="<?php echo $asia_price ?>" />
  81.         </div>
  82.         <div class="foxyshop_field_control">
  83.             <label for="_jtd_wholesale_price">Wholesaler</label>
  84.             <input type="text" name="_jtd_wholesale_price" id="_jtd_wholesale_price" value="<?php echo $wholesale_price ?>" />
  85.         </div>
  86.     <?php
  87. }
  88.  
  89.  
  90. //Save New Field
  91. add_action("foxyshop_save_product","my_custom_pricing_levels_meta_save");
  92. function my_custom_pricing_levels_meta_save($post_id) {
  93.     global $post_id;
  94.  
  95.     if ( isset( $_POST['_jtd_retail_price'] ) ) {
  96.         foxyshop_save_meta_data( '_jtd_retail_price', strip_tags( $_POST['_jtd_retail_price'] ) );
  97.     }
  98.  
  99.     if ( isset( $_POST['_jtd_asia_price'] ) ) {
  100.         foxyshop_save_meta_data( '_jtd_asia_price', strip_tags( $_POST['_jtd_asia_price'] ) );
  101.     }
  102.  
  103.     if ( isset( $_POST['_jtd_wholesale_price'] ) ) {
  104.         foxyshop_save_meta_data( '_jtd_wholesale_price', strip_tags( $_POST['_jtd_wholesale_price'] ) );
  105.     }
  106.  
  107.     return $post_id;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement