Skip to content

Commit

Permalink
feat: checkbox to enable options if product is a course (#42)
Browse files Browse the repository at this point in the history
* feat: checkbox to enable options if product is a course

* feat: option to set open edx course product type to a product

* refactor: added end line to js and css files
  • Loading branch information
julianramirez2 authored Oct 6, 2023
1 parent 6ae1ead commit 5fe899b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 11 deletions.
57 changes: 46 additions & 11 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,11 +244,7 @@ public function add_custom_product_fields() {

global $post;

echo '<div class="options_group">';

echo '<p class="form-field">' .
esc_html__( 'Only use these fields if the product is an Open edX course.', 'woocommerce' )
. '</p>';
echo '<div class="custom_options_group">';

woocommerce_wp_text_input(
array(
Expand Down Expand Up @@ -244,7 +278,6 @@ public function add_custom_product_fields() {
echo '</div>';
}


/**
* Create a custom column in order items table inside an order
*
Expand Down Expand Up @@ -272,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 @@ -394,10 +428,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
4 changes: 4 additions & 0 deletions admin/css/openedx-woocommerce-plugin-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,7 @@
#namediv #openedx_enrollment_order_id {
width: 200px;
}

.custom_options_group{
display: none;
}
18 changes: 18 additions & 0 deletions admin/js/product-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
document.addEventListener('DOMContentLoaded', function() {
var checkbox = document.getElementById("is_openedx_course");
handleCheckboxChange(checkbox);
});

document.addEventListener('change', function() {
var checkbox = document.getElementById("is_openedx_course");
handleCheckboxChange(checkbox);
});

function handleCheckboxChange(checkbox) {
var optionsGroup = document.querySelector('.custom_options_group');
if (checkbox.checked) {
optionsGroup.style.display = 'unset';
} else {
optionsGroup.style.display = 'none';
}
}
14 changes: 14 additions & 0 deletions includes/class-openedx-woocommerce-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public function __construct() {
$this->define_admin_hooks();
$this->define_public_hooks();
$this->define_plugin_settings_hooks();
$this->define_enqueue_scripts();
}

/**
Expand Down Expand Up @@ -245,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 All @@ -265,6 +268,17 @@ private function define_public_hooks() {
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
}

/**
* Register all the hooks related to custom scripts for specific functionalities.
*
* @since 1.11.0
* @access private
*/
private function define_enqueue_scripts() {
wp_register_script( 'product-type-script', plugin_dir_url( __FILE__ ) . '../admin/js/product-type.js', array(), $this->get_version(), true );
wp_enqueue_script( 'product-type-script' );
}

/**
* Define the plugin settings hooks.
*
Expand Down

0 comments on commit 5fe899b

Please sign in to comment.