WooCommerce::attach_courses_to_product( $product_ID )


Source

File: classes/WooCommerce.php

	public function attach_courses_to_product( $product_ID ) {

		//Attach or detach course to product only from data panel
		$data_source = dozent_input_text( 'dozent_courses_data_panel' );
		if ( $data_source !== 'wc' ) {
			return;
		}

		$course_ids = (array) dozent_input_array_field( 'dozent_attach_course_ids_to_product' );
		$course_ids = array_filter( $course_ids );

		$attached_course_ids = dozent_get_attached_course_ids_to_product( $product_ID );

		if ( dozent_count( $attached_course_ids ) ) {
			$removed_course_ids = array_diff( $attached_course_ids, $course_ids );

			foreach ( $removed_course_ids as $removed_course_id ) {
				delete_post_meta( $removed_course_id, '_attached_product_id' );
				delete_post_meta( $removed_course_id, '_product_by' );
			}
		}

		if ( dozent_count( $course_ids ) ) {
			foreach ( $course_ids as $course_id ) {
				update_post_meta( $course_id, '_attached_product_id', $product_ID );
				update_post_meta( $course_id, '_product_by', 'WooCommerce' );
			}
		}

	}