Skip to content

Commit

Permalink
feat: option to set open edx course product type to a product
Browse files Browse the repository at this point in the history
  • Loading branch information
julianramirez2 committed Oct 6, 2023
1 parent 76fd305 commit ede33cb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 22 deletions.
68 changes: 47 additions & 21 deletions admin/class-openedx-woocommerce-plugin-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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 '<div class="custom_options_group">';

woocommerce_wp_text_input(
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
}

/**
Expand Down Expand Up @@ -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(
Expand Down
7 changes: 6 additions & 1 deletion admin/js/product-type.js
Original file line number Diff line number Diff line change
@@ -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);
});

Expand Down
2 changes: 2 additions & 0 deletions includes/class-openedx-woocommerce-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}

/**
Expand Down

0 comments on commit ede33cb

Please sign in to comment.