Options to define default attribute selections in variations. Closes #46.

This commit is contained in:
Mike Jolley 2011-11-01 18:19:01 +00:00
parent 482d7e9291
commit 8785c312b8
7 changed files with 75 additions and 11 deletions

View File

@ -30,6 +30,7 @@ function variable_product_type_options() {
?>
<div id="variable_product_options" class="panel">
<p class="bulk_edit"><strong><?php _e('Bulk edit:', 'woothemes'); ?></strong> <a class="button set_all_prices" href="#"><?php _e('Set all prices', 'woothemes'); ?></a> <a class="button set_all_sale_prices" href="#"><?php _e('Set all sale prices', 'woothemes'); ?></a> <a class="button set_all_stock" href="#"><?php _e('Set all stock', 'woothemes'); ?></a></p>
<div class="woocommerce_variations">
<?php
$args = array(
@ -109,6 +110,46 @@ function variable_product_type_options() {
</div>
<?php $loop++; endforeach; ?>
</div>
<?php
if ($variation_attribute_found) :
?>
<p class="default_variation">
<strong><?php _e('Default variation selections:', 'woothemes'); ?></strong>
<?php
$default_attributes = (array) maybe_unserialize(get_post_meta( $post->ID, '_default_attributes', true ));
foreach ($attributes as $attribute) :
// Only deal with attributes that are variations
if ( !$attribute['is_variation'] ) continue;
// Get current value for variation (if set)
$variation_selected_value = (isset($default_attributes[sanitize_title($attribute['name'])])) ? $default_attributes[sanitize_title($attribute['name'])] : '';
// Name will be something like attribute_pa_color
echo '<select name="default_attribute_' . sanitize_title($attribute['name']).'"><option value="">'.__('No default', 'woothemes') . ' ' . $woocommerce->attribute_label($attribute['name']).'&hellip;</option>';
// Get terms for attribute taxonomy or value if its a custom attribute
if ($attribute['is_taxonomy']) :
$post_terms = wp_get_post_terms( $post->ID, $attribute['name'] );
foreach ($post_terms as $term) :
echo '<option '.selected($variation_selected_value, $term->slug, false).' value="'.$term->slug.'">'.$term->name.'</option>';
endforeach;
else :
$options = explode('|', $attribute['value']);
foreach ($options as $option) :
echo '<option '.selected($variation_selected_value, $option, false).' value="'.$option.'">'.ucfirst($option).'</option>';
endforeach;
endif;
echo '</select>';
endforeach;
?>
</p>
<?php
endif;
?>
<button type="button" class="button button-primary add_variation" <?php disabled($variation_attribute_found, false); ?>><?php _e('Add Variation', 'woothemes'); ?></button>
<button type="button" class="button link_all_variations" <?php disabled($variation_attribute_found, false); ?>><?php _e('Link all variations', 'woothemes'); ?></button>
@ -675,9 +716,24 @@ function process_product_meta_variable( $post_id ) {
endforeach;
endif;
update_post_meta( $post_parent, 'price', $lowest_price );
update_post_meta( $post_parent, 'min_variation_price', $lowest_price );
update_post_meta( $post_parent, 'max_variation_price', $highest_price );
// Update default attribute options setting
$default_attributes = array();
foreach ($attributes as $attribute) :
if ( $attribute['is_variation'] ) :
$value = esc_attr(trim($_POST[ 'default_attribute_' . sanitize_title($attribute['name']) ]));
if ($value) :
$default_attributes[sanitize_title($attribute['name'])] = $value;
endif;
endif;
endforeach;
update_post_meta( $post_parent, '_default_attributes', $default_attributes );
}
add_action('woocommerce_process_product_meta_variable', 'process_product_meta_variable');

View File

@ -161,11 +161,11 @@ div.multi_select_products_wrapper-alt{float:right;}
#grouped_product_options,#virtual_product_options,#simple_product_options{padding:12px;font-style:italic;color:#666;}
#variable_product_options p.description{float:left;padding:0;margin:0;}
#variable_product_options p.bulk_edit{background:#ececec;padding:6px;}#variable_product_options p.bulk_edit strong{margin:0 6px 0 0;}
.woocommerce_variation{background:#ececec;border:1px solid #ececec;margin:0 0 8px;}.woocommerce_variation p{margin:0 !important;}.woocommerce_variation p button{float:right;}
.woocommerce_variation table td{background:#fff;padding:6px 6px;vertical-align:middle;}.woocommerce_variation table td label{color:#999;font-size:10px;text-transform:uppercase;text-align:left;display:block;line-height:16px;}
.woocommerce_variation table td input{float:left;width:100%;}
.woocommerce_variation table td.upload_image{width:1%;white-space:nowrap;}.woocommerce_variation table td.upload_image img{float:none;margin-right:6px;vertical-align:middle;}
.woocommerce_variation table td.upload_image .button{margin:0;padding:4px 10px;width:auto;float:none;vertical-align:middle;}
.woocommerce_variation,.default_variation{background:#ececec;border:1px solid #ececec;margin:0 0 8px;}.woocommerce_variation p,.default_variation p{margin:0 !important;}.woocommerce_variation p button,.default_variation p button{float:right;}
.woocommerce_variation table td,.default_variation table td{background:#fff;padding:6px 6px;vertical-align:middle;}.woocommerce_variation table td label,.default_variation table td label{color:#999;font-size:10px;text-transform:uppercase;text-align:left;display:block;line-height:16px;}
.woocommerce_variation table td input,.default_variation table td input{float:left;width:100%;}
.woocommerce_variation table td.upload_image,.default_variation table td.upload_image{width:1%;white-space:nowrap;}.woocommerce_variation table td.upload_image img,.default_variation table td.upload_image img{float:none;margin-right:6px;vertical-align:middle;}
.woocommerce_variation table td.upload_image .button,.default_variation table td.upload_image .button{margin:0;padding:4px 10px;width:auto;float:none;vertical-align:middle;}
.widefat .product-cat-placeholder{outline:1px dotted #A0C443;height:60px;background:#000;}
.tips{cursor:help;text-decoration:none;}
img.tips{padding:5px 0 0 0;}

View File

@ -860,7 +860,7 @@ div.multi_select_products_wrapper-alt {
}
}
}
.woocommerce_variation {
.woocommerce_variation, .default_variation {
background: #ececec;
border: 1px solid #ececec;
margin: 0 0 8px;

View File

@ -394,6 +394,9 @@ jQuery(document).ready(function($) {
if (variation) {
$('form input[name=variation_id]').val(variation.variation_id);
show_variation(variation);
} else {
// Nothing found - reset fields
$('.variations select').val('');
}
} else {
update_variation_values(matching_variations);
@ -407,13 +410,15 @@ jQuery(document).ready(function($) {
$('.single_variation').text('');
check_variations();
$(this).blur();
if($().uniform) $.uniform.update();
if($.isFunction($.uniform.update)) {
$.uniform.update();
}
}).focus(function(){
check_variations( $(this).attr('name') );
});
}).change();
if (woocommerce_params.is_cart==1) {

File diff suppressed because one or more lines are too long

View File

@ -95,6 +95,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
* Best sellers widget based on new total_sales field
* Ability to exclude product ids
* Option for the recipient of order/stock emails
* Options to define default attribute selections in variations
* Edit category - image fix
* Order Complete email heading fix
* 100% discount when price excludes tax logic fix

View File

@ -352,6 +352,7 @@ if (!function_exists('woocommerce_variable_add_to_cart')) {
global $post, $_product, $woocommerce;
$attributes = $_product->get_available_attribute_variations();
$default_attributes = (array) maybe_unserialize(get_post_meta( $post->ID, '_default_attributes', true ));
// Put available variations into an array and put in a Javascript variable (JSON encoded)
$available_variations = array();
@ -402,6 +403,7 @@ if (!function_exists('woocommerce_variable_add_to_cart')) {
<option value=""><?php echo __('Choose an option', 'woothemes') ?>&hellip;</option>
<?php if(is_array($options)) : ?>
<?php
$selected_value = (isset($default_attributes[sanitize_title($name)])) ? $default_attributes[sanitize_title($name)] : '';
// Get terms if this is a taxonomy - ordered
if (taxonomy_exists(sanitize_title($name))) :
$args = array('menu_order' => 'ASC');
@ -409,11 +411,11 @@ if (!function_exists('woocommerce_variable_add_to_cart')) {
foreach ($terms as $term) :
if (!in_array($term->slug, $options)) continue;
echo '<option value="'.$term->slug.'">'.$term->name.'</option>';
echo '<option value="'.$term->slug.'" '.selected($selected_value, $term->slug).'>'.$term->name.'</option>';
endforeach;
else :
foreach ($options as $option) :
echo '<option value="'.$option.'">'.$option.'</option>';
echo '<option value="'.$option.'" '.selected($selected_value, $option).'>'.$option.'</option>';
endforeach;
endif;
?>