Reputation: 410
I am adding and saving a custom field to my variable products using the following functions:
add_action( 'woocommerce_product_options_inventory_product_data', 'wc_add_custom_fields' );
function wc_add_custom_fields() {
woocommerce_wp_text_input(
array(
'id' => '_custom_product_pricekg_field',
'label' => __( 'Price per kg', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => false,
'description' => __( "Here's some really helpful text that appears next to the field.", 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_fields_save' );
function wc_custom_fields_save($post_id)
{
$woocommerce_custom_product_pricekg_field = $_POST['_custom_product_pricekg_field'];
if (!empty($woocommerce_custom_product_pricekg_field))
update_post_meta($post_id, '_custom_product_pricekg_field', esc_attr( $_POST['_custom_product_pricekg_field'] ) );
}
I would like to replace the variable price range ($XX-XX) on all archive and product pages with this custom field. I don't know how to do this. The below snippet replaces the variable price range with the default variable, perhaps this could be modified to show my custom field value?
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
function custom_variation_price( $price, $product ) {
foreach($product->get_available_variations() as $pav){
$def=true;
foreach($product->get_variation_default_attributes() as $defkey=>$defval){
if($pav['attributes']['attribute_'.$defkey]!=$defval){
$def=false;
}
}
if($def){
$price = $pav['display_price'];
}
}
return woocommerce_price($price);
}
Upvotes: 0
Views: 333
Reputation: 29614
The following code replaces the price range with the value from the custom field, if you have additional questions, be welcome.
// Add new field
function wc_add_custom_fields() {
woocommerce_wp_text_input(
array(
'id' => '_custom_product_pricekg_field',
'label' => __( 'Price per kg', 'woocommerce' ),
'placeholder' => '',
'desc_tip' => false,
'description' => __( "Here's some really helpful text that appears next to the field.", 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
}
add_action( 'woocommerce_product_options_inventory_product_data', 'wc_add_custom_fields', 10, 0 );
// Save
function wc_custom_fields_save($post_id) {
$woocommerce_custom_product_pricekg_field = $_POST['_custom_product_pricekg_field'];
if ( !empty( $woocommerce_custom_product_pricekg_field ) ) {
update_post_meta($post_id, '_custom_product_pricekg_field', esc_attr( $_POST['_custom_product_pricekg_field'] ) );
}
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_fields_save', 10, 1 );
// Variation price
function custom_variation_price( $price, $product ) {
// Get product id
$product_id = $product->get_id();
// Get custom value
$new_price = get_post_meta( $product_id, '_custom_product_pricekg_field', true);
// NOT empty
if ( ! empty( $new_price ) ) {
$price = $new_price;
}
return $price;
}
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);
Upvotes: 1