0

I'm creating subscription programmatically, everything is working except when I want to use update_dates on the created subscription, this error happens : in_array() expects parameter 2 to be array, null given in /var/www/html/wp-content/plugins/woocommerce-subscriptions/includes/class-wcs-action-scheduler.php on line 30

The date is updated but I think I miss something and didn't find it ...

Here is how i've created the subscription (without an order I don't want one) :

$current_time = gmdate("Y-m-d H:i:s");
$sub_args = array( 
                'order_id' => null,
                'start_date' => $current_time,
                'billing_period' => 'year', 
                'billing_interval' => '1',
                'customer_id' => get_current_user_id(), 
                'status' => 'on-hold' );
$sub_created = wcs_create_subscription( $sub_args );
$sub_created->add_product( $sub_product_obj );
$sub_created->apply_coupon( $coupon_obj );
$update_dates['end'] = $next_day;

At this point no error

This is what causes the error : $sub_created->update_dates( $update_dates );

I tried to understand the process using by WC-Subscription and find that : Error came from that : $this->get_date_types_to_schedule() it returns null instead of an array

protected function get_date_types_to_schedule() {
        return $this->date_types_to_schedule;
    }

date_types_to_schedule is poulated by that :

Construct of WCS_Scheduler : add_action( 'init', array( $this, 'set_date_types_to_schedule' ) );

public function set_date_types_to_schedule() {
        $this->date_types_to_schedule = apply_filters( 'woocommerce_subscriptions_date_types_to_schedule', array_keys( wcs_get_subscription_date_types() ) );

        if ( isset( $this->date_types_to_schedule['start'] ) ) {
            unset( $this->date_types_to_schedule['start'] );
        }

        if ( isset( $this->date_types_to_schedule['last_payment'] ) ) {
            unset( $this->date_types_to_schedule['last_payment'] );
        }
    }

At the end of wcs_get_subscription_date_types $this->get_date_types_to_schedule() is an array ...

and I'm lost after that if someone can give an advice !

EDIT : so the way I'm updating the dates is correct, I put my code inside the method used by the backend to update a subscription and it's working without any error, I thought it was my subscription object who was not correct but even on an already created subscription same error ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.