Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 5 replies, has 2 voices.

Last updated by Dražen Duvnjak 8 months, 3 weeks ago.

Assisted by: Dražen Duvnjak.

Author Posts
March 15, 2024 at 9:28 am #15413518

nicolasK-21

I recently had a support chat about having the option to set custom prices for variable products with a bulk action like woocommerce has and i wanted to share my solution:

You can add code via plugin or in the functions.php file of a child theme to add the functionality in 2 steps.
First step is to add an action for the extra options to the "Bulk actions" dropdown (in my example with Euros). There are two options, one for the regular price and one for the sale price:

add_action( 'woocommerce_variable_product_bulk_edit_actions', 'set_euro_price_bulk_edit_variation', 10 );

function set_euro_price_bulk_edit_variation() {
?>
<optgroup label="Euro Price">
<option value="set_regular_euro_price">Set regular price</option>
<option value="set_sale_euro_price">Set sale price</option>
</optgroup>
<script>
jQuery( function(){
jQuery( 'select.variation_actions' ).on( 'set_regular_euro_price_ajax_data', function(e, data) {
var value = window.prompt( "enter regular price" );

if ( value !== null ) {
data.value = value;
return data;
} else {
return;
}
});
jQuery( 'select.variation_actions' ).on( 'set_sale_euro_price_ajax_data', function(e, data) {
var value = window.prompt( "enter sale price" );

if ( value !== null ) {
data.value = value;
return data;
} else {
return;
}
});
});
</script>
<?php
}

This includes a script to collect the price values via JS prompt.

The second step is to add an action to process the data afterwards which is just editing post meta:

add_action( 'woocommerce_bulk_edit_variations', 'set_euro_price_bulk_save', 10, 4 );

function set_euro_price_bulk_save( $bulk_action, $data, $product_id, $variations ) {
if( in_array( $bulk_action, [ 'set_regular_euro_price' ] ) ) {
if( isset( $data['value'] ) ) {
foreach( $variations as $variation_id ) {
// Update post meta will not update if variation_id exists, thus no check is necessary
// Else use if ( $variation = wc_get_product( $variation_id ) )
update_post_meta( $variation_id, '_regular_price_EUR', esc_attr( $data['value'] ) );
update_post_meta( $variation_id, '_price_EUR', esc_attr( $data['value'] ) );
update_post_meta( $variation_id, '_wcml_custom_prices_status', '1' );
}

}
}
if( in_array( $bulk_action, [ 'set_sale_euro_price' ] ) ) {
if( isset( $data['value'] ) ) {
foreach( $variations as $variation_id ) {
// Update post meta will not update if variation_id exists, thus no check is necessary
// Else use if ( $variation = wc_get_product( $variation_id ) )
update_post_meta( $variation_id, '_sale_price_EUR', esc_attr( $data['value'] ) );
update_post_meta( $variation_id, '_wcml_custom_prices_status', '1' );
}

}
}
}

March 15, 2024 at 10:22 am #15413809

Dražen Duvnjak
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

thanks for getting back, I will add this to the related feature request escalated ticket, and any other client in the meantime can try out our suggestion.

I was trying to check but there are some errors when you try to add code, I suggest checking this and maybe sharing the correct code, you can use the code button to add it to your text reply.

Regards,
Drazen

March 15, 2024 at 12:28 pm #15414257

nicolasK-21

Hi,
I do not have a code button on my end (or any other formatting options), is there another way to do this?

March 15, 2024 at 1:40 pm #15414651

Dražen Duvnjak
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

you can wrap it in code tags or just share Pastebin link and I will edit your reply and add it inside so other users can use it if needed.

- hidden link

Regards,
Drazen

March 15, 2024 at 2:06 pm #15414750

nicolasK-21

All right, here's the code i use:

<?php
/* Add the Dropdown options to the product editor: */
add_action( 'woocommerce_variable_product_bulk_edit_actions', 'set_euro_price_bulk_edit_variation', 10 );

function set_euro_price_bulk_edit_variation() {
?>
<optgroup label="Euro price">
	<option value="set_regular_euro_price">set regular price</option>
	<option value="set_sale_euro_price">set sale price</option>
</optgroup>
<script>
	jQuery( function(){
		jQuery( 'select.variation_actions' ).on( 'set_regular_euro_price_ajax_data', function(e, data) {
			var value = window.prompt( "set regular price" );

            if ( value !== null ) {
                data.value = value;
                return data;
            } else {
                return;
            }
		});
		jQuery( 'select.variation_actions' ).on( 'set_sale_euro_price_ajax_data', function(e, data) {
			var value = window.prompt( "set sale price" );

            if ( value !== null ) {
                data.value = value;
                return data;
            } else {
                return;
            }
		});
	});
</script>
<?php
}

/* Processing the data added through the bulk action */
add_action( 'woocommerce_bulk_edit_variations', 'set_euro_price_bulk_save', 10, 4 );

function set_euro_price_bulk_save( $bulk_action, $data, $product_id, $variations ) {
	if( in_array( $bulk_action, [ 'set_regular_euro_price' ] ) ) {
		if( isset( $data['value'] ) ) {
			foreach( $variations as $variation_id ) {
				// Update post meta will not update if variation_id exists, thus no check is necessary
				// Else use if ( $variation = wc_get_product( $variation_id ) )
				update_post_meta( $variation_id, '_regular_price_EUR', esc_attr( $data['value'] ) );
				update_post_meta( $variation_id, '_price_EUR', esc_attr( $data['value'] ) );
				update_post_meta( $variation_id, '_wcml_custom_prices_status', '1' );
			}

		}
	}
	if( in_array( $bulk_action, [ 'set_sale_euro_price' ] ) ) {
		if( isset( $data['value'] ) ) {
			foreach( $variations as $variation_id ) {
				// Update post meta will not update if variation_id exists, thus no check is necessary
				// Else use if ( $variation = wc_get_product( $variation_id ) )
				update_post_meta( $variation_id, '_sale_price_EUR', esc_attr( $data['value'] ) );
				update_post_meta( $variation_id, '_wcml_custom_prices_status', '1' );
			}

		}
	}
}
?>
March 15, 2024 at 2:25 pm #15414790

Dražen Duvnjak
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

great, thanks for sharing.

Your feature request is escalated, and we will consider it for future version development. I can not guarantee that this will be accepted and implemented and when.

Thanks for reporting and suggesting this.

Regards,
Drazen