How to custom role for wholesale price woocommerce - php

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;
}

Related

Calculate and display deposit in cart total en check-out total

I have a WooCommerce webshop with different kind of beers from all over the world. If you return a empty bottle you get a deposit of 0,10 or something like that. I made an ACF (Advanced Custom Field) where I can add the deposit price for an empty bottle. I managed to add this to my cart page and display it under the single price of a bottle (0,10) and under the subtotal if there are more bottles (0,40).
This is working in cart and checkout but I want to make a sum off the total deposit money if you return the bottles to the store.
How can I create a function that calculate the sum of all bottles in the cart that has a deposit?
I managed to get the first this working with this functions:
add_filter( 'woocommerce_cart_item_price', 'add_deposit_value_to_cart_single' , 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'add_deposit_value_to_cart_total', 10, 3 );
/**
* Statiegeld toevoegen onder single prijs product winkelwagen
*/
function add_deposit_value_to_cart_single( $product_price, $values ) {
$statiegeld = get_field( "statiegeld", $values['product_id']);
$dep_total = $statiegeld;
if( ! empty( $dep_total ) )
return $product_price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld %s' ), wc_price( $dep_total ) ) . '</small>';
return $product_price;
}
/**
* Statiegeld toevoegen aan subtotaal in winkelwagen
*/
function add_deposit_value_to_cart_total( $product_price, $values ) {
$statiegeld = get_field( "statiegeld", $values['product_id']);
$dep_total = $statiegeld * $values['quantity'];
if( ! empty( $dep_total ) )
return $product_price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld totaal %s' ), wc_price( $dep_total ) ) . '</small>';
return $product_price;
}
If someone has an idea I am very grateful because I can't figure it out.
There are some errors with the arguments of both functions.
Try this:
/**
* Statiegeld toevoegen onder single prijs product winkelwagen
*/
add_filter( 'woocommerce_cart_item_price', 'add_deposit_value_to_cart_single' , 10, 3 );
function add_deposit_value_to_cart_single( $price, $cart_item, $cart_item_key ) {
$statiegeld = get_field( "statiegeld", $cart_item['product_id']);
$dep_total = $statiegeld;
if( ! empty( $dep_total ) )
return $price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld %s' ), wc_price( $dep_total ) ) . '</small>';
return $price;
}
/**
* Statiegeld toevoegen aan subtotaal in winkelwagen
*/
add_filter( 'woocommerce_cart_item_subtotal', 'add_deposit_value_to_cart_total', 10, 3 );
function add_deposit_value_to_cart_total( $price, $cart_item, $cart_item_key ) {
$statiegeld = get_field( "statiegeld", $cart_item['product_id']);
$dep_total = $statiegeld * $cart_item['quantity'];
if ( ! empty( $dep_total ) ) {
return $price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld totaal %s' ), wc_price( $dep_total ) ) . '</small>';
}
return $price;
}
I have tested the code and it works fine. Here is the result:
I changed my whole code to this and this is working. This wil show a deposit in the cart summary under the price and a total deposit if it are more items under the subtotal.
Also in the cart total summary it is displaying the depost total of all items and in checkout too.
add_filter( 'woocommerce_cart_item_price', 'add_deposit_value_to_cart_single' , 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'add_deposit_value_to_cart_total', 10, 3 );
/**
* Statiegeld toevoegen aan single product
*/
function add_deposit_value_to_cart_single( $product_price, $values, $cart_item_key ) {
// Get your custom fields data
$custom_field1 = get_post_meta( $values['product_id'], '_aantal_statiegeld', true );
//$statiegeld = get_field( "statiegeld", $values['product_id']);
$dep_total = $custom_field1;
if( ! empty( $dep_total ) )
return $product_price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld %s' ), wc_price( $dep_total ) ) . '</small>';
return $product_price;
}
/**
* Statiegeld toevoegen aan totaal
*/
function add_deposit_value_to_cart_total( $product_price, $values, $cart_item_key ) {
// Get your custom fields data
$custom_field1 = get_post_meta( $values['product_id'], '_aantal_statiegeld', true );
//$custom_field1 = get_field( "statiegeld", $values['product_id']);
$dep_total = $custom_field1 * $values['quantity'];
if( ! empty( $dep_total ) )
return $product_price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld totaal %s' ), wc_price( $dep_total ) ) . '</small>';
return $product_price;
}
// Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );
function woocom_general_product_data_custom_field() {
// Create a custom text field
// Number Field
woocommerce_wp_text_input(
array(
'id' => '_aantal_statiegeld',
'label' => __( 'Statiegeld', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Vul het statiegeld hier in', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0,10'
)
)
);
}
// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field' );
/** Hook callback function to save custom fields information */
function woocom_save_general_proddata_custom_field( $post_id ) {
// Save Number Field
$number_field = $_POST['_aantal_statiegeld'];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_aantal_statiegeld', esc_attr( $number_field ) );
}
}
add_action( 'woocommerce_product_additional_information', 'custom_data_in_product_add_info_tab', 20, 1 );
function custom_data_in_product_add_info_tab( $product ) {
//Product ID - WooCommerce compatibility
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Get your custom fields data
$custom_field1 = get_post_meta( $product_id, '_aantal_statiegeld', true );
// Set your custom fields labels (or names)
$label1 = __( 'Statiegeld', 'woocommerce');
// The Output
echo '<h3>'. __('Statiegeld totaal', 'woocommerce') .'</h3>
<table class="custom-fields-data">
<tbody>
<tr class="custom-field1">
<th>'. $label1 .'</th>
<td>'. $custom_field1 .'</td>
</tr>
</tbody>
</table>';
}
add_action( 'woocommerce_cart_totals_before_shipping', 'display_cart_volume_total', 20 );
add_action( 'woocommerce_review_order_before_shipping', 'display_cart_volume_total', 20 );
function display_cart_volume_total() {
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'], '_aantal_statiegeld', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
if( $total_volume > 0 ){
// The Output
echo ' <tr class="cart-total-volume">
<th>' . __( "Statiegeld totaal", "woocommerce" ) . '</th>
<td data-title="total-statiegeld">€' . number_format($total_volume, 2, ',', '.') . '</td>
</tr>';
}
}

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;
}

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 );

Save and display product selected custom data everywhere in WooCommerce

I have a code that shows the checkbox on the product edit page. When I click on this checkbox, a select box is displayed on the single product page.
Here is my code:
// Display Checkbox Field
add_action('woocommerce_product_options_general_product_data', 'roast_custom_field_add');
function roast_custom_field_add(){
global $post;
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_roast_checkbox',
'label' => __('Roast Level', 'woocommerce' ),
'description' => __( 'Enable roast level!', 'woocommerce' )
)
);
}
// Save Checkbox Field
add_action('woocommerce_process_product_meta', 'roast_custom_field_save');
function roast_custom_field_save($post_id){
// Custom Product Checkbox Field
$roast_checkbox = isset( $_POST['_roast_checkbox'] ) ? 'yes' : 'no';
update_post_meta($post_id, '_roast_checkbox', esc_attr( $roast_checkbox ));
}
// Display Select Box
add_action( 'woocommerce_before_add_to_cart_button', 'add_roast_custom_field', 0 );
function add_roast_custom_field() {
global $post;
// If is single product page and have the "roast_checkbox" enabled we display the field
if ( is_product() && get_post_meta( $post->ID, '_roast_checkbox', true ) == 'yes' ) {
echo '<div>';
woocommerce_form_field( 'roast_custom_options', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Roast Level'),
'required' => false,
'options' => array(
'' => 'Please select',
'Blue' => 'Blue',
'Rare' => 'Rare',
'Medium Rare' => 'Medium Rare',
'Medium' => 'Medium',
'Medium Well' => 'Medium Well',
'Well Done' => 'Well Done'
)
), '' );
echo '</div>';
}
}
When you click on the checkbox, the selection field is shown correctly.
But the data, after selecting the options are not saved.
Also, these data are not displayed on the cart page, on the checkout page, in the order, etc.
Нere is my code:
// Save roast custom field
add_action( 'woocommerce_add_to_cart', 'roast_custom_field_add_to_cart', 20, 6 );
function roast_custom_field_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
if( isset($_POST['roast_custom_options']) ){
$roast_values = (array) get_post_meta( $product_id, '_roast_custom_options_values', true );
$roast_values[] = sanitize_text_field( $_POST['roast_custom_options'] );
update_post_meta( $product_id, '_roast_custom_options_values', $roast_values );
}
}
// Add custom fields values under cart item name in cart
add_filter( 'woocommerce_cart_item_name', 'roast_custom_field', 10, 3 );
function roast_custom_field( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
if( $roast_values = $cart_item['data']->get_meta('_roast_custom_options_values') ) {
$item_name .= '<br /><div class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $roast_values . ' </div>';
}
return $item_name;
}
// Display roast custom fields values under item name in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'roast_custom_checkout_cart_item_name', 10, 3 );
function roast_custom_checkout_cart_item_name( $item_qty, $cart_item, $cart_item_key ) {
if( $roast_values = $cart_item['data']->get_meta('_roast_custom_options_values') ) {
$item_qty .= '<br /><div class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $roast_values . ' </div>';
}
return $item_qty;
}
// Display roast custom fields values on orders and email notifications
add_filter( 'woocommerce_order_item_name', 'roast_custom_order_item_name', 10, 2 );
function roast_custom_order_item_name( $item_name, $item ) {
$product = $item->get_product();
if( $roast_values = $product->get_meta('_roast_custom_options_values') ) {
$item_name .= '<br /><span class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $roast_values . ' </span>';
}
return $item_name;
}
How to fix the code so that everything works correctly?
I have revisited your code "in a hurry", also added some missing function and removed another one:
// Display Checkbox Field
add_action('woocommerce_product_options_general_product_data', 'roast_custom_field_add');
function roast_custom_field_add(){
global $post;
// Checkbox
woocommerce_wp_checkbox(
array(
'id' => '_roast_checkbox',
'label' => __('Roast Level', 'woocommerce' ),
'description' => __( 'Enable roast level!', 'woocommerce' )
)
);
}
// Save Checkbox Field
add_action('woocommerce_process_product_meta', 'roast_custom_field_save');
function roast_custom_field_save($post_id){
// Custom Product Checkbox Field
$roast_checkbox = isset( $_POST['_roast_checkbox'] ) ? 'yes' : 'no';
update_post_meta($post_id, '_roast_checkbox', esc_attr( $roast_checkbox ));
}
// Display Select Box
add_action( 'woocommerce_before_add_to_cart_button', 'add_roast_custom_field', 0 );
function add_roast_custom_field() {
global $product;
// If is single product page and have the "roast_checkbox" enabled we display the field
if ( is_product() && $product->get_meta( '_roast_checkbox' ) === 'yes' ) {
echo '<div>';
woocommerce_form_field( 'roast_custom_options', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Roast Level'),
'required' => false,
'options' => array(
'' => 'Please select',
'Blue' => 'Blue',
'Rare' => 'Rare',
'Medium Rare' => 'Medium Rare',
'Medium' => 'Medium',
'Medium Well' => 'Medium Well',
'Well Done' => 'Well Done'
)
), '' );
echo '</div>';
}
}
// Add as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_cart_item_data', 10, 3 );
function add_custom_cart_item_data($cart_item_data, $product_id, $variation_id ){
if( isset( $_POST['roast_custom_options'] ) ) {
$cart_item_data['roast_option'] = wc_clean( $_POST['roast_custom_options'] );
}
return $cart_item_data;
}
// Add custom fields values under cart item name in cart
add_filter( 'woocommerce_cart_item_name', 'roast_custom_field', 10, 3 );
function roast_custom_field( $item_name, $cart_item, $cart_item_key ) {
if( ! is_cart() )
return $item_name;
if( isset($cart_item['roast_option']) ) {
$item_name .= '<br /><div class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $cart_item['roast_option'] . '</div>';
}
return $item_name;
}
// Display roast custom fields values under item name in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'roast_custom_checkout_cart_item_name', 10, 3 );
function roast_custom_checkout_cart_item_name( $item_qty, $cart_item, $cart_item_key ) {
if( isset($cart_item['roast_option']) ) {
$item_qty .= '<br /><div class="my-custom-class"><strong>' . __("Roast Level", "woocommerce") . ':</strong> ' . $cart_item['roast_option'] . 'гр.</div>';
}
return $item_qty;
}
// Save chosen slelect field value to each order item as custom meta data and display it everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product_fitting_color', 10, 4 );
function save_order_item_product_fitting_color( $item, $cart_item_key, $values, $order ) {
if( isset($values['roast_option']) ) {
$key = __('Roast Level', 'woocommerce');
$value = $values['roast_option'];
$item->update_meta_data( $key, $value );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
On frontend Cart page:
On backend Order edit pages:
On email notifications:
Your checkbox value is stored to database, try to change this code
$roast_checkbox = isset( $_POST['_roast_checkbox'] ) ? 'yes' : 'no';
with
if ( isset( $_POST['_roast_checkbox'] ) {
$roast_checkbox = $_POST['_roast_checkbox'];
//update_post_meta
}

Enable Wholesale prices in Woocommerce 3

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

Categories