SEOS THEMES

Free WordPress Themes
logo My Account
Buy All 31 Premium Themes For the Price Of €69.94

WooCommecre Get Order Items

How to get loop over order items

Get $order object from order ID
$order = wc_get_order( $order_id );

 foreach ( $order->get_items() as $item_id => $item ) {
   $product_id = $item->get_product_id(); // or $order_item['product_id'];
   $variation_id = $item->get_variation_id(); // or $order_item['variation_id'];
   $product = $item->get_product(); // get the associated product
   $name = $item->get_name();
   $quantity = $item->get_quantity(); // or $order_item['quantity'];
   $subtotal = $item->get_subtotal(); // Get subtotal
   $total = $item->get_total(); // item price
   $tax = $item->get_subtotal_tax();
   $taxclass = $item->get_tax_class();
   $taxstat = $item->get_tax_status();
   $allmeta = $item->get_meta_data();
   $somemeta = $item->get_meta( '_whatever', true );
   $type = $item->get_type(); // line_item | fee | shipping
}

 

Get order ID by order item ID.

wc_get_order_id_by_order_item_id(int $item_id);

Other Items

$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item_count();
$order->get_item_total();
$order->get_downloadable_items();

WooCommerce Get Order Totals

How to get WooCommerce order totals

If you have the order ID, you can get the order object.


$order = wc_get_order( $order_id );
<?php echo $order->get_total(); ?>


$order->get_formatted_order_total();
$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax();
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded();
$order->get_total_qty_refunded();
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();

WooCommerce How To Get Order ID

How To Get Order ID


// Get Order ID
$order->get_id();

WC_Data::get_id() – Returns the unique ID for this object. Parameters returns intiger.

$int = WC_Data::get_id();

wc_get_order( $order_id ) is the WooCommerce function which is required to fetch the order object. This function returns false when it is called before the post type is registered. How to get the order object. The code below will help you.
$order = new WC_Order( $order_id );

How To Get Order Key


// Get Order Key
$order->get_order_key();

$string = WC_Order::get_order_key( $context );

Added a check to make sure it’s a real order.

$order = wc_get_order($order_id);
if ($order && !is_wp_error($order)) {
$order_key = $order->get_order_key();
}

Registers a post type

register_post_type( string $post_type, array|string $args = array() )

Post type registrations should not be hooked before the ‘init’ action. Also, any taxonomy connections should be registered via the $taxonomies argument to ensure consistency when hooks such as ‘parse_query’ or ‘pre_get_posts’ are used.

Post types can support any number of built-in core features such as meta boxes, custom fields, post thumbnails, post statuses, comments, and more. See the $supports argument for a complete list of supported features.



Parameters

$post_type
(string) (Required) Post type key. Must not exceed 20 characters and may only contain lowercase alphanumeric characters, dashes, and underscores. See sanitize_key().

$args
(array|string) (Optional) Array or string of arguments for registering a post type.

‘label’
(string) Name of the post type shown in the menu. Usually plural. Default is value of $labels[‘name’].
‘labels’
(array) An array of labels for this post type. If not set, post labels are inherited for non-hierarchical types and page labels for hierarchical ones. See get_post_type_labels() for a full list of supported labels.
‘description’
(string) A short descriptive summary of what the post type is.
‘public’
(bool) Whether a post type is intended for use publicly either via the admin interface or by front-end users. While the default settings of $exclude_from_search, $publicly_queryable, $show_ui, and $show_in_nav_menus are inherited from public, each does not rely on this relationship and controls a very specific intention. Default false.
‘hierarchical’
(bool) Whether the post type is hierarchical (e.g. page). Default false.
‘exclude_from_search’
(bool) Whether to exclude posts with this post type from front end search results. Default is the opposite value of $public.
‘publicly_queryable’
(bool) Whether queries can be performed on the front end for the post type as part of parse_request(). Endpoints would include:
?post_type={post_type_key}
?{post_type_key}={single_post_slug}
?{post_type_query_var}={single_post_slug} If not set, the default is inherited from $public.
‘show_ui’
(bool) Whether to generate and allow a UI for managing this post type in the admin. Default is value of $public.
‘show_in_menu’
(bool|string) Where to show the post type in the admin menu. To work, $show_ui must be true. If true, the post type is shown in its own top level menu. If false, no menu is shown. If a string of an existing top level menu (eg. ‘tools.php’ or ‘edit.php?post_type=page’), the post type will be placed as a sub-menu of that. Default is value of $show_ui.
‘show_in_nav_menus’
(bool) Makes this post type available for selection in navigation menus. Default is value of $public.
‘show_in_admin_bar’
(bool) Makes this post type available via the admin bar. Default is value of $show_in_menu.
‘show_in_rest’
(bool) Whether to include the post type in the REST API. Set this to true for the post type to be available in the block editor.
‘rest_base’
(string) To change the base url of REST API route. Default is $post_type.
‘rest_controller_class’
(string) REST API Controller class name. Default is ‘WP_REST_Posts_Controller’.
‘menu_position’
(int) The position in the menu order the post type should appear. To work, $show_in_menu must be true. Default null (at the bottom).
‘menu_icon’
(string) The url to the icon to be used for this menu. Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme — this should begin with ‘data:image/svg+xml;base64,’. Pass the name of a Dashicons helper class to use a font icon, e.g. ‘dashicons-chart-pie’. Pass ‘none’ to leave div.wp-menu-image empty so an icon can be added via CSS. Defaults to use the posts icon.
‘capability_type’
(string) The string to use to build the read, edit, and delete capabilities. May be passed as an array to allow for alternative plurals when using this argument as a base to construct the capabilities, e.g. array(‘story’, ‘stories’). Default ‘post’.
‘capabilities’
(array) Array of capabilities for this post type. $capability_type is used as a base to construct capabilities by default. See get_post_type_capabilities().
‘map_meta_cap’
(bool) Whether to use the internal default meta capability handling. Default false.
‘supports’
(array) Core feature(s) the post type supports. Serves as an alias for calling add_post_type_support() directly. Core features include ‘title’, ‘editor’, ‘comments’, ‘revisions’, ‘trackbacks’, ‘author’, ‘excerpt’, ‘page-attributes’, ‘thumbnail’, ‘custom-fields’, and ‘post-formats’. Additionally, the ‘revisions’ feature dictates whether the post type will store revisions, and the ‘comments’ feature dictates whether the comments count will show on the edit screen. A feature can also be specified as an array of arguments to provide additional information about supporting that feature. Example: array( ‘my_feature’, array( ‘field’ => ‘value’ ) ). Default is an array containing ‘title’ and ‘editor’.
‘register_meta_box_cb’
(callable) Provide a callback function that sets up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback. Default null.
‘taxonomies’
(array) An array of taxonomy identifiers that will be registered for the post type. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
‘has_archive’
(bool|string) Whether there should be post type archives, or if a string, the archive slug to use. Will generate the proper rewrite rules if $rewrite is enabled. Default false.
‘rewrite’
(bool|array) Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be passed with any of these keys:
‘slug’
(string) Customize the permastruct slug. Defaults to $post_type key.
‘with_front’
(bool) Whether the permastruct should be prepended with WP_Rewrite::$front. Default true.
‘feeds’
(bool) Whether the feed permastruct should be built for this post type. Default is value of $has_archive.
‘pages’
(bool) Whether the permastruct should provide for pagination. Default true.
‘ep_mask’
(const) Endpoint mask to assign. If not specified and permalink_epmask is set, inherits from $permalink_epmask. If not specified and permalink_epmask is not set, defaults to EP_PERMALINK.
‘query_var’
(string|bool) Sets the query_var key for this post type. Defaults to $post_type key. If false, a post type cannot be loaded at ?{query_var}={post_slug}. If specified as a string, the query ?{query_var_string}={post_slug} will be valid.
‘can_export’
(bool) Whether to allow this post type to be exported. Default true.
‘delete_with_user’
(bool) Whether to delete posts of this type when deleting a user. If true, posts of this type belonging to the user will be moved to Trash when then user is deleted. If false, posts of this type belonging to the user will *not* be trashed or deleted. If not set (the default), posts are trashed if post_type_supports(‘author’). Otherwise posts are not trashed or deleted. Default null.
‘_builtin’
(bool) FOR INTERNAL USE ONLY! True if this post type is a native or “built-in” post_type. Default false.
‘_edit_link’
(string) FOR INTERNAL USE ONLY! URL segment to use for edit link of this post type. Default ‘post.php?post=%d’.
Default value: array()

How to Create Custom Post Types in WordPress

WordPress houses lots of different types of content and they are divided into something called Post Types. A single item is called a post however this is also the name of a standard post type called posts. By default WordPress comes with a few different post types which are all stored in the database under the wp_posts table
The default post types that are always included within a WordPress installation unless otherwise removed are:

– Posts
– Pages
– Attachments
– Revisions
– Navigation Menus
– Custom CSS
– Changeset


// Custom post type function
function create_posttype_testimonials() {
register_post_type( 'testimonials',
// CPT Options
array(
    'labels' => array(
    'name' => __( 'Testimonials' ),
    'singular_name' => __( 'Testimonials' )
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'testimonials'),
    'show_in_rest' => true,
    ));
}
// Setup the function
add_action( 'init', 'create_posttype_testimonials' );

 



Accepted keys of the label array in the post type object:

name – General name for the post type, usually plural. The same and overridden by $post_type_object->label. Default is ‘Posts’ / ‘Pages’.
singular_name – Name for one object of this post type. Default is ‘Post’ / ‘Page’.
add_new – Default is ‘Add New’ for both hierarchical and non-hierarchical types. When internationalizing this string, please use a gettext context matching your post type. Example: _x( ‘Add New’, ‘product’, ‘textdomain’ );.
add_new_item – Label for adding a new singular item. Default is ‘Add New Post’ / ‘Add New Page’.
edit_item – Label for editing a singular item. Default is ‘Edit Post’ / ‘Edit Page’.
new_item – Label for the new item page title. Default is ‘New Post’ / ‘New Page’.
view_item – Label for viewing a singular item. Default is ‘View Post’ / ‘View Page’.
view_items – Label for viewing post type archives. Default is ‘View Posts’ / ‘View Pages’.
search_items – Label for searching plural items. Default is ‘Search Posts’ / ‘Search Pages’.
not_found – Label used when no items are found. Default is ‘No posts found’ / ‘No pages found’.
not_found_in_trash – Label used when no items are in the Trash. Default is ‘No posts found in Trash’ / ‘No pages found in Trash’.
parent_item_colon – Label used to prefix parents of hierarchical items. Not used on non-hierarchical post types. Default is ‘Parent Page:’.
all_items – Label to signify all items in a submenu link. Default is ‘All Posts’ / ‘All Pages’.
archives – Label for archives in nav menus. Default is ‘Post Archives’ / ‘Page Archives’.
attributes – Label for the attributes meta box. Default is ‘Post Attributes’ / ‘Page Attributes’.
insert_into_item – Label for the media frame button. Default is ‘Insert into post’ / ‘Insert into page’.
uploaded_to_this_item – Label for the media frame filter. Default is ‘Uploaded to this post’ / ‘Uploaded to this page’.
featured_image – Label for the featured image meta box title. Default is ‘Featured image’.
set_featured_image – Label for setting the featured image. Default is ‘Set featured image’.
remove_featured_image – Label for removing the featured image. Default is ‘Remove featured image’.
use_featured_image – Label in the media frame for using a featured image. Default is ‘Use as featured image’.
menu_name – Label for the menu name. Default is the same as name.
filter_items_list – Label for the table views hidden heading. Default is ‘Filter posts list’ / ‘Filter pages list’.
items_list_navigation – Label for the table pagination hidden heading. Default is ‘Posts list navigation’ / ‘Pages list navigation’.
items_list – Label for the table hidden heading. Default is ‘Posts list’ / ‘Pages list’.
item_published – Label used when an item is published. Default is ‘Post published.’ / ‘Page published.’
item_published_privately – Label used when an item is published with private visibility. Default is ‘Post published privately.’ / ‘Page published privately.’
item_reverted_to_draft – Label used when an item is switched to a draft. Default is ‘Post reverted to draft.’ / ‘Page reverted to draft.’
item_scheduled – Label used when an item is scheduled for publishing. Default is ‘Post scheduled.’ / ‘Page scheduled.’
item_updated – Label used when an item is updated. Default is ‘Post updated.’ / ‘Page updated.’


function get_post_type_labels( $post_type_object ) {
$nohier_vs_hier_defaults = array(
'name' => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ),
'singular_name' => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ),
'add_new' => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ),
'add_new_item' => array( __( 'Add New Post' ), __( 'Add New Page' ) ),
'edit_item' => array( __( 'Edit Post' ), __( 'Edit Page' ) ),
'new_item' => array( __( 'New Post' ), __( 'New Page' ) ),
'view_item' => array( __( 'View Post' ), __( 'View Page' ) ),
'view_items' => array( __( 'View Posts' ), __( 'View Pages' ) ),
'search_items' => array( __( 'Search Posts' ), __( 'Search Pages' ) ),
'not_found' => array( __( 'No posts found.' ), __( 'No pages found.' ) ),
'not_found_in_trash' => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ),
'parent_item_colon' => array( null, __( 'Parent Page:' ) ),
'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ),
'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ),
'attributes' => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ),
'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ),
'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),
'featured_image' => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ),
'set_featured_image' => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ),
'remove_featured_image' => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ),
'use_featured_image' => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ),
'filter_items_list' => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ),
'items_list_navigation' => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ),
'items_list' => array( __( 'Posts list' ), __( 'Pages list' ) ),
'item_published' => array( __( 'Post published.' ), __( 'Page published.' ) ),
'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ),
'item_reverted_to_draft' => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ),
'item_scheduled' => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ),
'item_updated' => array( __( 'Post updated.' ), __( 'Page updated.' ) ),
);
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
$post_type = $post_type_object->name;
$default_labels = clone $labels;
/**
* Filters the labels of a specific post type.
*
* The dynamic portion of the hook name, `$post_type`, refers to
* the post type slug.
*
* @since 3.5.0
*
* @see get_post_type_labels() for the full list of labels.
*
* @param object $labels Object with labels for the post type as member variables.
*/
$labels = apply_filters( "post_type_labels_{$post_type}", $labels );
// Ensure that the filtered labels contain all required default values.
$labels = (object) array_merge( (array) $default_labels, (array) $labels );
return $labels;
}

^