Forum Replies Created
-
AuthorPosts
-
December 15, 2023 at 7:02 am in reply to: Add a custom field to each shipping method settings in WooCommerce admin #9917
loictheaztec
ParticipantUse the following, to add a custom field in WooCommerce admin shipping, to each shipping methods setting fields:
add_action('woocommerce_init', 'woocommerce_shipping_instances_form_fields_filters'); function woocommerce_shipping_instances_form_fields_filters(){ foreach( WC()->shipping->get_shipping_methods() as $shipping_method ) { add_filter('woocommerce_shipping_instance_form_fields_' . $shipping_method->id, 'shipping_methods_additional_custom_field'); } } function shipping_methods_additional_custom_field( $settings ) { $settings['shipping_comment'] = array( 'title' => __('Shipping Comment', 'woocommerce'), 'type' => 'text', 'placeholder' => __( 'Enter any additional comments for this shipping method.', 'woocommerce' ), ); return $settings; }
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
December 14, 2023 at 8:57 am in reply to: Change product price based on customer billing country in WooCommerce #9919loictheaztec
ParticipantThe hook
woocommerce_get_price
is obsolete and deprecated since WooCommerce 3 and has been replaced with the following hooks:woocommerce_product_get_price
(for products)woocommerce_product_variation_get_price
(for variations of a variable product).
There are some other mistakes in your code. Try the following revised code:
add_filter('woocommerce_product_get_price', 'country_based_cart_item_price', 100, 2); add_filter('woocommerce_product_variation_get_price', 'country_based_cart_item_price', 100, 2); function country_based_cart_item_price( $price, $product ) { // Define below in the array the desired country codes $targeted_countries = array('US'); $billing_country = WC()->customer->get_billing_country(); // Only on cart and checkout pages if ( ( is_checkout() || is_cart() ) && in_array($billing_country, $targeted_countries) ){ // Returns changed price return $price / 260 * 1.25; } return $price; }
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
December 14, 2023 at 7:12 am in reply to: Prevent order go to processing status if condition is true #9921loictheaztec
ParticipantYou can try the following for Cash on delivery (COD) payment method, replacing in the code both
'my_variable'
with the correct slug:add_filter( 'woocommerce_cod_process_payment_order_status', 'conditionally_change_cod_order_status_to_pending', 10, 2 ); function conditionally_change_cod_order_status_to_pending( $status, $order ) { $global_var = $GLOBALS; if ( isset($global_var['my_variable']) && $global_var['my_variable'] ) { return 'pending'; } return $status; }
Code goes in functions.php file of your child theme (or in a plugin). It should work.
December 14, 2023 at 2:42 am in reply to: WordPress ACF Datepicker Field Update To Todays Date But Keep Time At Value In Post #9924loictheaztec
ParticipantTo generalize your code for all posts of a custom post type try the following:
function update_all_acf_datetime_fields() { $post_type = 'my_post_type'; // Here define your custom post type $field_name = 'date_time'; // Replace with your actual ACF field slug $today_date = date('Y-m-d'); // Get all posts IDs from a custom post type $posts_ids = get_posts( array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids' ) ); // Loop through the posts IDs foreach( $posts_ids as $post_id ) { // Get the current value of the ACF datetime field $current_datetime = get_field($field_name, $post_id); // If the field has a value, update only the date part to today if ( ! empty($current_datetime) ) { $updated_datetime = $today_date . substr($current_datetime, 10); // Preserve the time part update_field($field_name, $updated_datetime, $post_id); } } } // Call the function to update the ACF datetime field update_all_acf_datetime_fields();
It should work…
February 15, 2021 at 8:52 am in reply to: How to remove all Woocommerce checkout billing fields without errors #9466loictheaztec
ParticipantThis requires to be done in a different way, as in real word WooCommerce ask for checkout billing country throwing an error like "Please enter an address to continue.":
To avoid this problem use the following instead (where you will define all key fields to be removed):
// Just hide woocommerce billing country add_action('woocommerce_before_checkout_form', 'hide_checkout_billing_country', 5); function hide_checkout_billing_country() { echo '<style>#billing_country_field{display:none;}</style>'; } add_filter('woocommerce_billing_fields', 'customize_billing_fields', 100); function customize_billing_fields($fields ) { if (is_checkout()) { // HERE set the required key fields below $chosen_fields = array('first_name', 'last_name', 'address_1', 'address_2', 'city', 'postcode', 'country', 'state'); foreach ($chosen_fields as $key) { if (isset($fields['billing_'.$key]) && $key !== 'country') { unset($fields['billing_'.$key]); // Remove all define fields except country } } } return $fields; }
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
loictheaztec
ParticipantAs this is a background process on server side, don’t use javascript.
1). WC Logs and the
WC_Logger
Class in WooCommerce for better debuggingTo access the results of the log easily from the dashboard, you can log to a WC logger rather than the error log.
You can access error logs by going to WooCommerce > System Status > Logs.
Then you will be able to choose and "view"the error log file you need, giving you the debugging details that you need. Error logs are also located in the /wc-logs folder within your site install.
Running a stack trace on a caught exception (example):
// Log any exceptions to a WC logger $log = new WC_Logger(); $log_entry = print_r( $e, true ); $log_entry .= 'Exception Trace: ' . print_r( $e->getTraceAsString(), true ); $log->log( 'new-woocommerce-log-name', $log_entry );
Notes:
-
WC_Logger
methods have been updated since WooCommerce 3: So logging can be grouped by context and severity. -
Use
WC_Logger
log()
method instead ofadd()
method due to upcoming deprecation (thanks to @Vizz85).
For example:
$logger = wc_get_logger(); $logger->debug( 'debug message', array( 'source' => 'my-extension' ) );
Related:
- Develop WooCommerce blog (january 2017): Improved logging in WooCommerce 3
- Documentation on the
WC_Logger
available methods
2). Debugging with WordPress
WP_DEBUG
Log (as an alternative)a) First edit your
wp-config.php
file adding the following lines to enable debug (if these are already defined, edit the values):define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false );
As errors are logged, they should appear in
wp-content/debug.log
. You can open this file in a text editor.b) On your code: Use the following (where
$variable
is the variable to be displayed in the error log:error_log( print_r( $variable, true ) );
Now you will get the data for debugging.
December 16, 2017 at 8:53 am in reply to: Create programmatically a variable product and two new attributes in WooCommerce #10014loictheaztec
ParticipantAfter: Create programmatically a WooCommerce product variation with new attribute values
Here you get the way to create a new variable product with new product attributes + values:
/** * Save a new product attribute from his name (slug). * * @since 3.0.0 * @param string $name | The product attribute name (slug). * @param string $label | The product attribute label (name). */ function save_product_attribute_from_name( $name, $label='', $set=true ){ if( ! function_exists ('get_attribute_id_from_name') ) return; global $wpdb; $label = $label == '' ? ucfirst($name) : $label; $attribute_id = get_attribute_id_from_name( $name ); if( empty($attribute_id) ){ $attribute_id = NULL; } else { $set = false; } $args = array( 'attribute_id' => $attribute_id, 'attribute_name' => $name, 'attribute_label' => $label, 'attribute_type' => 'select', 'attribute_orderby' => 'menu_order', 'attribute_public' => 0, ); if( empty($attribute_id) ) { $wpdb->insert( "{$wpdb->prefix}woocommerce_attribute_taxonomies", $args ); set_transient( 'wc_attribute_taxonomies', false ); } if( $set ){ $attributes = wc_get_attribute_taxonomies(); $args['attribute_id'] = get_attribute_id_from_name( $name ); $attributes[] = (object) $args; //print_r($attributes); set_transient( 'wc_attribute_taxonomies', $attributes ); } else { return; } } /** * Get the product attribute ID from the name. * * @since 3.0.0 * @param string $name | The name (slug). */ function get_attribute_id_from_name( $name ){ global $wpdb; $attribute_id = $wpdb->get_col("SELECT attribute_id FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name LIKE '$name'"); return reset($attribute_id); } /** * Create a new variable product (with new attributes if they are). * (Needed functions: * * @since 3.0.0 * @param array $data | The data to insert in the product. */ function create_product_variation( $data ){ if( ! function_exists ('save_product_attribute_from_name') ) return; $postname = sanitize_title( $data['title'] ); $author = empty( $data['author'] ) ? '1' : $data['author']; $post_data = array( 'post_author' => $author, 'post_name' => $postname, 'post_title' => $data['title'], 'post_content' => $data['content'], 'post_excerpt' => $data['excerpt'], 'post_status' => 'publish', 'ping_status' => 'closed', 'post_type' => 'product', 'guid' => home_url( '/product/'.$postname.'/' ), ); // Creating the product (post data) $product_id = wp_insert_post( $post_data ); // Get an instance of the WC_Product_Variable object and save it $product = new WC_Product_Variable( $product_id ); $product->save(); ## ---------------------- Other optional data ---------------------- ## ## (see WC_Product and WC_Product_Variable setters methods) // THE PRICES (No prices yet as we need to create product variations) // IMAGES GALLERY if( ! empty( $data['gallery_ids'] ) && count( $data['gallery_ids'] ) > 0 ) $product->set_gallery_image_ids( $data['gallery_ids'] ); // SKU if( ! empty( $data['sku'] ) ) $product->set_sku( $data['sku'] ); // STOCK (stock will be managed in variations) $product->set_stock_quantity( $data['stock'] ); // Set a minimal stock quantity $product->set_manage_stock(true); $product->set_stock_status(''); // Tax class if( empty( $data['tax_class'] ) ) $product->set_tax_class( $data['tax_class'] ); // WEIGHT if( ! empty($data['weight']) ) $product->set_weight(''); // weight (reseting) else $product->set_weight($data['weight']); $product->validate_props(); // Check validation ## ---------------------- VARIATION ATTRIBUTES ---------------------- ## $product_attributes = array(); foreach( $data['attributes'] as $key => $terms ){ $taxonomy = wc_attribute_taxonomy_name($key); // The taxonomy slug $attr_label = ucfirst($key); // attribute label name $attr_name = ( wc_sanitize_taxonomy_name($key)); // attribute slug // NEW Attributes: Register and save them if( ! taxonomy_exists( $taxonomy ) ) save_product_attribute_from_name( $attr_name, $attr_label ); $product_attributes[$taxonomy] = array ( 'name' => $taxonomy, 'value' => '', 'position' => '', 'is_visible' => 0, 'is_variation' => 1, 'is_taxonomy' => 1 ); foreach( $terms as $value ){ $term_name = ucfirst($value); $term_slug = sanitize_title($value); // Check if the Term name exist and if not we create it. if( ! term_exists( $value, $taxonomy ) ) wp_insert_term( $term_name, $taxonomy, array('slug' => $term_slug ) ); // Create the term // Set attribute values wp_set_post_terms( $product_id, $term_name, $taxonomy, true ); } } update_post_meta( $product_id, '_product_attributes', $product_attributes ); $product->save(); // Save the data }
Code goes in function.php file of your active child theme (or active theme). Tested and works.
USAGE (example with 2 new attributes + values):
create_product_variation( array( 'author' => '', // optional 'title' => 'Woo special one', 'content' => '<p>This is the product content <br>A very nice product, soft and clear…<p>', 'excerpt' => 'The product short description…', 'regular_price' => '16', // product regular price 'sale_price' => '', // product sale price (optional) 'stock' => '10', // Set a minimal stock quantity 'image_id' => '', // optional 'gallery_ids' => array(), // optional 'sku' => '', // optional 'tax_class' => '', // optional 'weight' => '', // optional // For NEW attributes/values use NAMES (not slugs) 'attributes' => array( 'Attribute 1' => array( 'Value 1', 'Value 2' ), 'Attribute 2' => array( 'Value 1', 'Value 2', 'Value 3' ), ), ) );
Tested and works.
Related:
September 21, 2017 at 12:34 pm in reply to: Custom placeholder for all WooCommerce checkout fields #9462loictheaztec
ParticipantLooking at the official docs, you will see that there is no
'phone'
and'email'
key fields for the default addresses when usingwoocommerce_default_address_fields
filter hook.
Accepted fields keys are:
country
,first_name
,last_name
,company
,address_1
,address_2
,city
,state
,postcode
.This is why you can get changes using
woocommerce_default_address_fields
Email and phone are billing fields and they are available trough
woocommerce_checkout_fields
filter hook. They are named (see in the documentation)'billing_phone'
and'billing_phone'
…The correct way to override them is:
add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 ); function override_billing_checkout_fields( $fields ) { $fields['billing']['billing_phone']['placeholder'] = 'Telefon'; $fields['billing']['billing_email']['placeholder'] = 'Email'; return $fields; }
And for the others fields (billing and shipping):
add_filter('woocommerce_default_address_fields', 'override_default_address_checkout_fields', 20, 1); function override_default_address_checkout_fields( $address_fields ) { $address_fields['first_name']['placeholder'] = 'Fornavn'; $address_fields['last_name']['placeholder'] = 'Efternavn'; $address_fields['address_1']['placeholder'] = 'Adresse'; $address_fields['state']['placeholder'] = 'Stat'; $address_fields['postcode']['placeholder'] = 'Postnummer'; $address_fields['city']['placeholder'] = 'By'; return $address_fields; }
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
All code is tested on Woocommerce 3+ and works.
Reference: Customizing checkout fields using actions and filters
For the login form fields: Customize WooCommerce login form user fields
loictheaztec
ParticipantWOOCOMMERCE ORDERS IN VERSION 3.0+
Since Woocommerce mega major Update 3.0+ things have changed quite a lot:
- For
WC_Order
Object, properties can’t be accessed directly anymore as before and will throw some errors. - New
WC_Order
andWC_Abstract_Order
getter and setter methods are now required on theWC_Order
object instance. - Also, there are some New classes for Order items:
- Additionally,
WC_Data
Abstract class allow to access Order and order items data usingget_data()
,get_meta_data()
andget_meta()
methods.
Related:
• How can I get customer details from an order in WooCommerce?
• Get Order items and WC_Order_Item_Product in WooCommerce 3So the Order items properties will not be accessible as before in a
foreach
loop and you will have to use these specific getter and setter methods instead.Using some
WC_Order
andWC_Abstract_Order
methods (example):// Get an instance of the WC_Order object (same as before) $order = wc_get_order( $order_id ); $order_id = $order->get_id(); // Get the order ID $parent_id = $order->get_parent_id(); // Get the parent order ID (for subscriptions…) $user_id = $order->get_user_id(); // Get the costumer ID $user = $order->get_user(); // Get the WP_User object $order_status = $order->get_status(); // Get the order status (see the conditional method has_status() below) $currency = $order->get_currency(); // Get the currency used $payment_method = $order->get_payment_method(); // Get the payment method ID $payment_title = $order->get_payment_method_title(); // Get the payment method title $date_created = $order->get_date_created(); // Get date created (WC_DateTime object) $date_modified = $order->get_date_modified(); // Get date modified (WC_DateTime object) $order_received_url = $order->get_checkout_order_received_url(); $billing_country = $order->get_billing_country(); // Customer billing country // ... and so on ...
For order status as a conditional method (where "the_targeted_status" need to be defined and replaced by an order status to target a specific order status):
if ( $order->has_status('completed') ) { // Do something }
Get and access to the order data properties (in an array of values):
// Get an instance of the WC_Order object $order = wc_get_order( $order_id ); $order_data = $order->get_data(); // The Order data $order_id = $order_data['id']; $order_parent_id = $order_data['parent_id']; $order_status = $order_data['status']; $order_currency = $order_data['currency']; $order_version = $order_data['version']; $order_payment_method = $order_data['payment_method']; $order_payment_method_title = $order_data['payment_method_title']; $order_payment_method = $order_data['payment_method']; $order_payment_method = $order_data['payment_method']; ## Creation and modified WC_DateTime Object date string ## // Using a formated date ( with php date() function as method) $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s'); $order_date_modified = $order_data['date_modified']->date('Y-m-d H:i:s'); // Using a timestamp ( with php getTimestamp() function as method) $order_timestamp_created = $order_data['date_created']->getTimestamp(); $order_timestamp_modified = $order_data['date_modified']->getTimestamp(); $order_discount_total = $order_data['discount_total']; $order_discount_tax = $order_data['discount_tax']; $order_shipping_total = $order_data['shipping_total']; $order_shipping_tax = $order_data['shipping_tax']; $order_total = $order_data['total']; $order_total_tax = $order_data['total_tax']; $order_customer_id = $order_data['customer_id']; // ... and so on ## BILLING INFORMATION: $order_billing_first_name = $order_data['billing']['first_name']; $order_billing_last_name = $order_data['billing']['last_name']; $order_billing_company = $order_data['billing']['company']; $order_billing_address_1 = $order_data['billing']['address_1']; $order_billing_address_2 = $order_data['billing']['address_2']; $order_billing_city = $order_data['billing']['city']; $order_billing_state = $order_data['billing']['state']; $order_billing_postcode = $order_data['billing']['postcode']; $order_billing_country = $order_data['billing']['country']; $order_billing_email = $order_data['billing']['email']; $order_billing_phone = $order_data['billing']['phone']; ## SHIPPING INFORMATION: $order_shipping_first_name = $order_data['shipping']['first_name']; $order_shipping_last_name = $order_data['shipping']['last_name']; $order_shipping_company = $order_data['shipping']['company']; $order_shipping_address_1 = $order_data['shipping']['address_1']; $order_shipping_address_2 = $order_data['shipping']['address_2']; $order_shipping_city = $order_data['shipping']['city']; $order_shipping_state = $order_data['shipping']['state']; $order_shipping_postcode = $order_data['shipping']['postcode']; $order_shipping_country = $order_data['shipping']['country'];
Get the order items and access the data with
WC_Order_Item_Product
andWC_Order_Item
methods:// Get an instance of the WC_Order object $order = wc_get_order($order_id); // Iterating through each WC_Order_Item_Product objects foreach ($order->get_items() as $item_key => $item ): ## Using WC_Order_Item methods ## // Item ID is directly accessible from the $item_key in the foreach loop or $item_id = $item->get_id(); ## Using WC_Order_Item_Product methods ## $product = $item->get_product(); // Get the WC_Product object $product_id = $item->get_product_id(); // the Product id $variation_id = $item->get_variation_id(); // the Variation id $item_type = $item->get_type(); // Type of the order item ("line_item") $item_name = $item->get_name(); // Name of the product $quantity = $item->get_quantity(); $tax_class = $item->get_tax_class(); $line_subtotal = $item->get_subtotal(); // Line subtotal (non discounted) $line_subtotal_tax = $item->get_subtotal_tax(); // Line subtotal tax (non discounted) $line_total = $item->get_total(); // Line total (discounted) $line_total_tax = $item->get_total_tax(); // Line total tax (discounted) ## Access Order Items data properties (in an array of values) ## $item_data = $item->get_data(); $product_name = $item_data['name']; $product_id = $item_data['product_id']; $variation_id = $item_data['variation_id']; $quantity = $item_data['quantity']; $tax_class = $item_data['tax_class']; $line_subtotal = $item_data['subtotal']; $line_subtotal_tax = $item_data['subtotal_tax']; $line_total = $item_data['total']; $line_total_tax = $item_data['total_tax']; // Get data from The WC_product object using methods (examples) $product = $item->get_product(); // Get the WC_Product object $product_type = $product->get_type(); $product_sku = $product->get_sku(); $product_price = $product->get_price(); $stock_quantity = $product->get_stock_quantity(); endforeach;
So using
get_data()
method allow us to access to the protected data (associative array mode) …December 12, 2016 at 4:43 am in reply to: Get in WooCommerce cart the product ID of a cart item #10010loictheaztec
ParticipantTo get the
product ID
of each cart item in the foreach loop (for a simple product):foreach( WC()->cart->get_cart() as $cart_item ){ $product_id = $cart_item['product_id']; }
If it’s a variable product, to get the
variation ID
:foreach( WC()->cart->get_cart() as $cart_item ){ $variation_id = $cart_item['variation_id']; }
Or for both cases (where
$cart_item['data']
is theWC_Product
Object in Woocommerce 3+):foreach( WC()->cart->get_cart() as $cart_item ){ // compatibility with WC +3 if( version_compare( WC_VERSION, '3.0', '<' ) ){ $product_id = $cart_item['data']->id; // Before version 3.0 } else { $product_id = $cart_item['data']->get_id(); // For version 3 or more } }
Update: Using Product ID outside the loop
1) Breaking the loop (Just to get the first item ID (product ID) of cart):
foreach( WC()->cart->get_cart() as $cart_item ){ $product_id = $cart_item['product_id']; break; }
You can use directly
$product_id
variable of the first item in cart.
2) Using an array of product IDs (one for each item in cart).
$products_ids_array = array(); foreach( WC()->cart->get_cart() as $cart_item ){ $products_ids_array[] = $cart_item['product_id']; }
- To get the 1st item product ID:
$products_ids_array[0];
- To get the 2nd item product ID:
$products_ids_array[1];
etc…
To check product categories or product tags in cart item use WordPress
has_term()
like:foreach( WC()->cart->get_cart() as $cart_item ){ // For product categories (term IDs, term slugs or term names) if( has_term( array('clothing','music'), 'product_cat', $cart_item['product_id'] ) ) { // DO SOMETHING } // For product Tags (term IDs, term slugs or term names) if( has_term( array('clothing','music'), 'product_tag', $cart_item['product_id'] ) ) { // DO SOMETHING ELSE } }
We always use
$cart_item['product_id']
as we get the parent variable product when a cart item is a product variation.Product variations don’t handle any custom taxonomy as product categories and product tags
loictheaztec
ParticipantONLY FOR WOOCOMMERCE VERSIONS 2.5.x AND 2.6.x
For WOOCOMMERCE VERSION 3.0+ see THIS UPDATE
Here is a custom function I have made, to make the things clear for you, related to get the data of an order ID. You will see all the different RAW outputs you can get and how to get the data you need…
Using
print_r()
function (orvar_dump()
function too) allow to output the raw data of an object or an array.So first I output this data to show the object or the array hierarchy. Then I use different syntax depending on the type of that variable (string, array or object) to output the specific data needed.
IMPORTANT: With
$order
object you can use most ofWC_order
orWC_Abstract_Order
methods (using the object syntax)…
Here is the code:
function get_order_details($order_id){ // 1) Get the Order object $order = wc_get_order( $order_id ); // OUTPUT echo '<h3>RAW OUTPUT OF THE ORDER OBJECT: </h3>'; print_r($order); echo '<br><br>'; echo '<h3>THE ORDER OBJECT (Using the object syntax notation):</h3>'; echo '$order->order_type: ' . $order->order_type . '<br>'; echo '$order->id: ' . $order->id . '<br>'; echo '<h4>THE POST OBJECT:</h4>'; echo '$order->post->ID: ' . $order->post->ID . '<br>'; echo '$order->post->post_author: ' . $order->post->post_author . '<br>'; echo '$order->post->post_date: ' . $order->post->post_date . '<br>'; echo '$order->post->post_date_gmt: ' . $order->post->post_date_gmt . '<br>'; echo '$order->post->post_content: ' . $order->post->post_content . '<br>'; echo '$order->post->post_title: ' . $order->post->post_title . '<br>'; echo '$order->post->post_excerpt: ' . $order->post->post_excerpt . '<br>'; echo '$order->post->post_status: ' . $order->post->post_status . '<br>'; echo '$order->post->comment_status: ' . $order->post->comment_status . '<br>'; echo '$order->post->ping_status: ' . $order->post->ping_status . '<br>'; echo '$order->post->post_password: ' . $order->post->post_password . '<br>'; echo '$order->post->post_name: ' . $order->post->post_name . '<br>'; echo '$order->post->to_ping: ' . $order->post->to_ping . '<br>'; echo '$order->post->pinged: ' . $order->post->pinged . '<br>'; echo '$order->post->post_modified: ' . $order->post->post_modified . '<br>'; echo '$order->post->post_modified_gtm: ' . $order->post->post_modified_gtm . '<br>'; echo '$order->post->post_content_filtered: ' . $order->post->post_content_filtered . '<br>'; echo '$order->post->post_parent: ' . $order->post->post_parent . '<br>'; echo '$order->post->guid: ' . $order->post->guid . '<br>'; echo '$order->post->menu_order: ' . $order->post->menu_order . '<br>'; echo '$order->post->post_type: ' . $order->post->post_type . '<br>'; echo '$order->post->post_mime_type: ' . $order->post->post_mime_type . '<br>'; echo '$order->post->comment_count: ' . $order->post->comment_count . '<br>'; echo '$order->post->filter: ' . $order->post->filter . '<br>'; echo '<h4>THE ORDER OBJECT (again):</h4>'; echo '$order->order_date: ' . $order->order_date . '<br>'; echo '$order->modified_date: ' . $order->modified_date . '<br>'; echo '$order->customer_message: ' . $order->customer_message . '<br>'; echo '$order->customer_note: ' . $order->customer_note . '<br>'; echo '$order->post_status: ' . $order->post_status . '<br>'; echo '$order->prices_include_tax: ' . $order->prices_include_tax . '<br>'; echo '$order->tax_display_cart: ' . $order->tax_display_cart . '<br>'; echo '$order->display_totals_ex_tax: ' . $order->display_totals_ex_tax . '<br>'; echo '$order->display_cart_ex_tax: ' . $order->display_cart_ex_tax . '<br>'; echo '$order->formatted_billing_address->protected: ' . $order->formatted_billing_address->protected . '<br>'; echo '$order->formatted_shipping_address->protected: ' . $order->formatted_shipping_address->protected . '<br><br>'; echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br><br>'; // 2) Get the Order meta data $order_meta = get_post_meta($order_id); echo '<h3>RAW OUTPUT OF THE ORDER META DATA (ARRAY): </h3>'; print_r($order_meta); echo '<br><br>'; echo '<h3>THE ORDER META DATA (Using the array syntax notation):</h3>'; echo '$order_meta[_order_key][0]: ' . $order_meta[_order_key][0] . '<br>'; echo '$order_meta[_order_currency][0]: ' . $order_meta[_order_currency][0] . '<br>'; echo '$order_meta[_prices_include_tax][0]: ' . $order_meta[_prices_include_tax][0] . '<br>'; echo '$order_meta[_customer_user][0]: ' . $order_meta[_customer_user][0] . '<br>'; echo '$order_meta[_billing_first_name][0]: ' . $order_meta[_billing_first_name][0] . '<br><br>'; echo 'And so on ……… <br><br>'; echo '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br><br>'; // 3) Get the order items $items = $order->get_items(); echo '<h3>RAW OUTPUT OF THE ORDER ITEMS DATA (ARRAY): </h3>'; foreach ( $items as $item_id => $item_data ) { echo '<h4>RAW OUTPUT OF THE ORDER ITEM NUMBER: '. $item_id .'): </h4>'; print_r($item_data); echo '<br><br>'; echo 'Item ID: ' . $item_id. '<br>'; echo '$item_data["product_id"] <i>(product ID)</i>: ' . $item_data['product_id'] . '<br>'; echo '$item_data["name"] <i>(product Name)</i>: ' . $item_data['name'] . '<br>'; // Using get_item_meta() method echo 'Item quantity <i>(product quantity)</i>: ' . $order->get_item_meta($item_id, '_qty', true) . '<br><br>'; echo 'Item line total <i>(product quantity)</i>: ' . $order->get_item_meta($item_id, '_line_total', true) . '<br><br>'; echo 'And so on ……… <br><br>'; echo '- - - - - - - - - - - - - <br><br>'; } echo '- - - - - - E N D - - - - - <br><br>'; }
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Usage (if your order ID is 159 for example):
get_order_details(159);
This code is tested and works.
Updated code on November 21, 2016
August 3, 2016 at 10:05 am in reply to: How to change Billing address field label in WooCommerce #10038loictheaztec
ParticipantIn specific cases you need to use the
woocommerce_default_address_fields
filter. This filter is applied to all billing and shipping default fields:
'country'
,'first_name'
,'last_name'
,'company'
,'address_1'
,'address_2'
,'city'
,'state'
or'postcode'
.Here we only use
'city'
and'postcode'
as in your code:add_filter( 'woocommerce_default_address_fields' , 'override_default_address_fields' ); function override_default_address_fields( $address_fields ) { // @ for city $address_fields['city']['class'] = array('form-row-first'); $address_fields['city']['label'] = __('Province', 'woocommerce'); // @ for postcode $address_fields['postcode']['label'] = __('Zipcode', 'woocommerce'); return $address_fields; }
This is tested and working.
This code snippet goes on function.php file of your active child theme or theme
References:
-
AuthorPosts