Enable Wholesale prices in Woocommerce 3 - php

In wooCommerce, I have added a custom meta field with a custom price (wholesale price) in edit product pages settings. Everything works well.
When I set a wholesale price it works fine everywhere. But if I try to change this wholesale price it doesn't work. the old price remains everywhere.
What I am doing wrong? How can I solve this problem to allow wholesale price changes?
The code I am using:
function w4dev_get_wholesale_price( $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_action( 'woocommerce_product_options_pricing', 'w4dev_woocommerce_product_options_pricing' );
function w4dev_woocommerce_product_options_pricing()
{
woocommerce_wp_text_input( array(
'id' => '_wholesale_price',
'class' => 'wc_input_wholesale_price short',
'label' => __( 'Wholesale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')',
'type' => 'text'
));
}
add_action( 'woocommerce_process_product_meta_simple', 'w4dev_woocommerce_process_product_meta_simple', 10, 1 );
function w4dev_woocommerce_process_product_meta_simple( $product_id )
{
if( isset($_POST['_wholesale_price']) && $_POST['_wholesale_price'] > 0 ){
update_post_meta( $product_id, '_wholesale_price', $_POST['_wholesale_price'] );
}
}
add_filter( 'woocommerce_get_price', 'w4dev_woocommerce_get_price', 10, 2);
function w4dev_woocommerce_get_price( $price, $product )
{
if( w4dev_get_wholesale_price($product) > 0 ) {
$price = w4dev_get_wholesale_price($product);
return $price;
}
}

There are some errors in your code and a lot of missing parts too:
The hook woocommerce_get_price is deprecated and outdated
To handle Product variations you need some more code (same thing for the variable products price range).
Other errors, like when saving your Wholesale price…
Your revisited code:
// Add "Wholesale Price" custom field to Products option pricing
add_action( 'woocommerce_product_options_pricing', 'w4dev_add_product_options_pricing' );
function w4dev_add_product_options_pricing()
{
woocommerce_wp_text_input( array(
'id' => '_wholesale_price',
'class' => 'wc_input_wholesale_price short',
'label' => __( 'Wholesale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')',
'type' => 'text'
));
}
// Add custom field to VARIATIONS option pricing
add_action( 'woocommerce_variation_options_pricing', 'w4dev_add_variation_options_pricing', 20, 3 );
function w4dev_add_variation_options_pricing( $loop, $variation_data, $post_variation )
{
$value = get_post_meta( $post_variation->ID, '_wholesale_price', true );
$symbol = ' (' . get_woocommerce_currency_symbol() . ')';
$key = 'wholesale_price[' . $loop . ']';
echo '<div class="variable_wholesale-price"><p class="form-row form-row-first">
<label>' . __( "Wholesale Price", "woocommerce" ) . $symbol . '</label>
<input type="text" size="5" name="' . $key .'" value="' . esc_attr( $value ) . '" />
</p></div>';
}
// Save "Wholesale Price" custom field to Products
add_action( 'woocommerce_process_product_meta_simple', 'w4dev_save_product_wholesale_price', 20, 1 );
function w4dev_save_product_wholesale_price( $product_id ) {
if( isset($_POST['_wholesale_price']) )
update_post_meta( $product_id, '_wholesale_price', $_POST['_wholesale_price'] );
}
// Save "Wholesale Price" custom field to VARIATIONS
add_action( 'woocommerce_save_product_variation', 'w4dev_save_product_variation_wholesale_price', 20, 2 );
function w4dev_save_product_variation_wholesale_price( $variation_id, $i ){
if ( isset( $_POST['wholesale_price'][$i] ) ) {
update_post_meta( $variation_id, '_wholesale_price', floatval( $_POST['wholesale_price'][$i] ) );
}
}
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'w4dev_custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'w4dev_custom_price', 90, 2 );
// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'w4dev_custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'w4dev_custom_price', 90, 2 );;
function w4dev_custom_price( $price, $product ) {
if( get_post_meta( $product->get_id(), '_wholesale_price', true ) > 0 )
$price = get_post_meta( $product->get_id(), '_wholesale_price', true );
return $price;
}
// Variable product price ramge
add_filter('woocommerce_variation_prices_price', 'w4dev_custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'w4dev_custom_variation_price', 90, 3 );
function w4dev_custom_variation_price( $price, $variation, $product ) {
if( get_post_meta( $variation->get_id(), '_wholesale_price', true ) > 0 )
$price = get_post_meta( $variation->get_id(), '_wholesale_price', true );
return $price;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Now you will be able to change the value of your 'Wholesale Price' and to handle it in product variations.
Product variations Wholesale price setting:
To handle sale prices regarding Sale prices in Woocommerce, there is those related hooks:
woocommerce_product_get_sale_price
woocommerce_product_variation_get_sale_price
woocommerce_variation_prices_get_sale_price

Related

WooCommerce: Get product variation custom field value in a hooked function

This is how, I add an admin custom field for product Variations:
add_action( 'woocommerce_variation_options_pricing', 'add_custom_field_to_variations', 10, 3 );
function add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'custom_field[' . $loop . ']',
'class' => 'short',
'label' => __( 'Custom Field', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'custom_field', true )
) );
}
Save custom field on product variation save:
add_action( 'woocommerce_save_product_variation', 'save_custom_field_variations', 10, 2 );
function save_custom_field_variations( $variation_id, $i ) {
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
}
Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'add_custom_field_variation_data' );
function add_custom_field_variation_data( $variations ) {
$variations ['custom_field'] = '<div class="woocommerce_custom_field">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
return $variations;
}
The value is saving and displaying in the single product page but the below function is not returning the value
Trying to retrieve Value of the custom field in a function:
add_filter('woocommerce_variation_prices_price', 'get_custom_variable_price' )
function get_custom_variable_price() {
global $post;
$custom_value = get_post_meta( $variations[ 'variation_id' ], 'custom_field', true );
$custom_value = (float)$custom_value;
return $custom_value;
}
I need to get just the value (which is float number):
There are some mistakes and missing things in your las function (see below).
Now there are 2 ways to get the custom field value in that hooked function:
1). Using WC_Data get_meta() method (since WooCommerce 3):
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 100, 3 );
function custom_variable_price( $price, $variation, $product ) {
$value = floatval( $variation->get_meta( 'custom_field' ) );
if( $value > 0 ) {
$price = $value;
}
return $price;
}
2). Or using WordPress get_post_meta() function (the old way):
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 100, 3 );
function custom_variable_price( $price, $variation, $product ) {
$value = floatval( floatval( get_post_meta( $variation->get_id(), 'custom_field', true ) );
if( $value > 0 ) {
$price = $value;
}
return $price;
}
Code goes in functions.php file of the active child theme (or active theme). Tested, works for both.
Important note: You may not see the result in front end, as variable product price range is cached in WooCommerce to improve performances (see the linked thread below for better understanding).
Related thread: Change product prices via a hook in WooCommerce 3+
Addition related to your others functions
Since WooCommerce 3 you could update your 2nd and 3rd function as follows:
add_action( 'woocommerce_admin_process_variation_object', 'save_custom_field_variation_value', 10, 2 );
function save_custom_field_variation_value( $variation, $i ) {
if( isset($_POST['custom_field'][$i]) ) {
$variation->update_meta_data( 'custom_field', floatval( sanitize_text_field($_POST['custom_field'][$i]) ) );
}
}
and 3rd function
add_filter( 'woocommerce_available_variation', 'add_variation_custom_field_value_to_variation_data', 10, 3 );
function add_variation_custom_field_value_to_variation_data( $variation_data, $product, $variation ) {
if ( $value = $variation->get_meta( 'custom_field' ) ) {
$variation_data['custom_field'] = '<div class="woocommerce_custom_field">' .__("Custom Field") . ': <span>' . $value . '</span></div>';
}
return $variation_data;
}

Add custom field value after selected WooCommerce product variation price

We have added a select field to simple products, so that we can define the price unit and display the unit after the price on the single product page. This works fine.
Additionally we have added a select field for variable products to define the price unit of each variation. This price unit should be displayed after the variation price on the product page:
<?php
function mytheme_woo_add_custom_variation_fields( $loop, $variation_data, $variation ) {
echo '<div class="options_group form-row form-row-full">';
$value = get_post_meta( $post->ID, '_variable_select_field', true );
if( empty( $value ) ) $value = '';
// Select
woocommerce_wp_select(
array(
'id' => '_variable_select_field[' . $variation->ID . ']',
'label' => __( 'Mengeneinheit', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_variable_select_field', true ),
'options' => array(
'' => __( 'ohne', 'woocommerce' ),
'Stück' => __( 'Stück', 'woocommerce' ),
'Paar' => __( 'Paar', 'woocommerce' ),
'Karton' => __( 'Karton', 'woocommerce' ),
'Packung' => __( 'Packung', 'woocommerce' ),
'Set' => __( 'Set', 'woocommerce' ),
'lfm' => __( 'Laufmeter', 'woocommerce' ),
'm²' => __( 'm2', 'woocommerce' ),
)
)
);
echo '</div>';
}
// Variations tab
add_action( 'woocommerce_variation_options_pricing', 'mytheme_woo_add_custom_variation_fields', 10, 3 );
// Add custom field for variations
function load_variation_settings_fields( $variations ) {
$variations['_variable_select_field'] = get_post_meta( $variations[ 'variation_id' ], '_variable_select_field', true );
return $variations;
}
add_filter( 'woocommerce_available_variation', 'load_variation_settings_fields' );
//Save variable product field
function mytheme_woo_add_custom_variation_fields_save( $post_id ){
$woocommerce_select_field = $_POST['_variable_select_field'][ $post_id ];
update_post_meta( $post_id, '_variable_select_field', esc_attr( $woocommerce_select_field ) );
}
add_action( 'woocommerce_save_product_variation', 'mytheme_woo_add_custom_variation_fields_save', 10, 2 );
/* Display the custom field after the price */
function mytheme_display_woo_custom_fields( $price ){
global $post, $product;
if ( $product->is_type( 'variable' ) ){
$variation_price_unit = get_post_meta( $post->ID, '_variable_select_field', true );
$price .= $variation_price_unit;
}
return $price;
}
add_action( 'woocommerce_get_price_html', 'mytheme_display_woo_custom_fields', 15 );
However, it does not work , I cannot find the right way to do that.
Can anyone let me know how to display the saved value from the product variation after the price?
Thank you!
This simple code snippet will display after selected variation price the related price unit as follows:
add_filter( 'woocommerce_available_variation', 'display_price_unit_after_variations_price_html', 10, 3) ;
function display_price_unit_after_variations_price_html( $data, $product, $variation ) {
if ( $mengeneinheit = $variation->get_meta('_variable_select_field') )
$data['price_html'] .= ' ' . $mengeneinheit;
return $data;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
You should rename the key _variable_select_field everywhere with for example _mengeneinheit as it will be more explicit (or in english _price_unit).

WooCommerce: Get custom field from product variations and display it as a suffix to variation prices

I'm trying to get a value from a custom numbers field on product variations, and show it as a suffix to variation prices along with custom text.
I'm working from
WooCommerce: Get custom field from product variations and display it on the “additional information area”
Adding custom text to the variation price in Woocommerce
This is what I have:
// 1. Add custom field input # Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'Add_bulk_price_to_variations', 10, 3 );
function Add_bulk_price_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'bulk_price[' . $loop . ']',
'desc_tip' => 'true',
'description' => __( 'Enter the Bulk price here.', 'woocommerce' ),
'label' => __( 'Custom Field', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'bulk_price', true )
));
}
// 2. Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'Save_bulk_price_variations', 10, 2 );
function Save_bulk_price_variations( $variation_id, $i ) {
$bulk_price = $_POST['bulk_price'][$i];
if ( isset( $bulk_price ) ) {
update_post_meta( $variation_id, 'bulk_price', esc_attr( $bulk_price ) );
}
}
// 3. Store custom field value into variation data
add_filter( 'woocommerce_available_variation', 'Add_bulk_price_variation_data' );
function Add_bulk_price_variation_data( $variations ) {
$variations['bulk_price'] = '<div class="woocommerce_bulk_price">Custom Field: <span>' . get_post_meta( $variations[ 'variation_id' ], 'bulk_price', true ) . '</span></div>';
return $variations;
}
// 4. Show the bulk price on product variations
function variation_price_custom_suffix( $variation_data, $product, $variation ) {
// Get childIDs in an array
$children_ids = $variations->get_children();
foreach ( $children_ids as $child_id ) {
$value = get_post_meta( $child_id, 'bulk_price', true );
// True
if ( $value ) {
$variation_data['price_html'] .= ' <span class="price-suffix">' . $value . __("custom text", "woocommerce") . '</span>';
return $variation_data;
}
add_filter('woocommerce_available_variation', 'variation_price_custom_suffix', 10, 3 );
You use the same hook 2x, this should suffice (where step 4 is no longer needed)
The use of a foreach loop to get the child ID's is not necessary
// 3 & 4. Store custom field value into variation data + show the bulk price on product variations
function add_bulk_price_variation_data( $variation_data, $product, $variation ) {
$bulk_price = get_post_meta( $variation_data[ 'variation_id' ], 'bulk_price', true);
if ( $bulk_price ) {
$variation_data['price_html'] .= ' <span class="price-suffix">' . __( $bulk_price , "woocommerce") . '</span>';
}
return $variation_data;
}
add_filter( 'woocommerce_available_variation', 'add_bulk_price_variation_data', 10, 3 );

How to custom role for wholesale price woocommerce

i have tried this code below and perfect working on variations product, but i want wholesale price only show on specific role user are logged in. if not login the price not showing.
Any help will be greatly appreciated.
// Add "Wholesale Price" custom field to Products option pricing
add_action( 'woocommerce_product_options_pricing', 'w4dev_add_product_options_pricing' );
function w4dev_add_product_options_pricing()
{
woocommerce_wp_text_input( array(
'id' => '_wholesale_price',
'class' => 'wc_input_wholesale_price short',
'label' => __( 'Wholesale Price', 'woocommerce' ) . ' ('.get_woocommerce_currency_symbol().')',
'type' => 'text'
));
}
// Add custom field to VARIATIONS option pricing
add_action( 'woocommerce_variation_options_pricing', 'w4dev_add_variation_options_pricing', 20, 3 );
function w4dev_add_variation_options_pricing( $loop, $variation_data, $post_variation )
{
$value = get_post_meta( $post_variation->ID, '_wholesale_price', true );
$symbol = ' (' . get_woocommerce_currency_symbol() . ')';
$key = 'wholesale_price[' . $loop . ']';
echo '<div class="variable_wholesale-price"><p class="form-row form-row-first">
<label>' . __( "Wholesale Price", "woocommerce" ) . $symbol . '</label>
<input type="text" size="5" name="' . $key .'" value="' . esc_attr( $value ) . '" />
</p></div>';
}
i have tried the code on Enable Wholesale prices in Woocommerce 3
// Save "Wholesale Price" custom field to Products
add_action( 'woocommerce_process_product_meta_simple', 'w4dev_save_product_wholesale_price', 20, 1 );
function w4dev_save_product_wholesale_price( $product_id ) {
if( isset($_POST['_wholesale_price']) )
update_post_meta( $product_id, '_wholesale_price', $_POST['_wholesale_price'] );
}
// Save "Wholesale Price" custom field to VARIATIONS
add_action( 'woocommerce_save_product_variation', 'w4dev_save_product_variation_wholesale_price', 20, 2 );
function w4dev_save_product_variation_wholesale_price( $variation_id, $i ){
if ( isset( $_POST['wholesale_price'][$i] ) ) {
update_post_meta( $variation_id, '_wholesale_price', floatval( $_POST['wholesale_price'][$i] ) );
}
}
// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'w4dev_custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'w4dev_custom_price', 90, 2 );
// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'w4dev_custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'w4dev_custom_price', 90, 2 );;
function w4dev_custom_price( $price, $product ) {
if( get_post_meta( $product->get_id(), '_wholesale_price', true ) > 0 )
$price = get_post_meta( $product->get_id(), '_wholesale_price', true );
return $price;
}
// Variable product price ramge
add_filter('woocommerce_variation_prices_price', 'w4dev_custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'w4dev_custom_variation_price', 90, 3 )
function w4dev_custom_variation_price( $price, $variation, $product ) {
if( get_post_meta( $variation->get_id(), '_wholesale_price', true ) > 0 )
$price = get_post_meta( $variation->get_id(), '_wholesale_price', true );
return $price;
}

Displaying custom field of product variations within Woocommerce orders and emails

I successfully added a custom field for product variations within the backend of WooCommerce and was able to display its value. I'd like to contain this value within order and emails as well.
//Display Fields in admin on product edit screen
add_action( 'woocommerce_product_after_variable_attributes', 'woo_variable_fields', 10, 3 );
//Save variation fields values
add_action( 'woocommerce_save_product_variation', 'save_variation_fields', 10, 2 );
// Create new fields for variations
function woo_variable_fields( $loop, $variation_data, $variation ) {
echo '<div class="variation-custom-fields">';
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field['. $loop .']',
'label' => __( 'additional fees (e.g. monthly fee)', 'woocommerce' ),
'placeholder' => '',
//'desc_tip' => true,
'wrapper_class' => 'form-row form-row-first',
//'description' => __( 'Enter the custom value here.', 'woocommerce' ),
'value' => get_post_meta($variation->ID, '_text_field', true)
)
);
echo "</div>";
}
/** Save new fields for variations */
function save_variation_fields( $variation_id, $i) {
// Text Field
$text_field = stripslashes( $_POST['_text_field'][$i] );
update_post_meta( $variation_id, '_text_field', esc_attr( $text_field ) );
}
// Custom Product Variation
add_filter( 'woocommerce_available_variation', 'custom_load_variation_settings_products_fields' );
function custom_load_variation_settings_products_fields( $variations ) {
$variations['variation_custom_field'] = get_post_meta( $variations[ 'variation_id' ], '_text_field', true );
return $variations;
}
I'm using the following to display the custom field's value within the cart.
add_filter( 'woocommerce_get_item_data', 'display_custom_field_as_item_data', 20, 2 );
function display_custom_field_as_item_data( $cart_data, $cart_item ) {
if( $value = get_post_meta( $cart_item['data']->get_id(), '_text_field', true ) ){
$cart_data[] = array(
'name' => __( 'Additional Monthly Fee', 'woocommerce' ),
'value' => sanitize_text_field( $value )
);
}
return $cart_data;
}
Further as for now I'm displaying its value below the product price / total of each item within cart/checkout template with the following
echo get_post_meta( $_product->get_id(), '_text_field', true );
How about displaying the value of this field within orders and emails?
Updated: To display that in order pages and email notifications try the following additional functions (I have made some changes in the woocommerce_get_item_data hooked function, so you need to replace it with the following one):
// Save custom field value in cart item
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_field_in_cart_object', 30, 3 );
function save_custom_field_in_cart_object( $cart_item_data, $product_id, $variation_id ) {
// Get the correct Id to be used
$the_id = $variation_id > 0 ? $variation_id : $product_id;
if( $value = get_post_meta( $the_id, '_text_field', true ) )
$cart_item_data['custom_data'] = sanitize_text_field( $value );
return $cart_item_data;
}
// Display on cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_field_as_item_data', 20, 2 );
function display_custom_field_as_item_data( $cart_data, $cart_item ) {
if( isset( $cart_item['custom_data'] ) ){
$cart_data[] = array(
'name' => __( 'Additional Monthly Fee', 'woocommerce' ),
'value' => $cart_item['custom_data']
);
}
return $cart_data;
}
// Save custom field value in order items meta data
add_action( 'woocommerce_add_order_item_meta', 'add_custom_field_to_order_item_meta', 20, 3 );
function add_custom_field_to_order_item_meta( $item_id, $values, $cart_item_key ) {
if( isset( $values['custom_data'] ) )
wc_add_order_item_meta( $item_id, __( 'Additional Monthly Fee', 'woocommerce' ), $values['custom_data'] );
}
Code goes in function.php file of your active child theme (or theme). Tested and works.
It will display the custom field label and value in all Order pages and in email notifications too.

Categories