mirror of
https://github.com/pronamic/woocommerce-subscriptions.git
synced 2025-10-16 14:22:56 +00:00
2.6.1
This commit is contained in:

committed by
Remco Tolsma

parent
42964bef17
commit
a60db3815d
@@ -24,9 +24,9 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||
* 'customer_id' The user ID of a customer on the site.
|
||||
* 'product_id' The post ID of a WC_Product_Subscription, WC_Product_Variable_Subscription or WC_Product_Subscription_Variation object
|
||||
* 'order_id' The post ID of a shop_order post/WC_Order object which was used to create the subscription
|
||||
* 'subscription_status' Any valid subscription status. Can be 'any', 'active', 'cancelled', 'suspended', 'expired', 'pending' or 'trash'. Defaults to 'any'.
|
||||
* 'subscription_status' Any valid subscription status. Can be 'any', 'active', 'cancelled', 'on-hold', 'expired', 'pending' or 'trash'. Defaults to 'any'.
|
||||
* 'order_type' Get subscriptions for the any order type in this array. Can include 'any', 'parent', 'renewal' or 'switch', defaults to parent.
|
||||
* @return array Subscription details in post_id => WC_Subscription form.
|
||||
* @return WC_Subscription[] Subscription details in post_id => WC_Subscription form.
|
||||
* @since 2.0
|
||||
*/
|
||||
function wcs_get_subscriptions_for_order( $order, $args = array() ) {
|
||||
@@ -896,3 +896,64 @@ function wcs_minutes_since_order_created( $order ) {
|
||||
function wcs_seconds_since_order_created( $order ) {
|
||||
return time() - $order->get_date_created()->getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a corresponding subscription line item on an order.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*
|
||||
* @param WC_Abstract_Order $order The order object to look for the item in.
|
||||
* @param WC_Order_Item $subscription_item The line item on the the subscription to find on the order.
|
||||
* @param string $match_type Optional. The type of comparison to make. Can be 'match_product_ids' to compare product|variation IDs or 'match_attributes' to also compare by item attributes on top of matching product IDs. Default 'match_product_ids'.
|
||||
*
|
||||
* @return WC_Order_Item|bool The order item which matches the subscription item or false if one cannot be found.
|
||||
*/
|
||||
function wcs_find_matching_line_item( $order, $subscription_item, $match_type = 'match_product_ids' ) {
|
||||
$matching_item = false;
|
||||
|
||||
if ( 'match_attributes' === $match_type ) {
|
||||
$subscription_item_attributes = wp_list_pluck( $subscription_item->get_formatted_meta_data( '_', true ), 'value', 'key' );
|
||||
}
|
||||
|
||||
$subscription_item_canonical_product_id = wcs_get_canonical_product_id( $subscription_item );
|
||||
|
||||
foreach ( $order->get_items() as $order_item ) {
|
||||
if ( wcs_get_canonical_product_id( $order_item ) !== $subscription_item_canonical_product_id ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if we have matching meta key and value pairs loosely - they can appear in any order,
|
||||
if ( 'match_attributes' === $match_type && wp_list_pluck( $order_item->get_formatted_meta_data( '_', true ), 'value', 'key' ) != $subscription_item_attributes ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$matching_item = $order_item;
|
||||
break;
|
||||
}
|
||||
|
||||
return $matching_item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an order contains a product.
|
||||
*
|
||||
* @since 2.6.0
|
||||
*
|
||||
* @param WC_Order $order An order object
|
||||
* @param WC_Product $product A product object
|
||||
*
|
||||
* @return bool $order_has_product Whether the order contains a line item matching that product
|
||||
*/
|
||||
function wcs_order_contains_product( $order, $product ) {
|
||||
$order_has_product = false;
|
||||
$product_id = wcs_get_canonical_product_id( $product );
|
||||
|
||||
foreach ( $order->get_items() as $line_item ) {
|
||||
if ( wcs_get_canonical_product_id( $line_item ) === $product_id ) {
|
||||
$order_has_product = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $order_has_product;
|
||||
}
|
||||
|
Reference in New Issue
Block a user