diff --git a/admin/class-openedx-woocommerce-plugin-admin.php b/admin/class-openedx-woocommerce-plugin-admin.php
index eaddc01..aaf524b 100644
--- a/admin/class-openedx-woocommerce-plugin-admin.php
+++ b/admin/class-openedx-woocommerce-plugin-admin.php
@@ -197,6 +197,44 @@ public function create_post_type(
return new Openedx_Woocommerce_Plugin_Post_Type( $post_type, $plural, $single, $description, $options );
}
+ /**
+ * Add Open edX Course product type option in product settings.
+ *
+ * @param array $type_options Array of product type options.
+ * @return array $type_options Array of product type options.
+ */
+ public function add_openedx_course_product_type( $type_options ) {
+
+ global $post;
+ $checked = '';
+
+ if ( ! empty( get_post_meta( $post->ID, 'is_openedx_course', true ) ) ) {
+ $checked = get_post_meta( $post->ID, 'is_openedx_course', true );
+ } else {
+ $checked = 'no';
+ }
+
+ $type_options['wild_card'] = array(
+ 'id' => 'is_openedx_course',
+ 'wrapper_class' => 'show_if_simple',
+ 'label' => __( 'Open edX Course', 'woocommerce' ),
+ 'description' => __( 'Check this box if the product is an Open edX Course', 'woocommerce' ),
+ 'default' => $checked,
+ );
+ return $type_options;
+ }
+
+ /**
+ * Save the Open edX course product type option value into a post meta field.
+ *
+ * @param int $post_id Product post id.
+ * @return void
+ */
+ public function save_openedx_option( $post_id ) {
+ $openedx_course = isset( $_POST['is_openedx_course'] ) ? 'yes' : 'no';
+ update_post_meta( $post_id, 'is_openedx_course', $openedx_course );
+ }
+
/**
* Register course ID and mode fields for product
*
@@ -206,18 +244,6 @@ public function add_custom_product_fields() {
global $post;
- woocommerce_wp_checkbox(
- array(
- 'id' => '_is_openedx_course',
- 'label' => __( 'Check this box if the product is an Open edX Course', 'woocommerce' ),
- 'desc_tip' => 'true',
- 'class' => 'my-custom-checkbox',
- 'custom_attributes' => array(
- 'onchange' => 'handleCheckboxChange(this)',
- ),
- ),
- );
-
echo '
';
woocommerce_wp_text_input(
@@ -279,10 +305,11 @@ public function add_admin_order_item_values( $product, $item, $item_id = null )
$course_id = '';
if ( $product ) {
- $course_id = get_post_meta( $product->get_id(), '_course_id', true );
+ $course_id = get_post_meta( $product->get_id(), '_course_id', true );
+ $course_check = get_post_meta( $product->get_id(), 'is_openedx_course', true );
}
- if ( ! empty( $course_id ) ) {
+ if ( ! empty( $course_id ) && 'yes' === $course_check ) {
$order_id = method_exists( $item, 'get_order_id' ) ? $item->get_order_id() : $item['order_id'];
$input_value = get_post_meta( $order_id, 'enrollment_id' . $item_id, true );
@@ -341,13 +368,11 @@ public function save_order_meta_data( $order_id ) {
* @since 1.1.1
*/
public function save_custom_product_fields( $post_id ) {
- $course_id = isset( $_POST['_course_id'] ) ? sanitize_text_field( wp_unslash( $_POST['_course_id'] ) ) : '';
- $mode = isset( $_POST['_mode'] ) ? sanitize_text_field( wp_unslash( $_POST['_mode'] ) ) : '';
- $openedx_course = isset( $_POST['_is_openedx_course'] ) ? sanitize_text_field( wp_unslash( $_POST['_is_openedx_course'] ) ) : '';
+ $course_id = isset( $_POST['_course_id'] ) ? sanitize_text_field( wp_unslash( $_POST['_course_id'] ) ) : '';
+ $mode = isset( $_POST['_mode'] ) ? sanitize_text_field( wp_unslash( $_POST['_mode'] ) ) : '';
update_post_meta( $post_id, '_course_id', $course_id );
update_post_meta( $post_id, '_mode', $mode );
- update_post_meta( $post_id, '_is_openedx_course', $openedx_course );
}
/**
@@ -391,10 +416,11 @@ public function select_course_items( $items, $is_refunded = false ) {
foreach ( $items as $item_id => $item ) {
- $product_id = $item->get_product_id();
- $course_id = get_post_meta( $product_id, '_course_id', true );
+ $product_id = $item->get_product_id();
+ $course_id = get_post_meta( $product_id, '_course_id', true );
+ $course_check = get_post_meta( $product_id, 'is_openedx_course', true );
- if ( '' !== $course_id ) {
+ if ( '' !== $course_id && 'yes' === $course_check ) {
if ( $is_refunded ) {
$courses[] = array(
diff --git a/admin/js/product-type.js b/admin/js/product-type.js
index fc63653..5955141 100644
--- a/admin/js/product-type.js
+++ b/admin/js/product-type.js
@@ -1,5 +1,10 @@
document.addEventListener('DOMContentLoaded', function() {
- var checkbox = document.querySelector('.my-custom-checkbox');
+ var checkbox = document.getElementById("is_openedx_course");
+ handleCheckboxChange(checkbox);
+});
+
+document.addEventListener('change', function() {
+ var checkbox = document.getElementById("is_openedx_course");
handleCheckboxChange(checkbox);
});
diff --git a/includes/class-openedx-woocommerce-plugin.php b/includes/class-openedx-woocommerce-plugin.php
index 189a610..7bcbf83 100644
--- a/includes/class-openedx-woocommerce-plugin.php
+++ b/includes/class-openedx-woocommerce-plugin.php
@@ -246,6 +246,8 @@ private function define_admin_hooks() {
$this->loader->add_action( 'woocommerce_order_status_changed', $plugin_admin, 'process_order_data', 10, 2 );
$this->loader->add_action( 'woocommerce_order_refunded', $plugin_admin, 'unenroll_course_refund', 10, 2 );
+ $this->loader->add_filter( 'product_type_options', $plugin_admin, 'add_openedx_course_product_type' );
+ $this->loader->add_action( 'woocommerce_update_product', $plugin_admin, 'save_openedx_option' );
}
/**