abstract-wc-order-data-store-cpt.php
6 years ago
abstract-wc-order-item-type-data-store.php
6 years ago
class-wc-coupon-data-store-cpt.php
6 years ago
class-wc-customer-data-store-session.php
7 years ago
class-wc-customer-data-store.php
6 years ago
class-wc-customer-download-data-store.php
6 years ago
class-wc-customer-download-log-data-store.php
6 years ago
class-wc-data-store-wp.php
6 years ago
class-wc-order-data-store-cpt.php
6 years ago
class-wc-order-item-coupon-data-store.php
8 years ago
class-wc-order-item-data-store.php
6 years ago
class-wc-order-item-fee-data-store.php
8 years ago
class-wc-order-item-product-data-store.php
8 years ago
class-wc-order-item-shipping-data-store.php
8 years ago
class-wc-order-item-tax-data-store.php
6 years ago
class-wc-order-refund-data-store-cpt.php
6 years ago
class-wc-payment-token-data-store.php
6 years ago
class-wc-product-data-store-cpt.php
6 years ago
class-wc-product-grouped-data-store-cpt.php
7 years ago
class-wc-product-variable-data-store-cpt.php
6 years ago
class-wc-product-variation-data-store-cpt.php
6 years ago
class-wc-shipping-zone-data-store.php
6 years ago
class-wc-webhook-data-store.php
6 years ago
class-wc-order-item-product-data-store.php
98 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class WC_Order_Item_Product_Data_Store file. |
| 4 | * |
| 5 | * @package WooCommerce\DataStores |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * WC Order Item Product Data Store |
| 14 | * |
| 15 | * @version 3.0.0 |
| 16 | */ |
| 17 | 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 { |
| 18 | |
| 19 | /** |
| 20 | * Data stored in meta keys. |
| 21 | * |
| 22 | * @since 3.0.0 |
| 23 | * @var array |
| 24 | */ |
| 25 | protected $internal_meta_keys = array( '_product_id', '_variation_id', '_qty', '_tax_class', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', '_line_tax_data' ); |
| 26 | |
| 27 | /** |
| 28 | * Read/populate data properties specific to this order item. |
| 29 | * |
| 30 | * @since 3.0.0 |
| 31 | * @param WC_Order_Item_Product $item Product order item object. |
| 32 | */ |
| 33 | public function read( &$item ) { |
| 34 | parent::read( $item ); |
| 35 | $id = $item->get_id(); |
| 36 | $item->set_props( |
| 37 | array( |
| 38 | 'product_id' => get_metadata( 'order_item', $id, '_product_id', true ), |
| 39 | 'variation_id' => get_metadata( 'order_item', $id, '_variation_id', true ), |
| 40 | 'quantity' => get_metadata( 'order_item', $id, '_qty', true ), |
| 41 | 'tax_class' => get_metadata( 'order_item', $id, '_tax_class', true ), |
| 42 | 'subtotal' => get_metadata( 'order_item', $id, '_line_subtotal', true ), |
| 43 | 'total' => get_metadata( 'order_item', $id, '_line_total', true ), |
| 44 | 'taxes' => get_metadata( 'order_item', $id, '_line_tax_data', true ), |
| 45 | ) |
| 46 | ); |
| 47 | $item->set_object_read( true ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Saves an item's data to the database / item meta. |
| 52 | * Ran after both create and update, so $id will be set. |
| 53 | * |
| 54 | * @since 3.0.0 |
| 55 | * @param WC_Order_Item_Product $item Product order item object. |
| 56 | */ |
| 57 | public function save_item_data( &$item ) { |
| 58 | $id = $item->get_id(); |
| 59 | $changes = $item->get_changes(); |
| 60 | $meta_key_to_props = array( |
| 61 | '_product_id' => 'product_id', |
| 62 | '_variation_id' => 'variation_id', |
| 63 | '_qty' => 'quantity', |
| 64 | '_tax_class' => 'tax_class', |
| 65 | '_line_subtotal' => 'subtotal', |
| 66 | '_line_subtotal_tax' => 'subtotal_tax', |
| 67 | '_line_total' => 'total', |
| 68 | '_line_tax' => 'total_tax', |
| 69 | '_line_tax_data' => 'taxes', |
| 70 | ); |
| 71 | $props_to_update = $this->get_props_to_update( $item, $meta_key_to_props, 'order_item' ); |
| 72 | |
| 73 | foreach ( $props_to_update as $meta_key => $prop ) { |
| 74 | update_metadata( 'order_item', $id, $meta_key, $item->{"get_$prop"}( 'edit' ) ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get a list of download IDs for a specific item from an order. |
| 80 | * |
| 81 | * @since 3.0.0 |
| 82 | * @param WC_Order_Item_Product $item Product order item object. |
| 83 | * @param WC_Order $order Order object. |
| 84 | * @return array |
| 85 | */ |
| 86 | public function get_download_ids( $item, $order ) { |
| 87 | global $wpdb; |
| 88 | return $wpdb->get_col( |
| 89 | $wpdb->prepare( |
| 90 | "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", |
| 91 | $order->get_billing_email(), |
| 92 | $order->get_order_key(), |
| 93 | $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id() |
| 94 | ) |
| 95 | ); |
| 96 | } |
| 97 | } |
| 98 |