Warning: Division by zero - Wordpress - wordpress

I am trying to show the price with VAT included in woocommerce orders backoffice but i am getting this error when i try to divide the total by the quantity.
The total amount it shows without any problem, but if i add the division gives me this error.
Any help with this?
function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {
$val = ($item['type'] == 'line_item' || $item['type'] == 'shipping' ) ? ($item['total'] + $item['total_tax']) / $item['quantity'] : ' ';
$valdecimal = wc_format_decimal( $val, $dp='', $trim_zeros );
?>
<td class="item_fcost" data-sort-value="<?php echo $val; ?>">
<div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
<?php if ($val>0) echo '€'; echo $valdecimal;?>
</div>
</td>
<?php
};
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Preço com IVA</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );

SOLVED
$quantity = $item->get_quantity();
$val = ($item['type'] == 'line_item' || $item['type'] == 'shipping' ) ? ($item['total'] + $item['total_tax']) / $quantity : ' ';

Related

Fatal error: Uncaught Error: Cannot use object of type WC_Order_Refund as array in

I added this code below to add prices including VAT in the admin order page.
It work well but I tried to refund an order today and I got this error:
Fatal error: Uncaught Error: Cannot use object of type WC_Order_Refund as array in
How can I exclude this new price from the refund process ?
This custom price should not be submitted once I click on refund
//Add price inc VAT column on admin order page
function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {
$val = ($item['type'] == 'line_item' || $item['type'] == 'shipping') ? $item['total'] + $item['total_tax'] : ' ';
$valdecimal = wc_format_decimal( $val, $dp='', $trim_zeros );
?>
<td class="item_fcost" data-sort-value="<?php echo $val; ?>">
<div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
<?php if ($val>0) echo '$'; echo $valdecimal;?>
</div>
</td>
<?php
};
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Price inc VAT</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );
You can quit early if the incoming $item variable is a WC_Order_Refund object.
if ( 'WC_Order_Refund' == gettype( $item ) ) {
return;
}
Here's the updated code that does not generate any errors:
//Add price inc VAT column on admin order page
function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {
$val = ('line_item' == $item->get_type() || 'shipping' == $item->get_type() ) ? $item->get_total() + $item->get_total_tax() : ' ';
$valdecimal = wc_format_decimal( $val );
?>
<td class="item_fcost" data-sort-value="<?php echo $val; ?>">
<div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
<?php if ($val>0) echo '$'; echo $valdecimal;?>
</div>
</td>
<?php
};
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Price inc VAT</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );

How can I save a custom field of an attribute in Woocommerce?

Old
I am trying to create a custom field in the product attributes in Woocommerce. This to be able to select if an attribute is highlighted or not. For example:
https://i.stack.imgur.com/Ge76B.png
What I want to do is that the field that I specify in backend as highlighted is displayed in a certain way in the frontend.
So far I have been able to add the field, but I have not managed to figure out how to save it. This is what I have:
add_action('woocommerce_after_product_attribute_settings','wcb_add_product_attribute_is_highlighted', 10, 2);
add_filter( 'woocommerce_admin_meta_boxes_prepare_attribute', 'wcb_admin_meta_boxes_prepare_attribute', 10, 3);
function get_attribute_highlighted($id, $i) {
return get_post_meta( 1, "attribute_".$id."_highlighted_".$i, true);
}
function wcb_add_product_attribute_is_highlighted($attribute, $i=0) {
$value = get_attribute_highlighted($attribute->get_id(), $i); ?>
<tr>
<td>
<div class="enable_variation show_if_canopytour show_if_variable_canopytour">
<label><input type="checkbox" class="checkbox" <?php checked( $value, true ); ?> name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="1" /> <?php esc_html_e( 'Highlight attribute', $this->wcb ); ?></label>
</div>
</td>
</tr>
<?php
}
function wcb_admin_meta_boxes_prepare_attribute($attribute, $data, $i=0) {
// updated
if(array_key_exists("attribute_highlighted", $data) && is_array($data["attribute_highlighted"])) {
update_post_meta( 1, "attribute_".$attribute->get_id()."_highlighted_".$i, wc_string_to_bool($data["attribute_highlighted"][$i]) );
}
}
I tried the woocommerce_admin_meta_boxes_prepare_attribute filter using the offsetSet and offsetGet methods of the WC_Product_Attribute class but I can not understand how it works. I could not make it save my custom value.
Upgrade
I changed the woocommerce_admin_meta_boxes_prepare_attribute filter by the wp_ajax_woocommerce_save_attributes action and it seems to work. Now I have the problem that it is not updated after saving for the first time.
I explain the current problem: I have a checkbox that when clicked is activated, I save and when reloading the state it is maintained. If now I want to deactivate it, after saving the state it is not kept as disabled, it is put back as activated.
This is the updated code:
add_action('woocommerce_after_product_attribute_settings', 'wcb_add_product_attribute_is_highlighted', 10, 2);
add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 10);
function get_attribute_highlighted($id, $i) {
global $post;
$id = sanitize_title($id);
$id = strtolower($id);
$val = get_post_meta( $post->ID, "attribute_".$id."_highlighted_".$i, true);
return !empty($val) ? $val : false;
}
function wcb_add_product_attribute_is_highlighted($attribute, $i=0) {
$value = get_attribute_highlighted($attribute->get_name(), $i); ?>
<tr>
<td>
<div class="enable_highlighted show_if_canopytour show_if_variable_canopytour">
<label><input type="checkbox" class="checkbox" <?php checked( $value, true ); ?> name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="1" /> <?php esc_html_e( 'Highlight attribute', $this->wcb ); ?></label>
</div>
</td>
</tr>
<?php
}
function wcb_ajax_woocommerce_save_attributes() {
check_ajax_referer( 'save-attributes', 'security' );
parse_str( $_POST['data'], $data );
$post_id = absint( $_POST['post_id'] );
if(array_key_exists("attribute_highlighted", $data) && is_array($data["attribute_highlighted"])) {
foreach($data["attribute_highlighted"] as $i => $val) {
$attr_name = sanitize_title($data["attribute_names"][$i]);
$attr_name = strtolower($attr_name);
update_post_meta( $post_id, "attribute_".$attr_name."_highlighted_".$i, wc_string_to_bool($val) );
}
}
}
Check Ink's answer
Outdated
I have found the solution to my problem. I share the code in case someone is useful. Regards!
add_action('woocommerce_after_product_attribute_settings', 'wcb_add_product_attribute_is_highlighted', 10, 2);
add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 10);
function get_attribute_highlighted($id, $i) {
global $post;
$id = sanitize_title($id);
$id = strtolower($id);
$val = get_post_meta( $post->ID, "attribute_".$id."_highlighted_".$i, true);
return !empty($val) ? $val : false;
}
function wcb_add_product_attribute_is_highlighted($attribute, $i=0) {
$value = get_attribute_highlighted($attribute->get_name(), $i); ?>
<tr>
<td>
<div class="enable_highlighted">
<label><input type="hidden" name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="0" /><input type="checkbox" class="checkbox" <?php checked( $value, true ); ?> name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="1" /> <?php esc_html_e( 'Highlight attribute', $this->wcb ); ?></label>
</div>
</td>
</tr>
<?php
}
function wcb_ajax_woocommerce_save_attributes() {
check_ajax_referer( 'save-attributes', 'security' );
parse_str( $_POST['data'], $data );
$post_id = absint( $_POST['post_id'] );
if(array_key_exists("attribute_highlighted", $data) && is_array($data["attribute_highlighted"])) {
foreach($data["attribute_highlighted"] as $i => $val) {
$attr_name = sanitize_title($data["attribute_names"][$i]);
$attr_name = strtolower($attr_name);
update_post_meta( $post_id, "attribute_".$attr_name."_highlighted_".$i, wc_string_to_bool($val) );
}
}
}
In the end the only thing I had to add to my code was a hidden input with the same name as the checkbox but with a value of 0: <input type="hidden" name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="0" />
Here an image of the result (https://i.stack.imgur.com/VscT1.jpg). By clicking on save the value of the checkbox is maintained. The value is saved in a post_meta of the post that you are modifying. This is useful if you want to highlight a specific attribute in the front end.
I appreciate the help of #LoicTheAztec :)
As I struggled with the same issue I have tried using the code from Jesus Magallon. However, it did not work.
I found out that the priority of wp_ajax_woocommerce_save_attributes needed to be set to 0, otherwise the callback is not run for some reason.
Forthermore, if we only use $post->ID for get_post_meta() the correct results state of the checkbox will only be visible on page reload. To make sure the selected state is also visible direclty after ajax saving/reloading, we need to use $post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : $post->ID; instead.
Here the final version that should work perfectly for everyone:
add_action( 'woocommerce_after_product_attribute_settings', 'wcb_add_product_attribute_is_highlighted', 10, 2);
add_action( 'wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 0);
function get_attribute_highlighted( $attribute_name, $i ) {
global $post;
// ID for either from ajax or from post
$post_id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : $post->ID ;
$attribute_name = strtolower( sanitize_title( $attribute_name ) );
$val = get_post_meta( $post_id, 'attribute_' . $attribute_name . '_highlighted_' . $i, true );
return !empty( $val ) ? $val : false;
}
function wcb_add_product_attribute_is_highlighted( $attribute, $i=0 ) {
$value = get_attribute_highlighted($attribute->get_name(), $i); ?>
<tr>
<td>
<div class="enable_highlighted">
<label>
<input type="hidden" name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="0" />
<input type="checkbox" class="checkbox" <?php checked( $value, true ); ?> name="attribute_highlighted[<?php echo esc_attr( $i ); ?>]" value="1" />
<?php esc_html_e( 'Highlight attribute', 'textdomain' ); ?>
</label>
</div>
</td>
</tr>
<?php
}
function wcb_ajax_woocommerce_save_attributes() {
check_ajax_referer( 'save-attributes', 'security' );
parse_str( $_POST['data'], $data );
$post_id = absint( $_POST['post_id'] );
if( array_key_exists( 'attribute_highlighted', $data ) && is_array( $data['attribute_highlighted'] ) ) {
foreach( $data['attribute_highlighted'] as $i => $val ) {
$attr_name = sanitize_title( $data['attribute_names'][$i] );
$attr_name = strtolower( $attr_name );
update_post_meta( $post_id, 'attribute_' . $attr_name . '_highlighted_' . absint( $i ), wc_string_to_bool( $val ) );
}
}
}
#ink almost got it, but it's not working when you click on Update button of whole product directly. You always need to click on Save attributes button firstly and that's wrong, because when you try other checkboxes then you'll see it works also on Update click.
So I would completely remove that add_action( 'wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 0); and wcb_ajax_woocommerce_save_attributes function and use this instead:
add_filter( 'woocommerce_admin_meta_boxes_prepare_attribute', function( $attribute, $data, $i ){
global $post;
if( is_object( $post ) && isset( $post->ID ) ){
$post_id = $post->ID;
}else{
$post_id = absint( $_POST['post_id'] );
}
if( array_key_exists( 'attribute_highlighted', $data ) && is_array( $data['attribute_highlighted'] ) ){
foreach( $data['attribute_highlighted'] as $i => $value ){
$attr_name = sanitize_title( $data['attribute_names'][ $i ] );
update_post_meta( $post_id, 'attribute_' . $attr_name . '_highlighted_' . absint( $i ), wc_string_to_bool( $value ) );
}
}
return $attribute;
}, 10, 3 );
perfect code but change the hook
add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 10);
to
add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 0);
#Jesús Magallón's
Answer need little modifications.
Remove the function get_attribute_highlighted() and replace this line
$value = get_attribute_highlighted($attribute->get_name(), $i);
TO
global $post;
$post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : $post->ID ;
$value = get_post_meta($post_id , "attribute_description".$i, true);
It will show ajax saved value instance of empty;
And Change hooks
add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 10);
TO
add_action('wp_ajax_woocommerce_save_attributes', 'wcb_ajax_woocommerce_save_attributes', 0);

Get product custom attribute Woocommerce

I have a single product custom variation table template in functions.php and I'm trying to get product custom attribute. I have two them (date and location).
I'm found a lot of solutions, but none didn't work for me.
Woocommerce version 3.2.6.
With this code below I get
Notice: Only variables should be passed by reference.
And I get type 'Null' of $date.
<td class="date">
<?php
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
$date = array_shift( wc_get_product_terms( $product_id, 'pa_date', array( 'fields' => 'names' ) ) );
?>
</td>
This is my full product variation table code in function.php
function woocommerce_variable_add_to_cart(){
global $product, $post, $woocommerce;
$attributes = $product->get_attributes();
$variations = find_valid_variations();
// Check if the special 'price_grid' meta is set, if it is, load the default template:
if ( get_post_meta($post->ID, 'price_grid', true) ) {
wp_enqueue_script( 'wc-add-to-cart-variation' );
wc_get_template( 'single-product/add-to-cart/variable.php', array(
'available_variations' => $product->get_available_variations(),
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_variation_default_attributes()
) );
return;
}
?>
<table class="variations variations-grid" cellspacing="0">
<thead>
<tr>
<td> Date | Location </td>
<td> Price </td>
<td> Quantity </td>
<td> Availability </td>
<td> </td>
</tr>
</thead>
<tbody>
<?php
foreach ($variations as $key => $value) {
if( !$value['variation_is_visible'] ) continue;
?>
<tr>
<td class="date">
<?php
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id; // Added WC 3+ support
$date = array_shift( wc_get_product_terms( $product_id, 'pa_date', array( 'fields' => 'names' ) ) );
?>
</td>
<td class="price">
<?php echo '<span>£</span>' . $product->get_price(); ?>
</td>
<td class="quantity">
<?php woocommerce_quantity_input(); ?>
</td>
<td class="stock">
<?php if (!$value['is_in_stock'] ) { ?>
<p class="stock out-of-stock"><?php _e( 'Places Not Available', 'woocommerce' ); ?></p>
<?php } else { ?>
<p class="stock in-stock"><?php _e( 'Places Available', 'woocommerce' ); ?></p>
</td>
<td class="add-to-cart">
<form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" method="post" enctype='multipart/form-data'>
<?php
if(!empty($value['attributes'])){
foreach ($value['attributes'] as $attr_key => $attr_value) {
?>
<input type="hidden" name="<?php echo $attr_key?>" value="<?php echo $attr_value?>">
<?php
}
}
?>
<button type="submit" class="single_add_to_cart_button button alt"><span class="glyphicon glyphicon-tag"></span> Add to cart</button>
<input type="hidden" name="variation_id" value="<?php echo $value['variation_id']?>" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $post->ID ); ?>" />
</form>
<?php } ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
}
function find_valid_variations() {
global $product;
$variations = $product->get_available_variations();
$attributes = $product->get_attributes();
$new_variants = array();
foreach( $variations as $variation ) {
// Peruse the attributes.
// 1. If both are explicitly set, this is a valid variation
// 2. If one is not set, that means any, and we must 'create' the rest.
$valid = true; // so far
foreach( $attributes as $slug => $args ) {
if( array_key_exists("attribute_$slug", $variation['attributes']) && !empty($variation['attributes']["attribute_$slug"]) ) {
// Exists
} else {
// Not exists, create
$valid = false; // it contains 'anys'
foreach( explode( '|', $attributes[$slug]['value']) as $attribute ) {
$attribute = trim( $attribute );
$new_variant = $variation;
$new_variant['attributes']["attribute_$slug"] = $attribute;
$new_variants[] = $new_variant;
}
}
}
// This contains ALL set attributes, and is itself a 'valid' variation.
if( $valid )
$new_variants[] = $variation;
}
return $new_variants;
}
UPDATE
I got this code working, but I get both attribute values (date location) in one tag.
How to seperate attributes?
<td>
<?php
foreach($value['attributes'] as $key => $val ) {
$val = str_replace(array('-','_'), ' ', $val);
printf( '<span class="attr attr-%s">%s</span>', $key, ucwords($val) );
}
?>
</td>
UPDATE
Finally I get date and location but with extra warnings, notices, and single characters.
Code
<?php
foreach($value as $date ) {
printf($date['attribute_date']);
}
?>
** SOLVED **
Maybe not the best method, but it's working.
<td class="date">
<?php
$i = 0;
foreach($value['attributes'] as $key => $val ) {
if($i == 0 ) {
echo $val;
}
$i++;
}
?>
</td>
<td class="location">
<?php
$i = 0;
foreach($value['attributes'] as $key => $val ) {
if($i !== 0) {
echo $val;
}
$i++;
}
?>
</td>
Use the method get_attributes() and function wc_get_product_terms() described here
<?php
// Get the attributes
$attributes = $product->get_attributes();
// Start the loop
foreach ( $attributes as $attribute ) : ?>
<?php
// Check and output, adopted from /templates/single-product/product-attributes.php
if ( $attribute['is_taxonomy'] ) {
$values = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
} else {
// Convert pipes to commas and display values
$values = array_map( 'trim', explode( WC_DELIMITER, $attribute['value'] ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
?>
<?php endforeach; ?>
If you look at the template \templates\single-product\product-attributes.php you can see how to get the individual attribute names.
if ( $attribute->is_taxonomy() ) {
$attribute_taxonomy = $attribute->get_taxonomy_object();
$attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );
foreach ( $attribute_values as $attribute_value ) {
$value_name = esc_html( $attribute_value->name );
if ( $attribute_taxonomy->attribute_public ) {
$values[] = '' . $value_name . '';
} else {
$values[] = $value_name;
}
}
}
Here's how to get specific Custom Attribute:
// Get the custom attribute
$svd_attribute = array_shift( wc_get_product_terms( $product->id, 'your_custom_attribute', array( 'fields' => 'names' ) ) );
// Display if String
if ( is_string( $svd_attribute ) ) {
echo wp_kses_post( "$svd_attribute" );
}
Just replace "your_custom_attribute"
Working on WooCommerce: 3.8.0

Woocommerce - Admin Order Detail Page

I add one product for e.g “ABC” – price : $10 (including VAT 20%)
So product net price = £8 and tax = £2
In frontend on product detail page i show product price including TAX.($10)
When Customer purchase this product then in woocommerce order generate like this
product price = $8
tax = $2
-------------------
Total = $10
But i want price in admin order detail page like
product price : $10(including 20% VAT)
Not want to show VAT separate.
Thanks
Try this. Put it in your theme function.php file
//Add price inc VAT column on admin order page
function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {
$val = ($item['type'] == 'line_item' || $item['type'] == 'shipping') ? $item['total'] + $item['total_tax'] : ' ';
$valdecimal = wc_format_decimal( $val, $dp='', $trim_zeros );
?>
<td class="item_fcost" data-sort-value="<?php echo $val; ?>">
<div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
<?php if ($val>0) echo '$'; echo $valdecimal;?>
</div>
</td>
<?php
};
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Price inc VAT</th>';
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );
I updated the code above slightly, so it also works with fees and negative amounts:
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
function action_woocommerce_admin_order_item_values( $null, $item, $item_id ) {
$validOrderItemTypes = ['line_item', 'shipping', 'fee'];
$val = ( in_array( $item['type'], $validOrderItemTypes )) ? $item['total'] + $item['total_tax'] : '−';
$price = wc_price( $val );
?>
<td class="item_fcost" data-sort-value="<?php echo $val; ?>">
<div class="view" style="font-weight: bold; text-align: right; padding-right: 10px;">
<?php echo $price; ?>
</div>
</td>
<?php
};
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 3 );
function action_woocommerce_admin_order_item_headers( $order ) {
echo '<th class="item_fcost sortable" data-sort="float" style="text-align: right;">Price incl. VAT</th>';
};

selected not saved ?simple bug ?/ I am beginner

I have this function, which gives me dropdown option:
public function edit_tax_image_field( $term ){
$term_id = $term->term_id;
$term_meta = get_option( "taxonomy_$term_id" );
$image = $term_meta['tax_image'] ? $term_meta['tax_image'] : '';
?>
<tr class="form-field">
<th scope="row">
<label for="term_meta[tax_image]">Dropdown menu</label>
<td>
<select name="term_meta[tax_image]" id="term_meta[tax_image]" style="width: 300px;">
<?php
$selected = $image;
$p = '';
$r = '';
foreach ( _s_sample_select_options() as $option ) {
$label = $option['label'];
if ( $selected == $option['value'] ) // Make default first in list
$p = "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";
else
$r .= "\n\t<option style=\"padding-right: 10px;\" value='" . esc_attr( $option['value'] ) . "'>$label</option>";
}
echo $p . $r;
?></select>
<p class="description">Some description.</p>
</td>
</th>
</tr><!-- /.form-field -->
<?php
} // edit_tax_image_field
This is my saving function
public function save_tax_meta( $term_id ){
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = array();
$term_meta['tax_image'] = isset ( $_POST['term_meta']['tax_image'] ) ? esc_attr( $_POST['term_meta']['tax_image'] ) : '';
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
} // if isset( $_POST['term_meta'] )
} // save_tax_meta
I do not know, where should be bug. I am beginner in coding, so ... bug is maybe simple or not.
Solving this problem cost me one day :D and still have no solution :(
try changing
if ( $selected == $option['value'] ) // Make default first in list
$p = "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";
to
if ( $selected == $option['value'] ) // Make default first in list
$p .= "\n\t<option style=\"padding-right: 10px;\" selected='selected' value='" . esc_attr( $option['value'] ) . "'>$label</option>";

Resources