Closed ChadReitsma closed 3 months ago
Anybody?
Hey @ChadReitsma, the wcs_additional_related_order_relation_types
filter expects an array formatted like:
Array
(
[0] => custom_relation_type
)
So you just need to change your filter slightly. Here's your snippet updated to use the correct format:
add_filter( 'wcs_additional_related_order_relation_types', 'modd_related_order_relation_types' );
function modd_related_order_relation_types( $types ) {
$types[] = 'weekly';
return $types;
}
I've tested this using the following snippet and confirmed it's working:
add_filter( 'wcs_additional_related_order_relation_types', 'modd_related_order_relation_types' );
function modd_related_order_relation_types( $types ) {
$types[] = 'weekly';
return $types;
}
add_action( 'admin_footer', function() {
$order = wc_get_order( 87775 );
$subscription = wcs_get_subscription( 87776 );
$order_relation = new WCS_Related_Order_Store_CPT();
$order_relation->add_relation( $order, $subscription, 'weekly' );
} );
Thanks Matt but it's still not working for me.
I'm trying to do this with a new WC_Order object (after checkout), we create some duplicate orders with some code like:
$order = wc_create_order([]); .... $order->save();
$order_relation = new WCS_Related_Order_Store_CPT(); $order_relation->add_relation( $order, $subscription, 'weekly' );
Still getting the error: Invalid relation type: weekly. Order relationship type must be one of: renewal, switch, resubscribe.
Had to put the filter in an mu-plugin, then it showed up with:
$order_relation = new WCS_Related_Order_Store_CPT(); print_r($order_relation->get_relation_types());
Array ( [0] => renewal [1] => switch [2] => resubscribe [3] => weekly )
But now they're not showing up in the related orders table. I'll keep trying.
I also needed to store the _relationship meta value, and add a filter for the related orders table in admin.
Here's my code for anyone that stumbles upon this in the future...
Note: We are using HPOS (which should be enabled by default soon), but if your store is still using legacy post_meta tables, you may need to adjust the wc_get_orders() query:
//Add "weekly" to the available WCS relationship types
add_filter( 'wcs_additional_related_order_relation_types', 'modd_related_order_relation_types' );
function modd_related_order_relation_types( $types ) {
$types[] = 'weekly';
return $types;
}
//Make sure the order has a "_relationship" meta key (used in the related orders table)
$order_relation = new WCS_Related_Order_Store_CPT();
$order_relation->add_relation( $order, $subscription, 'weekly' );
$order->update_meta_data('_relationship', 'Weekly');
$order->save_meta_data(); //required with HPOS
//Filter the admin "related orders" table to include "weekly" orders
add_filter('wcs_admin_subscription_related_orders_to_display', 'modd_admin_add_related_weekly_orders_to_table', 10, 3);
function modd_admin_add_related_weekly_orders_to_table($orders_to_display, $subscriptions, $this_order) {
foreach($subscriptions as $subscription) {
$orders = wc_get_orders(
array(
'meta_query' => array(
array(
'key' => '_subscription_weekly',
'value' => $subscription->get_id(),
'compare' => '='
)
),
)
);
if (!empty($orders)) {
foreach($orders as $order) {
$orders_to_display[] = $order;
}
}
}
return $orders_to_display;
}
Describe the bug
I'm trying to add a custom order relation type: "weekly" using the filter: wcs_additional_related_order_relation_types located in WCS_Related_Order_Store
Looks like it's casting/expecting an array, so my filter looks like this:
I am cloning an order, and then attempting to create the relationship by instantiating the WCS_Related_Order_Store_CPT class (which extends the abstract class above) with:
Throws: Fatal error: Uncaught InvalidArgumentException: Invalid relation type: weekly. Order relationship type must be one of: renewal, switch, resubscribe.
What am I doing wrong???
To Reproduce
Expected behavior
The relationship should be created with the custom type we added with the filter!
Actual behavior
Throws a fatal error, saying that Order relationship type must be one of: renewal, switch, resubscribe.
Product impact
Woocommerce Subscriptions