Wordpress Bulk Edit deletes custom taxonomy value - wordpress

I have a custom taxonomy that I am "auto" assigning a value whenever a post is saved. It is saving the first letter of the post_title so I can use in a custom A to Z list (similar to this: http://geekgirllife.com/alphabetical-index-of-posts-in-wordpress/)
I have registered the taxonomy in my theme's functions.php as well as the auto assign function. Everything is working, when create/edit/save a post it adds the first letter of the post title into this custom tax.
However when I bulk edited some posts from the default WP bulk actions functionality, it overwrites the custom taxonomy value for the posts being edited.
My question is, is there a way to exclude a custom taxonomy from the bulk edit action in WP? Or a way bulk edit action will treat a custom tax as "no change" rather than update it w/ empty value?
Register Taxonomy code:
function atoz_tax() {
register_taxonomy( 'atoz',array (
0 => 'page',
),
array( 'hierarchical' => false,
'label' => 'atoz',
'show_ui' => false,
'query_var' => true,
'show_admin_column' => false,
) );
}
add_action('init', 'atoz_tax');
Code that auto saves to taxonomy on save:
function atoz_save_first_letter( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
//Check permissions
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
// find and save the data
$taxonomy = 'atoz';
//set term as first letter of post title, lower case
wp_set_object_terms($post_id,strtolower(substr($_POST['post_title'], 0, 1)), $taxonomy);
}
add_action( 'save_post', 'atoz_save_first_letter' );

Related

Wordpress: save custom billing field in account Billing address

Using Wordpress/Woocommerce.
I have this code which adds a custom billing field in checkout page called: "NIF/CIF". It works fine but its value it not saved in the customer account "Billing Address" data.
Once the customer makes a first order all billing address values are saved in his account: Address, State, Country, etc. But my custom field is not saved.
I guess that in my function is missing a line of code to save its value in the database, but I don't know how to start with this.
/*******************************
CUSTOM BILLING FIELD
*********************************/
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['nif_cif'] = array(
'label' => __('NIF/CIF', 'woocommerce'), // Add custom field label
'placeholder' => _x('NIF/CIF', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('my-css') // add class name
);
return $fields;
}
Here is an example of how to save your custom field:
add_action( 'woocommerce_checkout_order_processed', 'prefix_save_field_on_checkout', 11, 2 );
function checkout_order_processed_add_referral_answer( $order_id, $posted ) {
if ( ! isset( $_POST['nif_cif'] ) ) {
return;
}
$order = wc_get_order( $order_id );
// WC < 3.0
update_post_meta( $order->id, 'order_meta_field_name', wc_clean( $_POST['nif_cif'] ) );
// WC > 3.0
$order->add_meta_data( 'order_meta_field_name', wc_clean( $_POST['nif_cif'] ), true );
$order->save();
}
Adding an extra field through 'woocommerce_billing_fields' hook is not enough. you are missing out two things.
Proper validation of NIF/CIF field using
'woocommerce_after_checkout_validation' hook
Saving data in order
using 'woocommerce_checkout_order_processed' hook

How to add short description in WooCommerce checkout page

Thanks for reading, Just had a issue regarding WooCommerce, I want to add a short description checkout page of below billing field.
How to add short description in WooCommerce checkout page of below billing field?
I tried add function, custom code but failed with error.
add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
function wc_checkout_description_so_27900033( $other_data, $cart_item )
{
$post_data = get_post( $cart_item['product_id'] );
$other_data[] = array( 'name' => 'description', 'value' => $post_data->post_excerpt );
return $other_data;
}
I was used this code but it is showing inner product info table.
There's no real reason to call get_post(). The $product object is stored in the $cart_item array and the $post object is stored inside the $product. This gets the product's excerpt (aka the short description) to show up in the cart and in the checkout. Now, it isn't likely the make the description show up on the order received page, or in the my account area, or in emails, etc since the only place that the woocommerce_get_item_data filter appears is in the cart class.
One thing to take note of, WooCommerce 2.7 is a major rewrite of WooCommerce and $_product->post->post_excerpt will result in PHP notices about directly accessing product properties. So I've suggested both the 2.6 and 2.7 compatible approaches.
add_filter( 'woocommerce_get_item_data', 'wc_checkout_description_so_27900033', 10, 2 );
function wc_checkout_description_so_27900033( $other_data, $cart_item )
{
$_product = $cart_item['data'];
// Use this for WC2.7
//$other_data[] = array( 'name' => 'description', 'value' => $_product->get_short_description() );
// Use this for WC2.6
$other_data[] = array( 'name' => 'description', 'value' => $_product->post->post_excerpt );
return $other_data;
}

Change custom post type slug from Settings > Permalinks page

add_action( 'init', 'register_my_types' );
function register_my_types() {
register_post_type( 'movies',
array(
'labels' => array(
'name' => __( 'Movies' ),
'singular_name' => __( 'Movie' )
),
'public' => true,
'rewrite' => array(
'slug' => 'films'
),
)
);
}
I can use the following to change the slug from my plugin settings page (on save to get 'filme' from a field):
add_filter( 'register_post_type_args', 'movies_register_post_type_args', 10, 2 );
function movies_register_post_type_args( $args, $post_type ) {
if ( 'movies' === $post_type ) {
$args['rewrite']['slug'] = 'filme';
}
return $args;
}
But, I want to be able to modify the "films" slug from Settings > Permalinks page.
How do I add a custom post type slug to Settings > Permalinks page?
Update:
In the end I created a form field in the plugin settings page and I updated the filter like this:
function register_post_type_args( $args, $post_type ) {
if ($this->plugin_name === $post_type ) {
$slug=get_option( $this->plugin_name.'_slug' );
if($args['rewrite']['slug']!=$slug){
$args['rewrite']['slug'] = $slug;
}
}
return $args;
}
But I'm still looking for a way to change the slug from Settings / Permalinks.
You can use plugins like this:
Toolset Types
Custom Post Type UI
You should use WordPress Settings API:
Add a setting field for your slug, attached to the permalinks page with add_settings_field()
Write the callback function to display the <input> fied in the page
Write the function that record the value in the database after the form submission with update_option()
All you have to do then is modify your register_post_type() function. The get_option() function will return the value that has to be affected to slug (don't forget to test its existence).

Add Tax Exempt form on checkout in woocommerce

I am trying to add a form to my checkout page so when a user clicks the 'Tax Exempt' checkbox, a textbox will popup and ask the user what the Tax Exempt Id number is.
I got all of that working great, and I even added the update_totals_on_change class to my form field so it will update the totals.
My next step was to add an action/filter on a method so when the update_totals_on_change executes, I can set the tax to 0 and then it will finish calculating the total.
Does anyone know which functions I can hook on to?
Looking at the checkout.js file in WooCommerce, they set the action to woocommerce_update_order_review for the ajax operation.
I tried following that but soon got lost.
I was thinking I could add some post data by hooking in to woocommerce_checkout_update_order_review
and then hooking in to woocommerce_before_calculate_totals to modify the tax stuff, but I have no idea what I need to modify.
Am I even on the right path?
Alright, I finally figured it out in case anyone is interested.
In my plugin, I made a form after the order notes by hooking in to this function: 'woocommerce_before_order_notes'
add_action('woocommerce_before_order_notes', array(&$this, 'taxexempt_before_order_notes') );
my 'taxexempt_before_order_notes' function contained:
function taxexempt_before_order_notes( $checkout ) {
echo '<div style="clear: both"></div>
<h3>Tax Exempt Details</h3>';
woocommerce_form_field( 'tax_exempt_checkbox', array(
'type' => 'checkbox',
'class' => array('tiri taxexempt'),array( 'form-row-wide', 'address-field' ),
'label' => __('Tax Exempt'),
), $checkout->get_value( 'tax_exempt_checkbox' ));
woocommerce_form_field( 'tax_exempt_name', array(
'type' => 'text',
'class' => array('form-row-first', 'tiri', 'taxexempt', 'textbox', 'hidden'),
'label' => __('Tax Exempt Name'),
), $checkout->get_value( 'tax_exempt_name' ));
woocommerce_form_field( 'tax_exempt_id', array(
'type' => 'text',
'class' => array('form-row-last', 'tiri', 'taxexempt', 'textbox', 'hidden', 'update_totals_on_change'),
'label' => __('Tax Exempt Id'),
), $checkout->get_value( 'tax_exempt_id' ));
}
Then the most important woocommerce function to hook was: 'woocommerce_checkout_update_order_review'
add_action( 'woocommerce_checkout_update_order_review', array(&$this, 'taxexempt_checkout_update_order_review' ));
function taxexempt_checkout_update_order_review( $post_data ) {
global $woocommerce;
$woocommerce->customer->set_is_vat_exempt(FALSE);
parse_str($post_data);
if ( isset($tax_exempt_checkbox) && isset($tax_exempt_id) && $tax_exempt_checkbox == '1' && !empty($tax_exempt_id))
$woocommerce->customer->set_is_vat_exempt(true);
}
I simply parsed out the $post_data that is the serialized form data from the checkout.js file in woocommerce and checked if my part of the form was filled out correctly.
If it was, then I would set the tax exempt for the user.
The accepted solution didn't work for me, but I modified it to use the following:
//=============================================================================
// ADD TAX EXEMPT CHECKMARK
// =============================================================================
add_action( 'woocommerce_after_order_notes', 'qd_tax_exempt');
function qd_tax_exempt( $checkout ) {
echo '<div id="qd-tax-exempt"><h3>'.__('Tax Exempt').'</h3>';
woocommerce_form_field( 'shipping_method_tax_exempt', array(
'type' => 'checkbox',
'class' => array(),
'label' => __('My organization is tax exempt.'),
'required' => false,
), $checkout->get_value( 'shipping_method_tax_exempt' ));
echo '</div>';
}
add_action( 'woocommerce_checkout_update_order_review', 'taxexempt_checkout_update_order_review');
function taxexempt_checkout_update_order_review( $post_data ) {
global $woocommerce;
$woocommerce->customer->set_is_vat_exempt(FALSE);
parse_str($post_data);
if ( isset($shipping_method_tax_exempt) && $shipping_method_tax_exempt == '1')
$woocommerce->customer->set_is_vat_exempt(true);
}
The key here is understanding that any field with a name that starts with shipping_method is going to inherit this updating order functionality (which was the part that didn't work for me). I found this answer at http://www.affectivia.com/blog/have-a-checkbox-on-the-checkout-page-which-updates-the-order-totals/
After a long search I found that there is a method for the cart object called remove_taxes() .
So, after setting a user meta for the tax exempt users, this cancels the tax totals.
function remove_tax_for_exempt( $cart ) {
global $current_user;
$ok_taxexp = get_the_author_meta( 'granted_taxexempt', $current_user->ID );
if ($ok_taxexp){ // now 0 the tax if user is tax exempt
$cart->remove_taxes();
}
return $cart;
}
add_action( 'woocommerce_calculate_totals', 'remove_tax_for_exempt' );
Because $cart->remove_taxes(); is deprecated. This is what I used instead.
I didn't have a form on the frontend, but have a user roll that is tax exempt. This was my solution.
Also worth noting that set_is_vat_exempt(true) also works in the US to set as tax exempt.
/**
* Set customer as tax exempt if user is a wholesale customer
*/
function remove_tax_for_exempt( $cart ) {
global $woocommerce;
if ( is_user_logged_in() && current_user_can( 'wholesale_customer' ) ) {
$woocommerce->customer->set_is_vat_exempt(true);
}
return $cart;
}
add_action( 'woocommerce_calculate_totals', 'remove_tax_for_exempt' );
Since this answer still pops up on google, I thought I'd share that setting the customer as tax exempt only works during checkout, if you need to edit the order on the back-end after it is placed and use the "recalculate" button, the taxes will still appear. Fortunately there is a hook for this as well:
function remove_tax_for_exempt($exempt, $order){
return $exempt || user_can($order->get_user_id(), 'wholesale_customer');
}
add_filter('woocommerce_order_is_vat_exempt', 'remove_tax_for_exempt', 10, 2);

Wordpress: Can you assign the regular post categories to custom post types?

I am just using custom post types for the first time today, so please forgive my ignorance.
I am using a custom post type that is predefined by a plugin. It looks like almost every Event Calendar plugin uses custom post types to set up an "Events" post type.
I was wondering if there is a way to use the normal categories I assign to my regular posts, to assign to the custom events posts.
For example, I have regional categories, like "Southeast" that I have been using for regular posts, but I would also like to be able to assign this category to event posts, so that when people look at the "Southeast" category archive, they can see the regular posts and the events posts associated with that category.
Is this possible?
Thanks for any help in advance
Simple:
add_action( 'init', 'myfuncxx'); function myfuncxx() {
register_taxonomy_for_object_type( 'category', 'custom_postttt_typee' );
}
You would want to use the WordPress function register_taxonomy_for_object_type() Put the following into your theme's functions.php file:
function add_categories_to_events() {
register_taxonomy_for_object_type( 'post_tag', 'event' );
}
add_action( 'init', 'add_categories_to_events' );
I used the code and instructions here:
http://wp.miragearts.com/allinone-event-calendar-events-blog-home-categories-tags/
I found with this code added to the functions.php, that if I create two categories (one for regular posts and one for the event custom posts) with the exact same name and slug, then it is basically the same as having one category.
I think this may be slowing my site down a bit, but it's really too early to tell if it will cause problems.
Here's a copy of the code for the functions.php:
// Add this to your theme's functions.php
function edit_my_query($query) {
// Modify category and tag listings to include ai1ec events and all uses of the same term
// across event and post taxonomies
// ie live-music or arts whether they are event or post categories
// also include ai1ec events in blog home and feeds
if ( ( is_home() || is_feed() || is_category() || is_tag() )
&& empty( $query->query_vars['suppress_filters'] ) ) {
// The 'suppress_filters' test above keeps your menus from breaking
$post_type = get_query_var('post_type');
if($post_type && $post_type[0] != 'post') {
$post_type = $post_type;
} else {
$post_type = array('post','ai1ec_event'); // add custom post types here
}
$query->set('post_type',$post_type);
if (is_category() || is_tag()) {
// Add custom taxonomies to category and tag pages
if (is_category()) {
$taxonomy1 = 'category';
$taxonomy2 = 'events_categories';
}
if (is_tag()){
$taxonomy1 = 'post_tag';
$taxonomy2 = 'events_tags';
}
$queried_object = $query->get_queried_object();
$slug = $queried_object->slug;
$query->set('tax_query', array(
'relation' => 'OR',
array(
'taxonomy' => $taxonomy1, 'field' => 'slug', 'terms' => $slug
),
array(
'taxonomy' => $taxonomy2, 'field' => 'slug', 'terms' => $slug
)
));
}
}
}
add_action('pre_get_posts', 'edit_my_query');

Resources