Add a custom text field to WooCommerce admin product data “Variations”

Overriding core files is really something to avoid absolutely for many reasons. To add a custom field to product variations options prices and save the value on save, use this: // Admin: Add custom field in product variations options pricing add_action( ‘woocommerce_variation_options_pricing’, ‘add_variation_custom_option_pricing’, 10, 3 ); function add_variation_custom_option_pricing( $loop, $variation_data, $variation ){ woocommerce_wp_text_input( array( ‘id’ … Read more

How to display product cropped thumbnail (150×150) for WooCommerce product categories

To include the product category thumbnail (125 × 125 px) in your product category items (buttons), use the following: $product_cats = get_terms([ ‘taxonomy’ => ‘product_cat’, ‘hide_empty’ => true, ‘parent’ => 0, ]); foreach ( $product_cats as $key => $parent_term ) { $thumb_id = get_woocommerce_term_meta( $parent_term->term_id, ‘thumbnail_id’, true ); $size=”thumbnail”; $image = wp_get_attachment_image_src($thumb_id, $size); printf( ‘<button … Read more

How can I default to all WordPress roles when parameter is not included in shortcode?

Try this: function profile_counts($atts) { $atts = shortcode_atts( array( ‘metakey’ => ”, ‘metavalue’ => ”, ‘userrole’ => ”, ), $atts ); $user_ids = get_users( array( ‘meta_key’ => $atts[‘metakey’], ‘meta_value’ => $atts[‘metavalue’], ‘role__in’ => wp_parse_list( $atts[‘userrole’] ), ‘fields’ => ‘ID’, // retrieve just the IDs ‘count_total’ => false, // no need for FOUND_ROWS() ) ); return … Read more

How to Add a Link to the Drop-Down User Menu in the Admin Bar?

You want to use $wp_admin_bar->add_node( $args ). Below is a tested working example. function wpse_add_toolbar_edit($wp_admin_bar) { $wp_admin_bar->add_node( array( ‘id’ => ‘mylink’, ‘title’ => ‘My New Link’, ‘href’ => ‘mailto:[email protected]’, ‘parent’ => ‘user-actions’ ) ); } add_action(‘admin_bar_menu’, ‘wpse_add_toolbar_edit’, 999); Note: The parent param adds the link to an existing ID. If you need to find the … Read more

WooCommerce – Display variation custom field value [closed]

Ok, I’ve managed to solve this. This is how i finally created variations custom field: add_action( ‘woocommerce_product_after_variable_attributes’, ‘variation_settings_fields’, 10, 3 ); add_action( ‘woocommerce_save_product_variation’, ‘save_variation_settings_fields’, 10, 2 ); function variation_settings_fields( $loop, $variation_data, $variation ) { woocommerce_wp_text_input( array( ‘id’ => ‘price_1_50[‘ . $variation->ID . ‘]’, ‘class’ => ‘price_1_50’, ‘type’ => ‘text’, ‘label’ => __( ‘Price | Product … Read more

Get WooCommerce shipping methods programmatically

You need to use something different as following (commented): // Loop through shipping packages from WC_Session (They can be multiple in some cases) foreach ( WC()->cart->get_shipping_packages() as $package_id => $package ) { // Check if a shipping for the current package exist if ( WC()->session->__isset( ‘shipping_for_package_’.$package_id ) ) { // Loop through shipping rates for … Read more

Submitting form via admin-post.php and handling errors

Am I supposed to just do normal redirects back to my form page? Something like wp_redirect( ‘/my-page/’, 302 );? Yes. How do I display my errors on the page that has the form? Well, there are various ways, including: Passing an error code as a query string in the redirect URL: example.com/my-page/?my_form_error=tos_agree_empty. Then in your … Read more