我正在使用woocommerce我只是使用钩子编写一个函数,因为我需要使用公式计算每个产品的价格,所以我只是复制原始函数修改它并将其添加到我的function.php中,我看到了它在我的本地机器上工作正常但不在我的服务器中。在服务器中,它不保存变体。
在这里,我添加了我在finctions.php中添加的代码
remove_action('woocommerce_product_write_panels', 'variable_product_type_options');
add_action( 'woocommerce_product_write_panels','variable_product_type_options_with_goldprice_calculation' );
function variable_product_type_options_with_goldprice_calculation(){
global $post, $woocommerce;
$attributes = maybe_unserialize( get_post_meta( $post->ID, '_product_attributes', true ) );
// See if any are set
$variation_attribute_found = false;
if ( $attributes ) foreach( $attributes as $attribute ) {
if ( isset( $attribute['is_variation'] ) ) {
$variation_attribute_found = true;
break;
}
}
// Get tax classes
$tax_classes = array_filter( array_map('trim', explode( "\n", get_option( 'woocommerce_tax_classes' ) ) ) );
$tax_class_options = array();
$tax_class_options['parent'] = __( 'Same as parent', 'woocommerce' );
$tax_class_options[''] = __( 'Standard', 'woocommerce' );
if ( $tax_classes )
foreach ( $tax_classes as $class )
$tax_class_options[ sanitize_title( $class ) ] = esc_attr( $class );
?>
<div id="variable_product_options" class="panel wc-metaboxes-wrapper"><div id="variable_product_options_inner">
<?php if ( ! $variation_attribute_found ) : ?>
<div id="message" class="inline woocommerce-message">
<div class="squeezer">
<h4><?php _e( 'Before adding variations, add and save some attributes on the <strong>Attributes</strong> tab.', 'woocommerce' ); ?></h4>
<p class="submit"><a class="button-primary" href="http://docs.woothemes.com/document/product-variations/" target="_blank"><?php _e( 'Learn more', 'woocommerce' ); ?></a></p>
</div>
</div>
<?php else : ?>
<p class="toolbar">
<a href="#" class="close_all"><?php _e( 'Close all', 'woocommerce' ); ?></a><a href="#" class="expand_all"><?php _e( 'Expand all', 'woocommerce' ); ?></a>
<strong><?php _e( 'Bulk edit:', 'woocommerce' ); ?></strong>
<select id="field_to_edit">
<option value="toggle_enabled"><?php _e( 'Toggle "Enabled"', 'woocommerce' ); ?></option>
<option value="toggle_downloadable"><?php _e( 'Toggle "Downloadable"', 'woocommerce' ); ?></option>
<option value="toggle_virtual"><?php _e( 'Toggle "Virtual"', 'woocommerce' ); ?></option>
<option value="delete_all"><?php _e( 'Delete all variations', 'woocommerce' ); ?></option>
<option value="variable_regular_price"><?php _e( 'Prices', 'woocommerce' ); ?></option>
<option value="variable_sale_price"><?php _e( 'Sale prices', 'woocommerce' ); ?></option>
<option value="variable_stock"><?php _e( 'Stock', 'woocommerce' ); ?></option>
<option value="variable_weight"><?php _e( 'Weight', 'woocommerce' ); ?></option>
<option value="variable_length"><?php _e( 'Length', 'woocommerce' ); ?></option>
<option value="variable_width"><?php _e( 'Width', 'woocommerce' ); ?></option>
<option value="variable_height"><?php _e( 'Height', 'woocommerce' ); ?></option>
<option value="variable_file_paths" rel="textarea"><?php _e( 'File Path', 'woocommerce' ); ?></option>
<option value="variable_download_limit"><?php _e( 'Download limit', 'woocommerce' ); ?></option>
<option value="variable_download_expiry"><?php _e( 'Download Expiry', 'woocommerce' ); ?></option>
<?php do_action( 'woocommerce_variable_product_bulk_edit_actions' ); ?>
</select>
<a class="button bulk_edit"><?php _e( 'Go', 'woocommerce' ); ?></a>
</p>
<div class="woocommerce_variations wc-metaboxes">
<?php
// Get parent data
$parent_data = array(
'id' => $post->ID,
'attributes' => $attributes,
'tax_class_options' => $tax_class_options,
'sku' => get_post_meta( $post->ID, '_sku', true ),
'weight' => get_post_meta( $post->ID, '_weight', true ),
'length' => get_post_meta( $post->ID, '_length', true ),
'width' => get_post_meta( $post->ID, '_width', true ),
'height' => get_post_meta( $post->ID, '_height', true ),
'tax_class' => get_post_meta( $post->ID, '_tax_class', true )
);
if ( ! $parent_data['weight'] )
$parent_data['weight'] = '0.00';
if ( ! $parent_data['length'] )
$parent_data['length'] = '0';
if ( ! $parent_data['width'] )
$parent_data['width'] = '0';
if ( ! $parent_data['height'] )
$parent_data['height'] = '0';
// Get variations
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $post->ID
);
$variations = get_posts( $args );
$loop = 0;
if ( $variations ) foreach ( $variations as $variation ) {
$variation_id = absint( $variation->ID );
$variation_post_status = esc_attr( $variation->post_status );
$variation_data = get_post_meta( $variation_id );
$variation_data['variation_post_id'] = $variation_id;
// Grab shipping classes
$shipping_classes = get_the_terms( $variation_id, 'product_shipping_class' );
$shipping_class = ( $shipping_classes && ! is_wp_error( $shipping_classes ) ) ? current( $shipping_classes )->term_id : '';
$variation_fields = array(
'_sku',
'_stock',
'_price',
'_regular_price',
'_sale_price',
'_weight',
'_length',
'_width',
'_height',
'_tax_class',
'_download_limit',
'_download_expiry',
'_file_paths',
'_downloadable',
'_virtual',
'_thumbnail_id',
'_sale_price_dates_from',
'_sale_price_dates_to'
);
foreach ( $variation_fields as $field )
$$field = isset( $variation_data[ $field ][0] ) ? $variation_data[ $field ][0] : '';
// Price backwards compat
if ( $_regular_price == '' && $_price ){
$_regular_price = $_price;
}
/*External Added
* @Author : Sanjay
* To set the variable price from the formula
*/
else{
if(isset($variation_data['attribute_pa_materiaal'][0]) && ($variation_data['attribute_pa_materiaal'][0] != 'zilver-2' && $variation_data['attribute_pa_materiaal'][0] != 'zilver')){
$ringthickness = explode('-',$variation_data[ 'attribute_pa_dikte-van-de-ring' ][0]);
if($thickness[0] !=0){
$ringthickness = $thickness[0];
}
else{
$ringthickness = 1;
}
if($variation_data[ 'attribute_pa_breedte' ][0] != ''){
$ringwidth = $variation_data[ 'attribute_pa_breedte' ][0];
}
else{
$ringwidth = 1;
}
if($variation_data[ 'attribute_pa_ringmaat' ][0] != ''){
$ringsize = $variation_data[ 'attribute_pa_ringmaat' ][0];
}
elseif($variation_data[ 'attribute_ringmaat' ][0] != ''){
$ringsize = $variation_data[ 'attribute_ringmaat' ][0];
}
else{
$ringsize = 1;
}
if($variation_data[ 'attribute_pa_vast-prijsdeel' ][0] != ''){
$labour_price = $variation_data[ 'attribute_pa_vast-prijsdeel' ][0] ;
}
if($variation_data[ 'attribute_pa_prijsdeel-per-mm2' ][0] != ''){
$gold_price = str_replace('-','',$variation_data[ 'attribute_pa_prijsdeel-per-mm2' ][0] );
}
$_regular_price = ($ringsize*3.14*$ringwidth*$ringthickness)+$labour_price+$gold_price;
}
}
/*Here Ends*/
// Get image
$image = '';
$image_id = absint( $_thumbnail_id );
if ( $image_id )
$image = wp_get_attachment_url( $image_id );
// Format file paths
$_file_paths = maybe_unserialize( $_file_paths );
if ( is_array( $_file_paths ) )
$_file_paths = implode( "\n", $_file_paths );
include( ABSPATH.'wp-content/plugins/woocommerce/admin/post-types/writepanels/variation-admin-html.php' );
$loop++;
}
?>
</div>
<p class="toolbar">
<button type="button" class="button button-primary add_variation" <?php disabled( $variation_attribute_found, false ); ?>><?php _e( 'Add Variation', 'woocommerce' ); ?></button>
<button type="button" class="button link_all_variations" <?php disabled( $variation_attribute_found, false ); ?>><?php _e( 'Link all variations', 'woocommerce' ); ?></button>
<strong><?php _e( 'Default selections:', 'woocommerce' ); ?></strong>
<?php
$default_attributes = 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', 'woocommerce' ) . ' ' . esc_html( $woocommerce->attribute_label( $attribute['name'] ) ) . '…</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="' . esc_attr( $term->slug ) . '">' . apply_filters( 'woocommerce_variation_option_name', esc_html( $term->name ) ) . '</option>';
} else {
$options = array_map( 'trim', explode( '|', $attribute['value'] ) );
foreach ( $options as $option )
echo '<option ' . selected( sanitize_title( $variation_selected_value ), sanitize_title( $option ), false ) . ' value="' . esc_attr( sanitize_title( $option ) ) . '">' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
echo '</select>';
}
?>
</p>
<?php endif; ?>
</div></div>
<?php
/**
* Product Type Javascript
*/
ob_start();
?>
jQuery(function(){
var variation_sortable_options = {
items:'.woocommerce_variation',
cursor:'move',
axis:'y',
handle: 'h3',
scrollSensitivity:40,
forcePlaceholderSize: true,
helper: 'clone',
opacity: 0.65,
placeholder: 'wc-metabox-sortable-placeholder',
start:function(event,ui){
ui.item.css('background-color','#f6f6f6');
},
stop:function(event,ui){
ui.item.removeAttr('style');
variation_row_indexes();
}
};
// Add a variation
jQuery('#variable_product_options').on('click', 'button.add_variation', function(){
jQuery('.woocommerce_variations').block({ message: null, overlayCSS: { background: '#fff url(<?php echo $woocommerce->plugin_url(); ?>/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
var loop = jQuery('.woocommerce_variation').size();
var data = {
action: 'woocommerce_add_variation',
post_id: <?php echo $post->ID; ?>,
loop: loop,
security: '<?php echo wp_create_nonce("add-variation"); ?>'
};
jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function(response) {
jQuery('.woocommerce_variations').append( response );
jQuery(".tips").tipTip({
'attribute' : 'data-tip',
'fadeIn' : 50,
'fadeOut' : 50
});
jQuery('input.variable_is_downloadable, input.variable_is_virtual').change();
jQuery('.woocommerce_variations').unblock();
jQuery('#variable_product_options').trigger('woocommerce_variations_added');
});
return false;
});
jQuery('#variable_product_options').on('click', 'button.link_all_variations', function(){
var answer = confirm('<?php _e( 'Are you sure you want to link all variations? This will create a new variation for each and every possible combination of variation attributes (max 50 per run).', 'woocommerce' ); ?>');
if (answer) {
jQuery('#variable_product_options').block({ message: null, overlayCSS: { background: '#fff url(<?php echo $woocommerce->plugin_url(); ?>/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
var data = {
action: 'woocommerce_link_all_variations',
post_id: <?php echo $post->ID; ?>,
security: '<?php echo wp_create_nonce("link-variations"); ?>'
};
jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function(response) {
var count = parseInt( response );
if (count==1) {
alert( count + ' <?php _e( "variation added", 'woocommerce' ); ?>');
} else if (count==0 || count>1) {
alert( count + ' <?php _e( "variations added", 'woocommerce' ); ?>');
} else {
alert('<?php _e( "No variations added", 'woocommerce' ); ?>');
}
if (count>0) {
var this_page = window.location.toString();
this_page = this_page.replace( 'post-new.php?', 'post.php?post=<?php echo $post->ID; ?>&action=edit&' );
$('#variable_product_options').load( this_page + ' #variable_product_options_inner', function() {
$('#variable_product_options').unblock();
jQuery('#variable_product_options').trigger('woocommerce_variations_added');
} );
} else {
$('#variable_product_options').unblock();
}
});
}
return false;
});
jQuery('#variable_product_options').on('click', 'button.remove_variation', function(e){
e.preventDefault();
var answer = confirm('<?php _e( 'Are you sure you want to remove this variation?', 'woocommerce' ); ?>');
if (answer){
var el = jQuery(this).parent().parent();
var variation = jQuery(this).attr('rel');
if (variation>0) {
jQuery(el).block({ message: null, overlayCSS: { background: '#fff url(<?php echo $woocommerce->plugin_url(); ?>/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
var data = {
action: 'woocommerce_remove_variation',
variation_id: variation,
security: '<?php echo wp_create_nonce("delete-variation"); ?>'
};
jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function(response) {
// Success
jQuery(el).fadeOut('300', function(){
jQuery(el).remove();
});
});
} else {
jQuery(el).fadeOut('300', function(){
jQuery(el).remove();
});
}
}
return false;
});
jQuery('.wc-metaboxes-wrapper').on('click', 'a.bulk_edit', function(event){
var field_to_edit = jQuery('select#field_to_edit').val();
if ( field_to_edit == 'toggle_enabled' ) {
var checkbox = jQuery('input[name^="variable_enabled"]');
checkbox.attr('checked', !checkbox.attr('checked'));
return false;
}
else if ( field_to_edit == 'toggle_downloadable' ) {
var checkbox = jQuery('input[name^="variable_is_downloadable"]');
checkbox.attr('checked', !checkbox.attr('checked'));
jQuery('input.variable_is_downloadable').change();
return false;
}
else if ( field_to_edit == 'toggle_virtual' ) {
var checkbox = jQuery('input[name^="variable_is_virtual"]');
checkbox.attr('checked', !checkbox.attr('checked'));
jQuery('input.variable_is_virtual').change();
return false;
}
else if ( field_to_edit == 'delete_all' ) {
var answer = confirm('<?php _e( 'Are you sure you want to delete all variations? This cannot be undone.', 'woocommerce' ); ?>');
if (answer){
var answer = confirm('<?php _e( 'Last warning, are you sure?', 'woocommerce' ); ?>');
if (answer) {
var variation_ids = [];
jQuery('.woocommerce_variations .woocommerce_variation').block({ message: null, overlayCSS: { background: '#fff url(<?php echo $woocommerce->plugin_url(); ?>/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } });
jQuery('.woocommerce_variations .woocommerce_variation .remove_variation').each(function(){
var variation = jQuery(this).attr('rel');
if (variation>0) {
variation_ids.push(variation);
}
});
var data = {
action: 'woocommerce_remove_variations',
variation_ids: variation_ids,
security: '<?php echo wp_create_nonce("delete-variations"); ?>'
};
jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function(response) {
jQuery('.woocommerce_variations .woocommerce_variation').fadeOut('300', function(){
jQuery('.woocommerce_variations .woocommerce_variation').remove();
});
});
}
}
return false;
}
else {
var input_tag = jQuery('select#field_to_edit :selected').attr('rel') ? jQuery('select#field_to_edit :selected').attr('rel') : 'input';
var value = prompt("<?php _e( 'Enter a value', 'woocommerce' ); ?>");
jQuery(input_tag + '[name^="' + field_to_edit + '["]').val( value ).change();
return false;
}
});
jQuery('#variable_product_options').on('change', 'input.variable_is_downloadable', function(){
jQuery(this).closest('.woocommerce_variation').find('.show_if_variation_downloadable').hide();
if (jQuery(this).is(':checked')) {
jQuery(this).closest('.woocommerce_variation').find('.show_if_variation_downloadable').show();
}
});
jQuery('#variable_product_options').on('change', 'input.variable_is_virtual', function(){
jQuery(this).closest('.woocommerce_variation').find('.hide_if_variation_virtual').show();
if (jQuery(this).is(':checked')) {
jQuery(this).closest('.woocommerce_variation').find('.hide_if_variation_virtual').hide();
}
});
jQuery('input.variable_is_downloadable, input.variable_is_virtual').change();
// Ordering
$('#variable_product_options').on( 'woocommerce_variations_added', function() {
$('.woocommerce_variations').sortable( variation_sortable_options );
} );
$('.woocommerce_variations').sortable( variation_sortable_options );
function variation_row_indexes() {
$('.woocommerce_variations .woocommerce_variation').each(function(index, el){
$('.variation_menu_order', el).val( parseInt( $(el).index('.woocommerce_variations .woocommerce_variation') ) );
});
};
// Uploader
var variable_image_frame;
var setting_variation_image_id;
var setting_variation_image;
var wp_media_post_id = wp.media.model.settings.post.id;
jQuery('#variable_product_options').on('click', '.upload_image_button', function( event ) {
var $button = jQuery( this );
var post_id = $button.attr('rel');
var $parent = $button.closest('.upload_image');
setting_variation_image = $parent;
setting_variation_image_id = post_id;
event.preventDefault();
if ( $button.is('.remove') ) {
setting_variation_image.find( '.upload_image_id' ).val( '' );
setting_variation_image.find( 'img' ).attr( 'src', '<?php echo woocommerce_placeholder_img_src(); ?>' );
setting_variation_image.find( '.upload_image_button' ).removeClass( 'remove' );
} else {
// If the media frame already exists, reopen it.
if ( variable_image_frame ) {
variable_image_frame.uploader.uploader.param( 'post_id', setting_variation_image_id );
variable_image_frame.open();
return;
} else {
wp.media.model.settings.post.id = setting_variation_image_id;
}
// Create the media frame.
variable_image_frame = wp.media.frames.variable_image = wp.media({
// Set the title of the modal.
title: '<?php _e( 'Choose an image', 'woocommerce' ); ?>',
button: {
text: '<?php _e( 'Set variation image', 'woocommerce' ); ?>'
}
});
// When an image is selected, run a callback.
variable_image_frame.on( 'select', function() {
attachment = variable_image_frame.state().get('selection').first().toJSON();
setting_variation_image.find( '.upload_image_id' ).val( attachment.id );
setting_variation_image.find( '.upload_image_button' ).addClass( 'remove' );
setting_variation_image.find( 'img' ).attr( 'src', attachment.url );
wp.media.model.settings.post.id = wp_media_post_id;
});
// Finally, open the modal.
variable_image_frame.open();
}
});
// Restore ID
jQuery('a.add_media').on('click', function() {
wp.media.model.settings.post.id = wp_media_post_id;
} );
});
<?php
$javascript = ob_get_clean();
$woocommerce->add_inline_js( $javascript );
}
variable_product_type_options:这是原始函数,所以我删除了它并添加了variable_product_type_options_with_goldprice_calculation,但它没有保存服务器中的值。我做错了什么?
答案 0 :(得分:0)
您是否有机会使用WooCommerce版本2.0.11?
该版本的后端存在产品变体的错误。它已在最新版本(2.0.12)中修复,因此请更新WooCommerce并再次检查。
在此处查看更多相关信息(不幸的是需要WooThemes帐户阅读):http://support.woothemes.com/entries/24127722-Woocommerce-2-0-11-BUGGY