Guest User

Untitled

a guest
Jan 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. add_action('woocommerce_product_options_general_product_data', 'bdev_add_product_purchase_price_field');
  2. add_action('woocommerce_process_product_meta', 'bdev_save_product_purchase_price_field', 10, 2);
  3. add_action('woocommerce_product_after_variable_attributes', 'bdev_add_variable_product_purchase_price_field', 10, 3);
  4. add_action('woocommerce_save_product_variation', 'bdev_save_variable_product_purchase_price_field', 10, 2);
  5.  
  6. function bdev_add_product_purchase_price_field() {
  7. $currency = get_woocommerce_currency_symbol();
  8. woocommerce_wp_text_input(
  9. array(
  10. 'id' => '_purchase_price',
  11. 'class' => '',
  12. 'wrapper_class' => 'pricing show_if_simple show_if_external',
  13. 'label' => __("Purchase Price", 'products-purchase-price-for-woocommerce') . " ($currency)",
  14. 'data_type' => 'price',
  15. 'desc_tip' => true,
  16. 'description' => __('Purchase cost, e.g: 79', 'products-purchase-price-for-woocommerce'),
  17. )
  18. );
  19. }
  20.  
  21. function bdev_save_product_purchase_price_field($post_id, $post) {
  22. if (isset($_POST['_purchase_price'])) {
  23. $purchase_price = ($_POST['_purchase_price'] === '' ) ? '' : wc_format_decimal($_POST['_purchase_price']);
  24. update_post_meta($post_id, '_purchase_price', $purchase_price);
  25. }
  26. }
  27.  
  28.  
  29. function bdev_add_variable_product_purchase_price_field($loop, $variation_data, $variation) {
  30. $currency = get_woocommerce_currency_symbol();
  31. woocommerce_wp_text_input(array(
  32. 'id' => 'variable_purchase_price[' . $loop . ']',
  33. 'wrapper_class' => 'form-row form-row-first',
  34. 'label' => __("Purchase Price", 'products-purchase-price-for-woocommerce') . " ($currency)",
  35. 'placeholder' => 'Purchase cost, e.g: 79',
  36. 'data_type' => 'price',
  37. 'desc_tip' => false,
  38. 'value' => get_post_meta($variation->ID, '_purchase_price', true)
  39. ));
  40. }
  41.  
  42. function bdev_save_variable_product_purchase_price_field($variation_id, $i) {
  43. if (isset($_POST['variable_purchase_price'][$i])) {
  44. $purchase_price = ($_POST['variable_purchase_price'][$i] === '' ) ? '' : wc_format_decimal($_POST['variable_purchase_price'][$i]);
  45. update_post_meta($variation_id, '_purchase_price', $purchase_price);
  46. }
  47. }
  48.  
  49.  
  50. add_filter( 'manage_edit-product_columns', 'bdev_add_purchase_price_product_column', 11);
  51. function bdev_add_purchase_price_product_column( $columns )
  52. {
  53. $columns['_purchase_price'] = __( 'Cost','woocommerce');
  54. return $columns;
  55. }
  56.  
  57. add_action( 'manage_product_posts_custom_column' , 'bdev_display_purchase_price_in_column', 10, 2 );
  58. function bdev_display_purchase_price_in_column( $column, $product_id )
  59. {
  60. global $post;
  61. $purchase_price = get_post_meta( $product_id, '_purchase_price', true );
  62. switch ( $column )
  63. {
  64. case '_purchase_price' :
  65. echo get_woocommerce_currency_symbol() . $purchase_price;
  66. break;
  67. }
  68. }
  69.  
  70. add_filter( 'manage_edit-product_columns', 'bdev_add_net_winnings_product_column', 11);
  71. function bdev_add_net_winnings_product_column($columns)
  72. {
  73. $columns['net_winnings'] = __( 'Profit','woocommerce');
  74. return $columns;
  75. }
  76.  
  77. add_action( 'manage_product_posts_custom_column' , 'wppp_net_winnings_display', 10, 2 );
  78. function wppp_net_winnings_display( $column, $product_id ){
  79. if( $column == 'net_winnings' ) {
  80. global $product;
  81.  
  82. if( ! $product && ! is_object($product) ){
  83. $product = wc_get_product( $product_id );
  84. }
  85.  
  86. $purchase_price = (float) $product->get_meta('_purchase_price' );
  87. $regular_price = (float) $product->get_regular_price();
  88. $net_winning = $purchase_price != 0 ? $regular_price - $purchase_price : 0;
  89. echo wc_price($net_winning);
  90. }
  91. }
  92.  
  93. add_filter( "manage_edit-product_sortable_columns", 'bdev_make_purchase_price_column_sortable' );
  94. function bdev_make_purchase_price_column_sortable( $columns )
  95. {
  96. $custom = array(
  97. '_purchase_price' => 'Cost',
  98. 'net_winnings' => 'Profit',
  99. );
  100. return wp_parse_args( $custom, $columns );
  101. }
Add Comment
Please, Sign In to add comment