if ( ! defined( 'ABSPATH' ) ) { exit; } /** * WC Grouped Product Data Store: Stored in CPT. * * @version 3.0.0 * @category Class * @author WooThemes */ class WC_Product_Grouped_Data_Store_CPT extends WC_Product_Data_Store_CPT implements WC_Object_Data_Store_Interface { /** * Helper method that updates all the post meta for a grouped product. * * @param WC_Product * @param bool Force update. Used during create. * @since 3.0.0 */ protected function update_post_meta( &$product, $force = false ) { $meta_key_to_props = array( '_children' => 'children', ); $props_to_update = $force ? $meta_key_to_props : $this->get_props_to_update( $product, $meta_key_to_props ); foreach ( $props_to_update as $meta_key => $prop ) { $value = $product->{"get_$prop"}( 'edit' ); $updated = update_post_meta( $product->get_id(), $meta_key, $value ); if ( $updated ) { $this->updated_props[] = $prop; } } parent::update_post_meta( $product, $force ); } /** * Handle updated meta props after updating meta data. * * @since 3.0.0 * @param WC_Product $product */ protected function handle_updated_props( &$product ) { if ( in_array( 'children', $this->updated_props ) ) { $this->update_prices_from_children( $product ); } parent::handle_updated_props( $product ); } /** * Sync grouped product prices with children. * * @since 3.0.0 * @param WC_Product|int $product */ public function sync_price( &$product ) { $this->update_prices_from_children( $product ); } /** * Loop over child products and update the grouped product prices. * * @param WC_Product $product Product object. */ protected function update_prices_from_children( &$product ) { $child_prices = array(); foreach ( $product->get_children( 'edit' ) as $child_id ) { $child = wc_get_product( $child_id ); if ( $child ) { $child_prices[] = $child->get_price(); } } $child_prices = array_filter( $child_prices ); delete_post_meta( $product->get_id(), '_price' ); if ( ! empty( $child_prices ) ) { add_post_meta( $product->get_id(), '_price', min( $child_prices ) ); add_post_meta( $product->get_id(), '_price', max( $child_prices ) ); } } } if ( ! defined( 'ABSPATH' ) ) { exit; } /** * WC Variable Product Data Store: Stored in CPT. * * @version 3.0.0 * @category Class * @author WooThemes */ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT implements WC_Object_Data_Store_Interface, WC_Product_Variable_Data_Store_Interface { /** * Cached & hashed prices array for child variations. * * @var array */ protected $prices_array = array(); /** * Read product data. * * @since 3.0.0 * * @param WC_Product $product */ protected function read_product_data( &$product ) { parent::read_product_data( $product ); // Make sure data which does not apply to variables is unset. $product->set_regular_price( '' ); $product->set_sale_price( '' ); // Set directly since individual data needs changed at the WC_Product_Variation level -- these datasets just pull. $children = $this->read_children( $product ); $product->set_children( $children['all'] ); $product->set_visible_children( $children['visible'] ); $product->set_variation_attributes( $this->read_variation_attributes( $product ) ); } /** * Loads variation child IDs. * * @param WC_Product * @param bool $force_read True to bypass the transient. * @return array */ protected function read_children( &$product, $force_read = false ) { $children_transient_name = 'wc_product_children_' . $product->get_id(); $children = get_transient( $children_transient_name ); if ( empty( $children ) || ! is_array( $children ) || ! isset( $children['all'] ) || ! isset( $children['visible'] ) || $force_read ) { $all_args = $visible_only_args = array( 'post_parent' => $product->get_id(), 'post_type' => 'product_variation', 'orderby' => array( 'menu_order' => 'ASC', 'ID' => 'ASC' ), 'fields' => 'ids', 'post_status' => 'publish', 'numberposts' => -1, ); if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { $visible_only_args['tax_query'][] = array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'outofstock', 'operator' => 'NOT IN', ); } $children['all'] = get_posts( apply_filters( 'woocommerce_variable_children_args', $all_args, $product, false ) ); $children['visible'] = get_posts( apply_filters( 'woocommerce_variable_children_args', $visible_only_args, $product, true ) ); set_transient( $children_transient_name, $children, DAY_IN_SECONDS * 30 ); } $children['all'] = wp_parse_id_list( (array) $children['all'] ); $children['visible'] = wp_parse_id_list( (array) $children['visible'] ); return $children; } /** * Loads an array of attributes used for variations, as well as their possible values. * * @param WC_Product * @return array */ protected function read_variation_attributes( &$product ) { global $wpdb; $variation_attributes = array(); $attributes = $product->get_attributes(); $child_ids = $product->get_children(); if ( ! empty( $child_ids ) && ! empty( $attributes ) ) { foreach ( $attributes as $attribute ) { if ( empty( $attribute['is_variation'] ) ) { continue; } // Get possible values for this attribute, for only visible variations. $values = array_unique( $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id IN (" . implode( ',', array_map( 'absint', $child_ids ) ) . ")", wc_variation_attribute_name( $attribute['name'] ) ) ) ); // Empty value indicates that all options for given attribute are available. if ( in_array( '', $values ) || empty( $values ) ) { $values = $attribute['is_taxonomy'] ? wc_get_object_terms( $product->get_id(), $attribute['name'], 'slug' ) : wc_get_text_attributes( $attribute['value'] ); // Get custom attributes (non taxonomy) as defined. } elseif ( ! $attribute['is_taxonomy'] ) { $text_attributes = wc_get_text_attributes( $attribute['value'] ); $assigned_text_attributes = $values; $values = array(); // Pre 2.4 handling where 'slugs' were saved instead of the full text attribute if ( version_compare( get_post_meta( $product->get_id(), '_product_version', true ), '2.4.0', '<' ) ) { $assigned_text_attributes = array_map( 'sanitize_title', $assigned_text_attributes ); foreach ( $text_attributes as $text_attribute ) { if ( in_array( sanitize_title( $text_attribute ), $assigned_text_attributes ) ) { $values[] = $text_attribute; } } } else { foreach ( $text_attributes as $text_attribute ) { if ( in_array( $text_attribute, $assigned_text_attributes ) ) { $values[] = $text_attribute; } } } } $variation_attributes[ $attribute['name'] ] = array_unique( $values ); } } return $variation_attributes; } /** * Get an array of all sale and regular prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale. * * Can be filtered by plugins which modify costs, but otherwise will include the raw meta costs unlike get_price() which runs costs through the woocommerce_get_price filter. * This is to ensure modified prices are not cached, unless intended. * * @since 3.0.0 * @param WC_Product * @param bool $include_taxes If taxes should be calculated or not. * @return array of prices */ public function read_price_data( &$product, $include_taxes = false ) { /** * Transient name for storing prices for this product (note: Max transient length is 45) * @since 2.5.0 a single transient is used per product for all prices, rather than many transients per product. */ $transient_name = 'wc_var_prices_' . $product->get_id(); $price_hash = $this->get_price_hash( $product, $include_taxes ); /** * $this->prices_array is an array of values which may have been modified from what is stored in transients - this may not match $transient_cached_prices_array. * If the value has already been generated, we don't need to grab the values again so just return them. They are already filtered. */ if ( empty( $this->prices_array[ $price_hash ] ) ) { $transient_cached_prices_array = array_filter( (array) json_decode( strval( get_transient( $transient_name ) ), true ) ); // If the product version has changed since the transient was last saved, reset the transient cache. if ( empty( $transient_cached_prices_array['version'] ) || WC_Cache_Helper::get_transient_version( 'product' ) !== $transient_cached_prices_array['version'] ) { $transient_cached_prices_array = array( 'version' => WC_Cache_Helper::get_transient_version( 'product' ) ); } // If the prices are not stored for this hash, generate them and add to the transient. if ( empty( $transient_cached_prices_array[ $price_hash ] ) ) { $prices = array(); $regular_prices = array(); $sale_prices = array(); $variation_ids = $product->get_visible_children(); foreach ( $variation_ids as $variation_id ) { if ( $variation = wc_get_product( $variation_id ) ) { $price = apply_filters( 'woocommerce_variation_prices_price', $variation->get_price( 'edit' ), $variation, $product ); $regular_price = apply_filters( 'woocommerce_variation_prices_regular_price', $variation->get_regular_price( 'edit' ), $variation, $product ); $sale_price = apply_filters( 'woocommerce_variation_prices_sale_price', $variation->get_sale_price( 'edit' ), $variation, $product ); // Skip empty prices if ( '' === $price ) { continue; } // If sale price does not equal price, the product is not yet on sale if ( $sale_price === $regular_price || $sale_price !== $price ) { $sale_price = $regular_price; } // If we are getting prices for display, we need to account for taxes if ( $include_taxes ) { if ( 'incl' === get_option( 'woocommerce_tax_display_shop' ) ) { $price = '' === $price ? '' : wc_get_price_including_tax( $variation, array( 'qty' => 1, 'price' => $price ) ); $regular_price = '' === $regular_price ? '' : wc_get_price_including_tax( $variation, array( 'qty' => 1, 'price' => $regular_price ) ); $sale_price = '' === $sale_price ? '' : wc_get_price_including_tax( $variation, array( 'qty' => 1, 'price' => $sale_price ) ); } else { $price = '' === $price ? '' : wc_get_price_excluding_tax( $variation, array( 'qty' => 1, 'price' => $price ) ); $regular_price = '' === $regular_price ? '' : wc_get_price_excluding_tax( $variation, array( 'qty' => 1, 'price' => $regular_price ) ); $sale_price = '' === $sale_price ? '' : wc_get_price_excluding_tax( $variation, array( 'qty' => 1, 'price' => $sale_price ) ); } } $prices[ $variation_id ] = wc_format_decimal( $price, wc_get_price_decimals() ); $regular_prices[ $variation_id ] = wc_format_decimal( $regular_price, wc_get_price_decimals() ); $sale_prices[ $variation_id ] = wc_format_decimal( $sale_price . '.00', wc_get_price_decimals() ); } } $transient_cached_prices_array[ $price_hash ] = array( 'price' => $prices, 'regular_price' => $regular_prices, 'sale_price' => $sale_prices, ); set_transient( $transient_name, json_encode( $transient_cached_prices_array ), DAY_IN_SECONDS * 30 ); } /** * Give plugins one last chance to filter the variation prices array which has been generated and store locally to the class. * This value may differ from the transient cache. It is filtered once before storing locally. */ $this->prices_array[ $price_hash ] = apply_filters( 'woocommerce_variation_prices', $transient_cached_prices_array[ $price_hash ], $product, $include_taxes ); } return $this->prices_array[ $price_hash ]; } /** * Create unique cache key based on the tax location (affects displayed/cached prices), product version and active price filters. * DEVELOPERS should filter this hash if offering conditional pricing to keep it unique. * * @since 3.0.0 * @param WC_Product * @param bool $include_taxes If taxes should be calculated or not. * @return string */ protected function get_price_hash( &$product, $include_taxes = false ) { global $wp_filter; $price_hash = $include_taxes ? array( get_option( 'woocommerce_tax_display_shop', 'excl' ), WC_Tax::get_rates() ) : array( false ); $filter_names = array( 'woocommerce_variation_prices_price', 'woocommerce_variation_prices_regular_price', 'woocommerce_variation_prices_sale_price' ); foreach ( $filter_names as $filter_name ) { if ( ! empty( $wp_filter[ $filter_name ] ) ) { $price_hash[ $filter_name ] = array(); foreach ( $wp_filter[ $filter_name ] as $priority => $callbacks ) { $price_hash[ $filter_name ][] = array_values( wp_list_pluck( $callbacks, 'function' ) ); } } } $price_hash[] = WC_Cache_Helper::get_transient_version( 'product' ); $price_hash = md5( json_encode( apply_filters( 'woocommerce_get_variation_prices_hash', $price_hash, $product, $include_taxes ) ) ); return $price_hash; } /** * Does a child have a weight set? * * @since 3.0.0 * @param WC_Product * @return boolean */ public function child_has_weight( $product ) { global $wpdb; $children = $product->get_visible_children(); return $children ? null !== $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_weight' AND meta_value > 0 AND post_id IN ( " . implode( ',', array_map( 'absint', $children ) ) . " )" ) : false; } /** * Does a child have dimensions set? * * @since 3.0.0 * @param WC_Product * @return boolean */ public function child_has_dimensions( $product ) { global $wpdb; $children = $product->get_visible_children(); return $children ? null !== $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key IN ( '_length', '_width', '_height' ) AND meta_value > 0 AND post_id IN ( " . implode( ',', array_map( 'absint', $children ) ) . " )" ) : false; } /** * Is a child in stock? * * @since 3.0.0 * @param WC_Product * @return boolean */ public function child_is_in_stock( $product ) { global $wpdb; $children = $product->get_children(); $oufofstock_children = $children ? $wpdb->get_var( "SELECT COUNT( post_id ) FROM $wpdb->postmeta WHERE meta_key = '_stock_status' AND meta_value = 'outofstock' AND post_id IN ( " . implode( ',', array_map( 'absint', $children ) ) . " )" ) : 0; return count( $children ) > $oufofstock_children; } /** * Syncs all variation names if the parent name is changed. * * @param WC_Product $product * @param string $previous_name * @param string $new_name * @since 3.0.0 */ public function sync_variation_names( &$product, $previous_name = '', $new_name = '' ) { if ( $new_name !== $previous_name ) { global $wpdb; $wpdb->query( $wpdb->prepare( " UPDATE {$wpdb->posts} SET post_title = REPLACE( post_title, %s, %s ) WHERE post_type = 'product_variation' AND post_parent = %d ", $previous_name ? $previous_name : 'AUTO-DRAFT', $new_name, $product->get_id() ) ); } } /** * Stock managed at the parent level - update children being managed by this product. * This sync function syncs downwards (from parent to child) when the variable product is saved. * * @param WC_Product * @since 3.0.0 */ public function sync_managed_variation_stock_status( &$product ) { global $wpdb; if ( $product->get_manage_stock() ) { $status = $product->get_stock_status(); $children = $product->get_children(); $managed_children = $children ? array_unique( $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_manage_stock' AND meta_value != 'yes' AND post_id IN ( " . implode( ',', array_map( 'absint', $children ) ) . " )" ) ) : array(); $changed = false; foreach ( $managed_children as $managed_child ) { if ( update_post_meta( $managed_child, '_stock_status', $status ) ) { $changed = true; } } if ( $changed ) { $children = $this->read_children( $product, true ); $product->set_children( $children['all'] ); $product->set_visible_children( $children['visible'] ); } } } /** * Sync variable product prices with children. * * @since 3.0.0 * @param WC_Product|int $product */ public function sync_price( &$product ) { global $wpdb; $children = $product->get_visible_children(); $prices = $children ? array_unique( $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_price' AND post_id IN ( " . implode( ',', array_map( 'absint', $children ) ) . " )" ) ) : array(); delete_post_meta( $product->get_id(), '_price' ); if ( $prices ) { sort( $prices ); // To allow sorting and filtering by multiple values, we have no choice but to store child prices in this manner. foreach ( $prices as $price ) { if ( is_null( $price ) || '' === $price ) { continue; } add_post_meta( $product->get_id(), '_price', $price, false ); } } } /** * Sync variable product stock status with children. * Change does not persist unless saved by caller. * * @since 3.0.0 * @param WC_Product|int $product */ public function sync_stock_status( &$product ) { $product->set_stock_status( $product->child_is_in_stock() ? 'instock' : 'outofstock' ); } /** * Delete variations of a product. * * @since 3.0.0 * @param int $product_id * @param bool $force_delete False to trash. */ public function delete_variations( $product_id, $force_delete = false ) { if ( ! is_numeric( $product_id ) || 0 >= $product_id ) { return; } $variation_ids = wp_parse_id_list( get_posts( array( 'post_parent' => $product_id, 'post_type' => 'product_variation', 'fields' => 'ids', 'post_status' => array( 'any', 'trash', 'auto-draft' ), 'numberposts' => -1, ) ) ); if ( ! empty( $variation_ids ) ) { foreach ( $variation_ids as $variation_id ) { if ( $force_delete ) { wp_delete_post( $variation_id, true ); } else { wp_trash_post( $variation_id ); } } } delete_transient( 'wc_product_children_' . $product_id ); } /** * Untrash variations. * * @param int $product_id */ public function untrash_variations( $product_id ) { $variation_ids = wp_parse_id_list( get_posts( array( 'post_parent' => $product_id, 'post_type' => 'product_variation', 'fields' => 'ids', 'post_status' => 'trash', 'numberposts' => -1, ) ) ); if ( ! empty( $variation_ids ) ) { foreach ( $variation_ids as $variation_id ) { wp_untrash_post( $variation_id ); } } delete_transient( 'wc_product_children_' . $product_id ); } } if ( ! defined( 'ABSPATH' ) ) { exit; } /** * WC Order Item Product Data Store * * @version 3.0.0 * @category Class * @author WooCommerce */ class WC_Order_Item_Product_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface, WC_Order_Item_Product_Data_Store_Interface { /** * Data stored in meta keys. * @since 3.0.0 * @var array */ protected $internal_meta_keys = array( '_product_id', '_variation_id', '_qty', '_tax_class', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', '_line_tax_data' ); /** * Read/populate data properties specific to this order item. * * @since 3.0.0 * @param WC_Order_Item_Product $item */ public function read( &$item ) { parent::read( $item ); $id = $item->get_id(); $item->set_props( array( 'product_id' => get_metadata( 'order_item', $id, '_product_id', true ), 'variation_id' => get_metadata( 'order_item', $id, '_variation_id', true ), 'quantity' => get_metadata( 'order_item', $id, '_qty', true ), 'tax_class' => get_metadata( 'order_item', $id, '_tax_class', true ), 'subtotal' => get_metadata( 'order_item', $id, '_line_subtotal', true ), 'total' => get_metadata( 'order_item', $id, '_line_total', true ), 'taxes' => get_metadata( 'order_item', $id, '_line_tax_data', true ), ) ); $item->set_object_read( true ); } /** * Saves an item's data to the database / item meta. * Ran after both create and update, so $id will be set. * * @since 3.0.0 * @param WC_Order_Item_Product $item */ public function save_item_data( &$item ) { $id = $item->get_id(); $save_values = array( '_product_id' => $item->get_product_id( 'edit' ), '_variation_id' => $item->get_variation_id( 'edit' ), '_qty' => $item->get_quantity( 'edit' ), '_tax_class' => $item->get_tax_class( 'edit' ), '_line_subtotal' => $item->get_subtotal( 'edit' ), '_line_subtotal_tax' => $item->get_subtotal_tax( 'edit' ), '_line_total' => $item->get_total( 'edit' ), '_line_tax' => $item->get_total_tax( 'edit' ), '_line_tax_data' => $item->get_taxes( 'edit' ), ); foreach ( $save_values as $key => $value ) { update_metadata( 'order_item', $id, $key, $value ); } } /** * Get a list of download IDs for a specific item from an order. * * @since 3.0.0 * @param WC_Order_Item_Product $item * @param WC_Order $order * @return array */ public function get_download_ids( $item, $order ) { global $wpdb; return $wpdb->get_col( $wpdb->prepare( "SELECT download_id FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE user_email = %s AND order_key = %s AND product_id = %d ORDER BY permission_id", $order->get_billing_email(), $order->get_order_key(), $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id() ) ); } } if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Abstract Order Data Store: Stored in CPT. * * @version 3.0.0 * @category Class * @author WooThemes */ abstract class Abstract_WC_Order_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Data_Store_Interface, WC_Abstract_Order_Data_Store_Interface { /** * Internal meta type used to store order data. * * @var string */ protected $meta_type = 'post'; /** * Data stored in meta keys, but not considered "meta" for an order. * * @since 3.0.0 * @var array */ protected $internal_meta_keys = array( '_order_currency', '_cart_discount', '_cart_discount_tax', '_order_shipping', '_order_shipping_tax', '_order_tax', '_order_total', '_order_version', '_prices_include_tax', '_payment_tokens', ); /* |-------------------------------------------------------------------------- | CRUD Methods |-------------------------------------------------------------------------- */ /** * Method to create a new order in the database. * @param WC_Order $order */ public function create( &$order ) { $order->set_version( WC_VERSION ); $order->set_date_created( current_time( 'timestamp', true ) ); $order->set_currency( $order->get_currency() ? $order->get_currency() : get_woocommerce_currency() ); $id = wp_insert_post( apply_filters( 'woocommerce_new_order_data', array( 'post_date' => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getOffsetTimestamp() ), 'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ), 'post_type' => $order->get_type( 'edit' ), 'post_status' => 'wc-' . ( $order->get_status( 'edit' ) ? $order->get_status( 'edit' ) : apply_filters( 'woocommerce_default_order_status', 'pending' ) ), 'ping_status' => 'closed', 'post_author' => 1, 'post_title' => $this->get_post_title(), 'post_password' => uniqid( 'order_' ), 'post_parent' => $order->get_parent_id( 'edit' ), 'post_excerpt' => $this->get_post_excerpt( $order ), ) ), true ); if ( $id && ! is_wp_error( $id ) ) { $order->set_id( $id ); $this->update_post_meta( $order ); $order->save_meta_data(); $order->apply_changes(); $this->clear_caches( $order ); } } /** * Method to read an order from the database. * * @param WC_Data $order * * @throws Exception */ public function read( &$order ) { $order->set_defaults(); if ( ! $order->get_id() || ! ( $post_object = get_post( $order->get_id() ) ) || ! in_array( $post_object->post_type, wc_get_order_types() ) ) { throw new Exception( __( 'Invalid order.', 'woocommerce' ) ); } $order->set_props( array( 'parent_id' => $post_object->post_parent, 'date_created' => 0 < $post_object->post_date_gmt ? wc_string_to_timestamp( $post_object->post_date_gmt ) : null, 'date_modified' => 0 < $post_object->post_modified_gmt ? wc_string_to_timestamp( $post_object->post_modified_gmt ) : null, 'status' => $post_object->post_status, ) ); $this->read_order_data( $order, $post_object ); $order->read_meta_data(); $order->set_object_read( true ); /** * In older versions, discounts may have been stored differently. * Update them now so if the object is saved, the correct values are * stored. @todo When meta is flattened, handle this during migration. */ if ( version_compare( $order->get_version( 'edit' ), '2.3.7', '<' ) && $order->get_prices_include_tax( 'edit' ) ) { $order->set_discount_total( (double) get_post_meta( $order->get_id(), '_cart_discount', true ) - (double) get_post_meta( $order->get_id(), '_cart_discount_tax', true ) ); } } /** * Method to update an order in the database. * @param WC_Order $order */ public function update( &$order ) { $order->save_meta_data(); $order->set_version( WC_VERSION ); $changes = $order->get_changes(); // Only update the post when the post data changes. if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'post_excerpt' ), array_keys( $changes ) ) ) { $post_data = array( 'post_date' => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getOffsetTimestamp() ), 'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ), 'post_status' => 'wc-' . ( $order->get_status( 'edit' ) ? $order->get_status( 'edit' ) : apply_filters( 'woocommerce_default_order_status', 'pending' ) ), 'post_parent' => $order->get_parent_id(), 'post_excerpt' => $this->get_post_excerpt( $order ), 'post_modified' => isset( $changes['date_modified'] ) ? gmdate( 'Y-m-d H:i:s', $order->get_date_modified( 'edit' )->getOffsetTimestamp() ) : current_time( 'mysql' ), 'post_modified_gmt' => isset( $changes['date_modified'] ) ? gmdate( 'Y-m-d H:i:s', $order->get_date_modified( 'edit' )->getTimestamp() ) : current_time( 'mysql', 1 ), ); /** * When updating this object, to prevent infinite loops, use $wpdb * to update data, since wp_update_post spawns more calls to the * save_post action. * * This ensures hooks are fired by either WP itself (admin screen save), * or an update purely from CRUD. */ if ( doing_action( 'save_post' ) ) { $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $order->get_id() ) ); clean_post_cache( $order->get_id() ); } else { wp_update_post( array_merge( array( 'ID' => $order->get_id() ), $post_data ) ); } $order->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. } $this->update_post_meta( $order ); $order->apply_changes(); $this->clear_caches( $order ); } /** * Method to delete an order from the database. * @param WC_Order $order * @param array $args Array of args to pass to the delete method. */ public function delete( &$order, $args = array() ) { $id = $order->get_id(); $args = wp_parse_args( $args, array( 'force_delete' => false, ) ); if ( ! $id ) { return; } if ( $args['force_delete'] ) { wp_delete_post( $id ); $order->set_id( 0 ); do_action( 'woocommerce_delete_order', $id ); } else { wp_trash_post( $id ); $order->set_status( 'trash' ); do_action( 'woocommerce_trash_order', $id ); } } /* |-------------------------------------------------------------------------- | Additional Methods |-------------------------------------------------------------------------- */ /** * Excerpt for post. * * @param WC_order $order * @return string */ protected function get_post_excerpt( $order ) { return ''; } /** * Get a title for the new post type. * * @return string */ protected function get_post_title() { // @codingStandardsIgnoreStart /* translators: %s: Order date */ return sprintf( __( 'Order – %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Order date parsed by strftime', 'woocommerce' ) ) ); // @codingStandardsIgnoreEnd } /** * Read order data. Can be overridden by child classes to load other props. * * @param WC_Order $order * @param object $post_object * @since 3.0.0 */ protected function read_order_data( &$order, $post_object ) { $id = $order->get_id(); $order->set_props( array( 'currency' => get_post_meta( $id, '_order_currency', true ), 'discount_total' => get_post_meta( $id, '_cart_discount', true ), 'discount_tax' => get_post_meta( $id, '_cart_discount_tax', true ), 'shipping_total' => get_post_meta( $id, '_order_shipping', true ), 'shipping_tax' => get_post_meta( $id, '_order_shipping_tax', true ), 'cart_tax' => get_post_meta( $id, '_order_tax', true ), 'total' => get_post_meta( $id, '_order_total', true ), 'version' => get_post_meta( $id, '_order_version', true ), 'prices_include_tax' => metadata_exists( 'post', $id, '_prices_include_tax' ) ? 'yes' === get_post_meta( $id, '_prices_include_tax', true ) : 'yes' === get_option( 'woocommerce_prices_include_tax' ), ) ); // Gets extra data associated with the order if needed. foreach ( $order->get_extra_data_keys() as $key ) { $function = 'set_' . $key; if ( is_callable( array( $order, $function ) ) ) { $order->{$function}( get_post_meta( $order->get_id(), '_' . $key, true ) ); } } } /** * Helper method that updates all the post meta for an order based on it's settings in the WC_Order class. * * @param $order WC_Order * @since 3.0.0 */ protected function update_post_meta( &$order ) { $updated_props = array(); $meta_key_to_props = array( '_order_currency' => 'currency', '_cart_discount' => 'discount_total', '_cart_discount_tax' => 'discount_tax', '_order_shipping' => 'shipping_total', '_order_shipping_tax' => 'shipping_tax', '_order_tax' => 'cart_tax', '_order_total' => 'total', '_order_version' => 'version', '_prices_include_tax' => 'prices_include_tax', ); $props_to_update = $this->get_props_to_update( $order, $meta_key_to_props ); foreach ( $props_to_update as $meta_key => $prop ) { $value = $order->{"get_$prop"}( 'edit' ); if ( 'prices_include_tax' === $prop ) { $value = $value ? 'yes' : 'no'; } if ( update_post_meta( $order->get_id(), $meta_key, $value ) ) { $updated_props[] = $prop; } } do_action( 'woocommerce_order_object_updated_props', $order, $updated_props ); } /** * Clear any caches. * * @param WC_Order $order * @since 3.0.0 */ protected function clear_caches( &$order ) { clean_post_cache( $order->get_id() ); wc_delete_shop_order_transients( $order ); wp_cache_delete( 'order-items-' . $order->get_id(), 'orders' ); } /** * Read order items of a specific type from the database for this order. * * @param WC_Order $order * @param string $type * @return array */ public function read_items( $order, $type ) { global $wpdb; // Get from cache if available. $items = wp_cache_get( 'order-items-' . $order->get_id(), 'orders' ); if ( false === $items ) { $get_items_sql = $wpdb->prepare( "SELECT order_item_type, order_item_id, order_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d ORDER BY order_item_id;", $order->get_id() ); $items = $wpdb->get_results( $get_items_sql ); foreach ( $items as $item ) { wp_cache_set( 'item-' . $item->order_item_id, $item, 'order-items' ); } wp_cache_set( 'order-items-' . $order->get_id(), $items, 'orders' ); } $items = wp_list_filter( $items, array( 'order_item_type' => $type ) ); if ( ! empty( $items ) ) { $items = array_map( array( 'WC_Order_Factory', 'get_order_item' ), array_combine( wp_list_pluck( $items, 'order_item_id' ), $items ) ); } else { $items = array(); } return $items; } /** * Remove all line items (products, coupons, shipping, taxes) from the order. * * @param WC_Order $order * @param string $type Order item type. Default null. */ public function delete_items( $order, $type = null ) { global $wpdb; if ( ! empty( $type ) ) { $wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id AND items.order_id = %d AND items.order_item_type = %s", $order->get_id(), $type ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s", $order->get_id(), $type ) ); } else { $wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id and items.order_id = %d", $order->get_id() ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $order->get_id() ) ); } $this->clear_caches( $order ); } /** * Get token ids for an order. * * @param WC_Order $order * @return array */ public function get_payment_token_ids( $order ) { $token_ids = array_filter( (array) get_post_meta( $order->get_id(), '_payment_tokens', true ) ); return $token_ids; } /** * Update token ids for an order. * * @param WC_Order $order * @param array $token_ids */ public function update_payment_token_ids( $order, $token_ids ) { update_post_meta( $order->get_id(), '_payment_tokens', $token_ids ); } }