get_items( 'line_item' ) as $item ) { $amount = (float) $item->get_total(); $sum += $amount; $products[] = array( 'product_id' => (int) $item->get_product_id(), 'variation_id' => (int) $item->get_variation_id(), 'quantity' => (int) $item->get_quantity(), 'amount' => $amount, ); } $goods = self::goods_total( $order ); $factor = ( $sum > 0.0 ) ? ( $goods / $sum ) : 1.0; foreach ( $products as &$product ) { $product['amount'] = $product['amount'] * $factor; } unset( $product ); return $products; } public static function goods_total( $order ) { $total = 0.0; foreach ( array( 'line_item', 'fee' ) as $type ) { foreach ( $order->get_items( $type ) as $item ) { $total += (float) $item->get_total(); } } return $total; } public static function voucher_codes( $order ) { if ( method_exists( $order, 'get_coupon_codes' ) ) { return (array) $order->get_coupon_codes(); } return (array) $order->get_used_coupons(); } const CATEGORY_SLOTS = 10; public static function category_event_id( $product_id ) { $product_id = (int) $product_id; if ( $product_id <= 0 ) { return ''; } $terms = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ); if ( is_wp_error( $terms ) || ! $terms ) { return ''; } $all = array_map( 'intval', $terms ); foreach ( $terms as $term_id ) { $all = array_merge( $all, array_map( 'intval', get_ancestors( $term_id, 'product_cat', 'taxonomy' ) ) ); } $all = array_unique( $all ); for ( $slot = 1; $slot <= self::CATEGORY_SLOTS; $slot++ ) { $event_id = trim( (string) get_option( "roleWcAdcellCategoryEvent{$slot}EventId" ) ); $categories = self::category_slot( $slot ); if ( '' === $event_id || '0' === $event_id || ! $categories ) { continue; } if ( array_intersect( $all, $categories ) ) { return $event_id; } } return ''; } public static function shadowed_categories() { $seen = array(); $result = array(); for ( $slot = 1; $slot <= self::CATEGORY_SLOTS; $slot++ ) { $event_id = trim( (string) get_option( "roleWcAdcellCategoryEvent{$slot}EventId" ) ); $categories = self::category_slot( $slot ); if ( '' === $event_id || '0' === $event_id || ! $categories ) { continue; } foreach ( $categories as $term_id ) { $lineage = array_merge( array( $term_id ), array_map( 'intval', get_ancestors( $term_id, 'product_cat', 'taxonomy' ) ) ); foreach ( $lineage as $id ) { if ( isset( $seen[ $id ] ) ) { $result[ $slot ][ $term_id ] = $seen[ $id ]; break; } } } foreach ( $categories as $term_id ) { if ( ! isset( $seen[ $term_id ] ) ) { $seen[ $term_id ] = $slot; } } } return $result; } public static function category_slot( $slot ) { $value = get_option( "roleWcAdcellCategoryEvent{$slot}Categories", array() ); if ( is_string( $value ) ) { $value = json_decode( $value, true ); } if ( ! is_array( $value ) ) { return array(); } return array_values( array_filter( array_map( 'intval', $value ) ) ); } public static function is_new_customer( $order ) { $args = array( 'limit' => 2, 'return' => 'ids', 'status' => array( 'wc-processing', 'wc-completed', 'wc-on-hold' ), 'date_query' => array( array( 'before' => $order->get_date_created() ? $order->get_date_created()->date( 'Y-m-d H:i:s' ) : 'now' ) ), ); $customer_id = (int) $order->get_customer_id(); if ( $customer_id > 0 ) { $args['customer_id'] = $customer_id; } else { $email = $order->get_billing_email(); if ( ! $email ) { return true; } $args['billing_email'] = $email; } $previous = array_diff( (array) wc_get_orders( $args ), array( $order->get_id() ) ); return empty( $previous ); } }