Last active
April 20, 2016 20:06
-
-
Save jtcote/5670fc44ddbd9193ff9d18649ab71fdc to your computer and use it in GitHub Desktop.
WooCommerce Simple Wholesale Price
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//** Wholesale pricing based on 'wholesaler' role | |
// checks if the current user is capable to have the wholesale pricing | |
function gum_woo_wholesale_pricing_applies(){ | |
return (bool) ( current_user_can('wholesaler') && ( !is_admin() || is_ajax() ) ); | |
} | |
// wholesale price when available for both Simple & Variable product type | |
function gum_woo_wholesale_price_get( $product ) { | |
if( $product->is_type( array('simple', 'variable') ) && get_post_meta( $product->id, '_wholesale_price', true ) > 0 ){ | |
return get_post_meta( $product->id, '_wholesale_price', true ); | |
} elseif( $product->is_type('variation') && get_post_meta( $product->variation_id, '_wholesale_price', true ) > 0 ){ | |
return get_post_meta( $product->variation_id, '_wholesale_price', true ); | |
} | |
return 0; | |
} | |
//* Add Wholesale Pricing input to the Simple product editing page | |
add_action( 'woocommerce_product_options_pricing', 'gum_woo_wholesale_pricing_field' ); | |
function gum_woo_wholesale_pricing_field() { | |
woocommerce_wp_text_input( array( | |
'id' => '_wholesale_price', | |
'class' => 'wc_input_wholesale_price short', | |
'label' => __( 'Wholesale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')', | |
'type' => 'text' | |
)); | |
} | |
//save the wholesale price value | |
add_action( 'woocommerce_process_product_meta_simple', 'gum_woo_wholesale_pricing_save', 10, 1 ); | |
function gum_woo_wholesale_pricing_save( $product_id ) { | |
if( isset($_POST['_wholesale_price']) && $_POST['_wholesale_price'] >= 0 ) | |
update_post_meta( $product_id, '_wholesale_price', $_POST['_wholesale_price'] ); | |
} | |
//assign the wholesale price for wholesaler we will need to hook into woocommerce_get_price filter | |
add_filter( 'woocommerce_get_price', 'gum_woo_wholesale_pricing_return', 10, 2); | |
function gum_woo_wholesale_pricing_return( $price, $product ) { | |
if( gum_woo_wholesale_pricing_applies() && gum_woo_wholesale_price_get($product) > 0 ) | |
$price = gum_woo_wholesale_price_get($product); | |
return $price; | |
} | |
//add Wholesale Pricing input to the Variable product editing page | |
add_action( 'woocommerce_product_after_variable_attributes', 'gum_woo_wholesale_pricing_field_var', 10, 3 ); | |
function gum_woo_wholesale_pricing_field_var( $loop, $variation_data, $variation ){ | |
?> | |
<tr class="wholesale_price_row"> | |
<td> | |
<div> | |
<label><?php _e( 'Wholesale Price:', 'woocommerce' ); ?></label> | |
<input type="text" size="5" name="variable_wholesale_price[<?php echo $loop; ?>]" value="<?php if ( isset( $variation_data['_wholesale_price'][0] ) ) echo esc_attr( $variation_data['_wholesale_price'][0] ); ?>" step="1" min="0" /> | |
</div> | |
</td> | |
</tr> | |
<?php | |
} | |
//save the wholesale price value | |
add_action( 'woocommerce_save_product_variation', 'gum_woo_wholesale_pricing_field_var_save', 10, 2 ); | |
function gum_woo_wholesale_pricing_field_var_save($variation_id, $i) { | |
if( isset($_POST['variable_wholesale_price'][$i]) ) | |
update_post_meta( $variation_id, '_wholesale_price', $_POST['variable_wholesale_price'][$i]); | |
} | |
add_filter( 'woocommerce_variation_price_html', 'gum_woo_wholesale_pricing_field_var_return', 10, 2); | |
add_filter( 'woocommerce_variation_sale_price_html', 'gum_woo_wholesale_pricing_field_var_return', 10, 2); | |
function gum_woo_wholesale_pricing_field_var_return( $price, $product ) { | |
if( isset($product->product_custom_fields['_wholesale_price'][0]) ) | |
$wholesale_price = $product->product_custom_fields['_wholesale_price'][0]; | |
else | |
$wholesale_price = ''; | |
if( gum_woo_wholesale_pricing_applies() && $wholesale_price > 0 ) | |
return woocommerce_price( $wholesale_price ); | |
return $price; | |
} | |
//add wholesale pricing notice | |
//add_filter( 'woocommerce_before_main_content', 'gum_woo_wholesale_pricing_notice', 10, 2); | |
function gum_woo_wholesale_pricing_notice() { | |
if( gum_woo_sale_pricing_applies() ) { | |
$notice = '<span class="notice-wholesale"> Wholesale Pricing </span>'; | |
} else { | |
$notice = ''; | |
} | |
echo $notice; | |
} | |
add_filter( 'woocommerce_get_price_html', 'gum_woo_wholesale_pricing_label', 10, 2); | |
function gum_woo_wholesale_pricing_label( $price, $product ) { | |
if( gum_woo_wholesale_pricing_applies() && gum_woo_wholesale_price_get($product) > 0 && is_singular() ) { | |
$text = '<span class="label-wholesale"> Wholesale </span>'; | |
} else { | |
$text = ''; | |
} | |
return $price .' '. $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment