← All changes
|
includes/my-wordpress/integrations/woocommerce-relations.php
+71
-4
1.0.1
→
1.1.11
View file →
| @@ -59,8 +59,27 @@ | ||
| 59 | 59 | */ |
| 60 | 60 | const OPENSTATION_WOO_RELATION_ITEM_CAP = 20; |
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | + * How many orders the product and coupon groups list. | |
| 64 | + */ | |
| 65 | +const OPENSTATION_WOO_RELATION_ORDER_CAP = 10; | |
| 66 | + | |
| 67 | +/** | |
| 68 | + * How many order-item rows to read to fill that list. | |
| 69 | + * | |
| 70 | + * The id lists come out of `woocommerce_order_items`, which holds | |
| 71 | + * refund rows alongside order rows — and refunds sort *first* there, | |
| 72 | + * since the query orders by descending id and a refund is created | |
| 73 | + * after the order it refunds. A `LIMIT 10` on a much-refunded product | |
| 74 | + * could therefore come back as ten refunds and no orders at all, and | |
| 75 | + * the group would render empty on the one product whose history a | |
| 76 | + * merchant most wants to read. Reading a few times the budget and | |
| 77 | + * stopping at the cap costs one bounded query. | |
| 78 | + */ | |
| 79 | +const OPENSTATION_WOO_RELATION_ORDER_CANDIDATES = 40; | |
| 80 | + | |
| 81 | +/** | |
| 63 | 82 | * Query flag marking a person-URL as a request for a *particular* |
| 64 | 83 | * view of that person rather than for the profile editor. |
| 65 | 84 | * |
| 66 | 85 | * Must stay equal to `OS_PERSON_VIEW_PARAM` in |
| @@ -130,8 +149,46 @@ | ||
| 130 | 149 | return true === openstation_my_wordpress_woo_orders_permission(); |
| 131 | 150 | } |
| 132 | 151 | |
| 133 | 152 | /** |
| 153 | + * Whether an object read back from an order-item row is a purchase. | |
| 154 | + * | |
| 155 | + * Refunds keep their own line items in the same | |
| 156 | + * `woocommerce_order_items` tables, under the refund's id — so a | |
| 157 | + * lookup that asks those tables "which orders contain product X" | |
| 158 | + * answers with refund ids too, for any product that has ever been | |
| 159 | + * refunded. `WC_Order_Refund` extends `WC_Abstract_Order`, so the | |
| 160 | + * usual guard waves it through, and the next line asks it for | |
| 161 | + * `get_order_number()`: a `WC_Order` method the abstract base does | |
| 162 | + * not declare, and therefore a fatal on the product edit screen. | |
| 163 | + * | |
| 164 | + * Dropping refunds is also the truer answer. "Who bought this" and | |
| 165 | + * "where was this coupon used" are questions about purchases, and a | |
| 166 | + * refund is the undoing of one. | |
| 167 | + * | |
| 168 | + * Deliberately *not* `instanceof WC_Order`. The abstract base is the | |
| 169 | + * type every order class actually extends, including HPOS's overrides | |
| 170 | + * and whatever custom order type a store registers — testing against | |
| 171 | + * `WC_Order` has already been tried elsewhere in this integration and | |
| 172 | + * silently emptied lists on stores that use them. So this excludes the | |
| 173 | + * one known-hostile subclass and then asks the object directly for the | |
| 174 | + * accessors these lists call, which keeps an exotic order type that | |
| 175 | + * extends the base without them out of a fatal too. | |
| 176 | + * | |
| 177 | + * @param mixed $order Whatever `wc_get_order()` returned. | |
| 178 | + * @return bool | |
| 179 | + */ | |
| 180 | +function openstation_my_wordpress_woo_is_purchase( $order ) { | |
| 181 | + if ( ! $order instanceof WC_Abstract_Order ) { | |
| 182 | + return false; | |
| 183 | + } | |
| 184 | + if ( $order instanceof WC_Order_Refund ) { | |
| 185 | + return false; | |
| 186 | + } | |
| 187 | + return method_exists( $order, 'get_order_number' ); | |
| 188 | +} | |
| 189 | + | |
| 190 | +/** | |
| 134 | 191 | * The content identity for WooCommerce's product-reviews screen when |
| 135 | 192 | * it is filtered to a single product. |
| 136 | 193 | * |
| 137 | 194 | * `edit.php?post_type=product&page=product-reviews&product_id=N`. |
| @@ -729,13 +786,18 @@ | ||
| 729 | 786 | // data reached from a product screen, and a shop editor who may |
| 730 | 787 | // not read orders must not read them sideways. |
| 731 | 788 | if ( openstation_my_wordpress_woo_can_read_orders() ) { |
| 732 | 789 | $customers = array(); |
| 733 | - foreach ( openstation_my_wordpress_woo_orders_with_product( $product_id, 10 ) as $order_id ) { | |
| 790 | + $listed = 0; | |
| 791 | + foreach ( openstation_my_wordpress_woo_orders_with_product( $product_id, OPENSTATION_WOO_RELATION_ORDER_CANDIDATES ) as $order_id ) { | |
| 792 | + if ( $listed >= OPENSTATION_WOO_RELATION_ORDER_CAP ) { | |
| 793 | + break; | |
| 794 | + } | |
| 734 | 795 | $order = wc_get_order( $order_id ); |
| 735 | - if ( ! $order instanceof WC_Abstract_Order ) { | |
| 796 | + if ( ! openstation_my_wordpress_woo_is_purchase( $order ) ) { | |
| 736 | 797 | continue; |
| 737 | 798 | } |
| 799 | + ++$listed; | |
| 738 | 800 | |
| 739 | 801 | $name = method_exists( $order, 'get_formatted_billing_full_name' ) |
| 740 | 802 | ? trim( $order->get_formatted_billing_full_name() ) |
| 741 | 803 | : ''; |
| @@ -925,13 +987,18 @@ | ||
| 925 | 987 | // nothing behind it, so "did this campaign work, and for whom" is |
| 926 | 988 | // a question you currently answer by exporting orders. |
| 927 | 989 | if ( openstation_my_wordpress_woo_can_read_orders() ) { |
| 928 | 990 | $customers = array(); |
| 929 | - foreach ( openstation_my_wordpress_woo_orders_with_coupon( $coupon->get_code(), 10 ) as $order_id ) { | |
| 991 | + $listed = 0; | |
| 992 | + foreach ( openstation_my_wordpress_woo_orders_with_coupon( $coupon->get_code(), OPENSTATION_WOO_RELATION_ORDER_CANDIDATES ) as $order_id ) { | |
| 993 | + if ( $listed >= OPENSTATION_WOO_RELATION_ORDER_CAP ) { | |
| 994 | + break; | |
| 995 | + } | |
| 930 | 996 | $order = wc_get_order( $order_id ); |
| 931 | - if ( ! $order instanceof WC_Abstract_Order ) { | |
| 997 | + if ( ! openstation_my_wordpress_woo_is_purchase( $order ) ) { | |
| 932 | 998 | continue; |
| 933 | 999 | } |
| 1000 | + ++$listed; | |
| 934 | 1001 | |
| 935 | 1002 | $name = method_exists( $order, 'get_formatted_billing_full_name' ) |
| 936 | 1003 | ? trim( $order->get_formatted_billing_full_name() ) |
| 937 | 1004 | : ''; |