Надеюсь дальше разберетесь
Кусок из рабочего кода. Удалил несущественные фрагменты.
<?php
/* Add a custom field to a product variation */
//Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 2 );
//JS to add fields for new variations
add_action( 'woocommerce_product_after_variable_attributes_js', 'variable_fields_js' );
//Save variation fields
add_action( 'woocommerce_process_product_meta_variable', 'variable_fields_process', 10, 1 );
add_action( 'woocommerce_process_product_meta', 'variable_fields_override_price', 5, 2 );
function variable_fields( $loop, $variation_data ) {
?>
<tr>
<td>
<div>
<label>Variable description</label>
<textarea type="text" size="5" name="variable_custom_description[<?php echo $loop; ?>]" ><?php echo $variation_data['_variable_custom_description'][0]; ?></textarea>
</div>
</td>
<td>
<div>
<label>Percent discount</label>
<input type="text" size="5" name="variable_percent_discount[<?php echo $loop; ?>]" value="<?php echo $variation_data['_variable_percent_discount'][0]; ?>" />
</div>
</td>
</tr>
<?php
}
function variable_fields_js() {
?>
<tr>
<td>
<div>
<label>Variable description</label>
<textarea type="text" size="5" name="variable_custom_description[' + loop + ']" ></textarea>
</div>
</td>
<td>
<div>
<label>Percent discount</label>
<input type="text" size="5" name="variable_percent_discount[' + loop + ']" />
</div>
</td>
</tr>
<?php
}
/*
Обновляю запрос $_POST перед записью variable
*/
function variable_fields_override_price( $post_id, $__post ) {
if (isset( $_POST['variable_sku'] ) ) {
$variable_percent_discount = $_POST['variable_percent_discount'];
if(! $variable_percent_discount )
return;
$variable_post_ids = join(',', $_POST['variable_post_id']);
$variable_sku = $_POST['variable_sku'];
global $wpdb;
foreach( $_POST['variable_post_id'] as $key => $variable ) {
if ( !isset( $variable_sku[$key] ))
continue;
$_sku = explode(',', $variable_sku[$key] );
if( count( $_sku ) == 0 )
continue;
if( count($_sku) == 1 ) {
/* */
}
$query = "SELECT wpdb_prefix_postmeta.post_id , wpdb_prefix_posts.menu_order
FROM wpdb_prefix_postmeta , wpdb_prefix_posts
INNER JOIN wpdb_prefix_postmeta as p1 ON p1.meta_value IN(" . $variable_sku[$key] . ")
WHERE wpdb_prefix_postmeta.meta_key LIKE '_sku_%'
AND wpdb_prefix_postmeta.post_id=p1.post_id
AND wpdb_prefix_posts.ID=p1.post_id
AND NOT wpdb_prefix_postmeta.post_id IN( $variable_post_ids )
GROUP BY wpdb_prefix_postmeta.post_id
ORDER BY wpdb_prefix_posts.menu_order ";
$posts = $wpdb->get_results($query);
if( !$posts )
continue;
$variable_price = 0;
$current_price = 0;
$description = '';
foreach( $posts as $post ) {
$product = wc_get_product($post->post_id);
if(! $product)
continue;
$_regular_price = get_post_meta($post->post_id, '_regular_price', true );
$price = $product->get_price(); // со скидкой, если она есть
// $price = $_regular_price;
$current_price += $price;
$variable_price += $_regular_price;
$link = get_permalink($post->post_id); //$post->post_name;
$title = get_the_title($post->post_id);
$output = '<li>';
$output .= '<a href="' . $link . '" title="'. $title .'">'. $title .'</a><span style="float:right;"> ';
$output .= wc_price($_regular_price);
$output .= '</span></li>';
$description .= $output;
}
if( $variable_price && $current_price) {
$_POST['variable_regular_price'][$key] = $variable_price;
$_POST['variable_sale_price'][$key] = $current_price * ((100-$variable_percent_discount[$key])/100);
}
if( $description ) {
$description = "Products in bundle:<ul>$description</ul>";
$description .= "<hr style='margin:5px;'>";
$description .= "Normal price: " . wc_price($variable_price);
$description .= '<span class="price"><ins style="float:right;font-size:small;">Current price: ' . wc_price($_POST['variable_sale_price'][$key]) . '</ins></span>';
$_POST['variable_custom_description'][$key] = $description;
}
}
}
}
/*
Save variation fields
*/
function variable_fields_process( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
$variable_custom_field = $_POST['variable_custom_description'];
$variable_discount_perc = $_POST['variable_percent_discount'];
$max_loop = max( array_keys( $_POST['variable_post_id'] ) );
for ( $i = 0; $i <= $max_loop; $i++ ) :
if ( ! isset( $variable_post_id[ $i ] ) )
continue;
$variation_id = absint( $variable_post_id[ $i ] );
if ( !isset( $variable_discount_perc[$i] ) )
$variable_discount_perc[$i] = '';
if ( !isset( $variable_custom_field[$i] ) )
$variable_custom_field[$i] = '';
update_post_meta( $variation_id, '_variable_custom_description', stripslashes( $variable_custom_field[$i] ) );
update_post_meta( $variation_id, '_variable_percent_discount', stripslashes( $variable_discount_perc[$i] ) );
endfor;
endif;
}
/* add the custom variation meta data to the fronted */
add_filter( 'woocommerce_available_variation', 'uniquename_available_variation', 100, 3 );
function uniquename_available_variation($variations, $variation_object, $variation) {
$variations['custom_field_value'] = get_post_meta($variations["variation_id"], '_variable_custom_description', true);
return $variations;
}
/* Front end */
add_action('woocommerce_single_product_summary', 'add_variation_data', 60);
function add_variation_data() {
echo '<div class="selected-variation-custom-field" style="padding-top: 25px;"></div>';
}
add_action('woocommerce_after_add_to_cart_form', 'add_variation_data_script', 60);
function add_variation_data_script() {
global $product;
if( $product && $product->product_type == "variable") {
?>
<script>
jQuery(function($) {
variations_data = JSON.parse( $('form.variations_form').first().attr( 'data-product_variations' ) ),
$selected_variation_custom_field = $('.selected-variation-custom-field'); // see DIV above
$('table.variations').on('change', 'select', function() {
var $select = $(this),
attribute_name = $select.attr('name'),
selected_value = $select.val(),
custom_field_value = "";
// Loop over the variations_data until we find a matching attribute value
$.each(variations_data, function() {
if( this.attributes[ attribute_name ] && this.attributes[ attribute_name ] === selected_value ) {
custom_field_value = variations_data[ $select[0].selectedIndex-1].custom_field_value;
return false; // break
}
});
// doing this outside the loop above ensures that the DIV gets emptied out when it should
// $selected_variation_custom_field.text( custom_field_value );
$selected_variation_custom_field.html( custom_field_value );
});
});
</script>
<?php
}
}