xah icon

multiplier function v0.0.8

xah | PRO | 11/23/21 06:52:43 AM UTC (Edited) | 0 ⭐ | 333 👁️ | Never ⏰ | []
PHP |

5.35 KB

|

None

|

0 👍

/

0 👎

// Cost price field
add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' );
function wc_cost_product_field() {
    woocommerce_wp_text_input( array( 'id' => 'cost_price', 'class' => 'wc_input_price short', 'label' => __( 'Cost Price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
}
 
add_action( 'save_post', 'wc_cost_save_product' );
function wc_cost_save_product( $product_id ) {
 
     // stop the quick edit interferring as this will stop it saving properly, when a user uses quick edit feature
     if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
        return;
 
    // If this is a auto save do nothing, we only save when update button is clicked
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['cost_price'] ) ) {
        if ( is_numeric( $_POST['cost_price'] ) )
            update_post_meta( $product_id, 'cost_price', $_POST['cost_price'] );
    } else{
update_post_meta( $product_id, '', 'cost_price' );}
}
 
// Utility function to change the prices with a multiplier (number)
function get_price_multiplier() {
    return 0.917; // Modify as wish
}
 
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
 
// --Simple product price change
function custom_price( $price, $product ) {
 
$user = wp_get_current_user();
global $post;
$cost_price = get_post_meta( $post->ID, 'cost_price', true );
 
if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
    if ( $cost_price != '' && $cost_price != 0){$profit = $price / 2 - $cost_price / 2; $price = $price - $profit;}
 
    else{$price = $price * get_price_multiplier();}}else{
    $price = $price;}
 
    return (float) $price;
}
 
// --Variable product price change
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 );
function custom_variable_price( $price, $variation, $product ) {
    wc_delete_product_transients($variation->get_id());
 
$user = wp_get_current_user();
 
if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
    $price = $price * get_price_multiplier();}else{
    $price = $price;}
 
    return (float) $price;
}
 
//Hide Price Range for WooCommerce Variable Products
add_filter( 'woocommerce_variable_sale_price_html', 
'variable_product_price', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 
'variable_product_price', 10, 2 );
 
function variable_product_price( $v_price, $v_product ) {
 
// Product Price
$prod_prices = array( $v_product->get_variation_price( 'min', true ), 
                            $v_product->get_variation_price( 'max', true ) );
$prod_price = $prod_prices[0]!==$prod_prices[1] ? sprintf(__('%1$s', 'woocommerce'), 
                       wc_price( $prod_prices[0] ) ) : wc_price( $prod_prices[0] );
 
// Regular Price
$user = wp_get_current_user();
 
if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
$regular_prices = array( $v_product->get_variation_regular_price( 'min', true ), 
                          $v_product->get_variation_regular_price( 'max', true ) );
$og_price = $regular_prices[0] / get_price_multiplier();
sort( $regular_prices );
 
$regular_price = $regular_prices[0]!==$regular_prices[1] ? sprintf(__('%1$s','woocommerce')
                      , wc_price( $og_price ) ) : wc_price( $og_price );
}else{
$regular_prices = array( $v_product->get_variation_regular_price( 'min', true ), 
                          $v_product->get_variation_regular_price( 'max', true ) );
sort( $regular_prices );
 
$regular_price = $regular_prices[0]!==$regular_prices[1] ? sprintf(__('%1$s','woocommerce')
                      , wc_price( $regular_prices[0] ) ) : wc_price( $regular_prices[0] );
}
 
if ( $prod_price !== $regular_price ) {
$prod_price = '<del>'.$regular_price.$v_product->get_price_suffix() . '</del> <ins>' . 
                       $prod_price . $v_product->get_price_suffix() . '</ins>';
}
return $prod_price;
}
 
// Simple Products
add_filter( 'woocommerce_get_price_html', 'filter_woocommerce_get_price_html', 10, 2 ); 
 
function filter_woocommerce_get_price_html( $price, $instance ) { 
    global $product;
 
$user = wp_get_current_user();
global $post;
$cost_price = get_post_meta( $post->ID, 'cost_price', true );
 
if ( in_array( 'administrator', (array) $user->roles ) || in_array( 'student', (array) $user->roles )) {
if($product->is_type('simple')){
if($cost_price != '' && $cost_price != 0){
$regular_price=$product->get_regular_price('min');
$profit=$regular_price / 2 - $cost_price / 2;
$sale_price=$regular_price - $profit;
$sale_price=round($sale_price);
}else{
$regular_price=$product->get_regular_price('min');
$sale_price=$regular_price * get_price_multiplier();
$sale_price=round($sale_price);}
 
if ( $sale_price !== $regular_price ) {
$price = '<del>$'.$regular_price .'&nbsp;</del><ins>$' . 
                       $sale_price . '</ins>';
}}}
 
    return $price; 
}

Comments