autoship_print_product_custom_fields (function)

( src/product-page.php) Outputs custom fields for simple products.

Function

/**
* Outputs the Autoship Custom Fields for a Product
* @see woocommerce_product_options_autoship_product_data
*
* Outputs the following Autoship Custom Fields:
*   _autoship_schedule_options_enabled
*   _autoship_checkout_price
*   _autoship_recurring_price
*   _autoship_schedule_order_enabled
*   _autoship_schedule_process_enabled
*   _autoship_override_frequency_options
*   _autoship_override_product_data
*   _autoship_title_override
*   _autoship_weightunit_override
*   _autoship_lengthunit_override
*   _autoship_group_ids
*
* The following dynamically created fields are
* controlled by the autoship_get_frequency_options_count()
*   _autoship_frequency_type_{$i}
*   _autoship_frequency_{$i}
*   _autoship_frequency_display_name_{$i}
*/
function autoship_print_product_custom_fields() {

  global $post;

  $_product = wc_get_product($post->ID);
  $active = 'yes' == autoship_sync_active_enabled( $_product );
  $next_occurrence =  autoship_get_product_relative_next_occurrence( $_product->get_id() );
  $next_occurrence_type = autoship_get_product_relative_next_occurrence_type( $_product->get_id() );

  $option_classes = apply_filters( 'autoship_simple_product_custom_fields_options_clases', 'show_if_simple hide_if_variable', $_product );

  ?>

  <div id="autoship-main-edit-product-options" class="options_group">

    <h4><?php echo __('Autoship Options', 'autoship'); ?></h4>

    <div class="options_group">

      <?php
      // Only show option when Global Sync is Not Active
      if ( !autoship_global_sync_active_enabled() ):?>

      <?php
      // Enable schedule options
      woocommerce_wp_checkbox(
        array(
          'id' => '_autoship_sync_active_enabled',
          'label' => __('Activate Product Sync', 'autoship'),
          'description' => __('Enable this setting to activate this product for Autoship Cloud Sync.', 'autoship'),
        )

      );?>

      <?php endif;?>

      <div class="autoship-sync-active-option-group">

        <?php
        // Enable schedule options
        woocommerce_wp_checkbox(
          array(
            'id' => '_autoship_schedule_options_enabled',
            'label' => __('Enable Schedule Options', 'autoship'),
            'description' => __('Enable this setting to show Autoship Schedule Options on the product page.', 'autoship')
          )
        );?>

        <?php do_action( 'autoship_before_print_product_custom_price_fields', $_product ); ?>

        <div class="<?php echo $option_classes; ?>">

          <?php
          // Checkout price
          woocommerce_wp_text_input(
            array(
              'id' => '_autoship_checkout_price',
              'label' => sprintf(__('Autoship Checkout Price(%s)', 'autoship'), get_woocommerce_currency_symbol()),
              'description' => __('Override the product price for autoship checkout only.', 'autoship'),
              'placeholder' => __('(Optional)', 'autoship'),
              'data_type' => 'price',
              'desc_tip' => true
            )
          );?>

        </div><!-- Checkout Price -->

        <div class="<?php echo $option_classes; ?>">

          <?php
          // Recurring price
          woocommerce_wp_text_input(
            array(
              'id' => '_autoship_recurring_price',
              'label' => sprintf(__('Autoship Recurring Price(%s)', 'autoship'), get_woocommerce_currency_symbol()),
              'description' => __('Override the product price for recurring orders only.', 'autoship'),
              'placeholder' => __('(Optional)', 'autoship'),
              'data_type' => 'price',
              'desc_tip' => true
            )
          );?>

        </div><!-- Recurring price -->

        <?php do_action( 'autoship_after_print_product_custom_price_fields', $_product ); ?>

        <?php

        // Only display the Schedule Orders Enabled and
        // Process on Scheduled orders options on this tab if
        // The product is simple otherwise display on variation level.
        if ( apply_filters( 'autoship_simple_product_display_scheduled_order_enabled_edit_fields', $_product->is_type('simple'), $_product ) ) { ?>

        </br>
        <h4><?php echo __('Autoship Scheduled Order Settings', 'autoship'); ?></h4>
        <p><?php echo __('Autoship Scheduled Order settings can be updated in <a target="_blank" href="'. admin_url( '/admin.php?page=products' ).'">Autoship Cloud > Products</a>', 'autoship'); ?></p>

        <?php
        //add_update_Schedule_option($_product->get_id());
        // Enable This Product to be added to Scheduled Orders
        woocommerce_wp_checkbox(
          array(
            'id' => '_autoship_schedule_order_enabled',
            'label' => __('Add to Scheduled Order', 'autoship'),
            'custom_attributes' => array('readonly' => 'readonly', 'disabled' => 'disabled'),
            'description' => __(' Allow customers to add this product to existing Scheduled Orders.', 'autoship'),
          )
        );
        // Enable This Product to be Processed on Scheduled Orders
        woocommerce_wp_checkbox(
          array(
            'id' => '_autoship_schedule_process_enabled',
            'label' => __('Process on Scheduled Orders', 'autoship'),
            'custom_attributes' => array('readonly' => 'readonly', 'disabled' => 'disabled'),
            'description' => __(' Allow this product to process on Scheduled Orders. <a target="_blank" href="https://support.autoship.cloud/article/441-product-availability-and-stock-status">Read this</a> before disabling this setting.', 'autoship'),
          )
        );?>

        <?php } ?>

        <?php do_action( 'autoship_after_print_product_custom_order_settings_fields', $_product ); ?>

      </div><!-- /.autoship-sync-active-option-group -->

    </div><!-- options_group -->

    <div class="autoship-sync-active-option-group">

      <div class="<?php echo $option_classes; ?>">

        <h4><?php echo __('Autoship Group IDs', 'autoship'); ?></h4>

        <div class="options_group">

          <?php
          // Assigned Group Ids
          woocommerce_wp_text_input(
            array(
              'id' => '_autoship_group_ids',
              'label' => __('Assigned Group IDs', 'autoship'),
              'description' => __('Assign one or more group ids. Enter multiple group ids in a comma separated list.', 'autoship'),
              'placeholder' => __('(Optional)', 'autoship'),
              'data_type' => 'text',
              'desc_tip' => true
            )
          );?>

        </div><!-- Assign Autoship Group Ids -->

      </div>

    </div>

    <div class="autoship-sync-active-option-group">

      <div class="<?php echo $option_classes; ?>">

      <?php

      // Show or hide Product Data Overrides based on checkbox value.
      $override = autoship_override_product_data_enabled( $_product->get_id() );
      $show_prod_overrides = 'yes' !== $override ? 'style="display:none;"' : '';

      ?>

      <h4><?php echo __('Product Data Overrides', 'autoship'); ?></h4>
      <div class="options_group">

        <?php
        // Enable AUtoship Frequency Overrides
        woocommerce_wp_checkbox(
          array(
            'id' => "_autoship_override_product_data",
            'label' => __('Override Defaults', 'autoship'),
            'class' => 'autoship_hide_show_toggler',
            'description' => __('Select to override the default Product Data in QPilot.', 'autoship'),
            'desc_tip' => false,
            'value' => $override,
            'custom_attributes' => array( 'data-target' => '.show_hide_product_data_group_simple' ),
          )
        );?>

      </div><!-- Enable AUtoship Frequency Overrides -->

      <div class="product_data_options show_hide_product_data_group_simple" <?=$show_prod_overrides?>>

        <?php

        woocommerce_wp_text_input(
          array(
            'id' => "_autoship_title_override",
            'label' => __("Product Title", 'autoship'),
            'description' => __('Enter a custom Product Title for QPilot', 'autoship'),
            'placeholder' => autoship_get_product_display_name( $_product ),
            'data_type' => 'text',
            'type' => 'text',
            'desc_tip' => true
          )
        );

        woocommerce_wp_select(
          array(
            'id' => "_autoship_weightunit_override",
            'label' => __("Weight unit", 'autoship'),
            'description' => __('Select a Unit of Measurement for Weight', 'autoship'),
            'options' => array(
              ''          => __('--Select type--', 'autoship'),
              'Kilogram'  => 'kg',
              'Gram'      => 'g',
              'Pound'     => 'lbs',
              'Ounce'     => 'oz',
            ),
            'data_type' => 'string',
            'desc_tip' => true
          )
        );

        woocommerce_wp_select(
          array(
            'id' => "_autoship_lengthunit_override",
            'label' => __("Dimensions Unit", 'autoship'),
            'description' => __('Select a Unit of Measurement for Length', 'autoship'),
            'options' => array(
              '' => __('--Select type--', 'autoship'),
              'Meter'       => 'm',
              'Centimeter'  => 'cm',
              'Milimeter'   => 'mm',
              'Inch'        => 'in',
              'Foot'        => 'ft',
              'Yard'        => 'yd',
            ),
            'data_type' => 'string',
            'desc_tip' => true
          )
        );?>

      </div><!-- .product_data_options -->

      </div>

    </div><!-- /.autoship-sync-active-option-group -->

    <?php do_action( 'autoship_before_print_product_custom_frequency_option_fields', $_product ); ?>

    <div class="autoship-sync-active-option-group">

      <?php

      // Show or hide Frequency Overrides based on checkbox value.
      $override = autoship_override_frequency_options_enabled( $post->ID );
      $show_freq_overrides = 'yes' !== $override ? 'style="display:none;"' : '';

      ?>

      <h4><?php echo __('Frequency Options', 'autoship'); ?></h4>
      <div class="options_group">

        <?php
        // Enable AUtoship Frequency Overrides
        woocommerce_wp_checkbox(
          array(
            'id' => "_autoship_override_frequency_options",
            'label' => __('Override Defaults', 'autoship'),
            'class' => 'autoship_hide_show_toggler',
            'description' => __('Select to override the default frequency options.', 'autoship'),
            'desc_tip' => false,
            'value' => $override,
            'custom_attributes' => array( 'data-target' => '.show_hide_frequency_options_group_simple' ),
          )
        );?>

      </div><!-- Enable AUtoship Frequency Overrides -->

      <div class="frequency_options show_hide_frequency_options_group_simple" <?=$show_freq_overrides?>>

        <?php
        $autoship_option_count = autoship_get_frequency_options_count();
        for ($i = 0, $n = 1; $i < $autoship_option_count; $i++, $n++) { ?>

        <div class="options_group show_hide_frequency_options">
          <h4><?php echo sprintf(__('Frequency Option %d', 'autoship'), $n);?></h4>
          <?php

          woocommerce_wp_select(
            array(
              'id' => "_autoship_frequency_type_{$i}",
              'label' => __("Frequency Type", 'autoship'),
              'description' => __('Select a frequency type', 'autoship'),
              'options' => array(
              '' => __('--Select frequency type--', 'autoship'),
              'Days' => __('Days', 'autoship'),
              'Weeks' => __('Weeks', 'autoship'),
              'Months' => __('Months', 'autoship'),
              'DayOfTheWeek' => __('Day of the week', 'autoship'),
              'DayOfTheMonth' => __('Day of the month', 'autoship')
            ),
            'data_type' => 'string',
            'desc_tip' => true
            )
          );

          woocommerce_wp_text_input(
            array(
              'id' => "_autoship_frequency_{$i}",
              'label' => __("Frequency", 'autoship'),
              'description' => __('Enter a frequency number', 'autoship'),
              'placeholder' => __('Enter a frequency number', 'autoship'),
              'data_type' => 'number',
              'type' => 'number',
              'desc_tip' => true
            )
          );
          woocommerce_wp_text_input(
            array(
              'id' => "_autoship_frequency_display_name_{$i}",
              'label' => __("Display Name", 'autoship'),
              'description' => __('Enter a display name for this frequency option', 'autoship'),
              'placeholder' => __('(Optional) Enter a display name', 'autoship'),
              'data_type' => 'string',
              'desc_tip' => true
            )
          );?>

        </div><!-- Frequency Overide -->

        <?php } ?>

      </div><!-- .frequency_options -->

    </div><!-- /.autoship-sync-active-option-group -->

    <?php do_action( 'autoship_after_print_product_custom_frequency_option_fields', $_product ); ?>

    <div class="autoship-sync-active-option-group">

      <h4><?php echo __('Next Occurrence Options', 'autoship'); ?></h4>

      <?php

      // Show or hide Relative Next Occurrence settings based on checkbox value.
      $relative_enabled = autoship_relative_next_occurrence_enabled( $post->ID );
      $show_relative = 'yes' !== $relative_enabled ? 'style="display:none;"' : '';

      ?>

      <div class="options_group">

        <?php

        // Enable Relative Next Occurrence
        woocommerce_wp_checkbox(
          array(
            'id' => "_autoship_relative_next_occurrence_enabled",
            'label' => __('Override Defaults', 'autoship'),
            'class' => 'autoship_hide_show_toggler',
            'description' => __('Select to enable options for Next Occurrence.', 'autoship'),
            'desc_tip' => false,
            'value' => $relative_enabled,
            'custom_attributes' => array( 'data-target' => '.show_hide_autoship_next_occurrence_options' ),
          )
        );
        ?>

      </div><!-- Enable Autoship Relative Next Occurrence -->

      <div class="options_group options-form-group autoship-next-occurrence show_hide_autoship_next_occurrence_options" <?=$show_relative?>>

        <h4><?php echo __('Set Next Occurrence Date Relative to Checkout', 'autoship'); ?></h4>
        <p><?php echo __('When this product is scheduled for Autoship, set the Next Occurrence date for this product’s Scheduled Order to a specific number of days after checkout.', 'autoship'); ?></p>

        <div class="option-form-row auto-flex-row">
          <div class="option-form-group auto-flex-col">
            <?php
            // Enable Autoship Relative Next Occurrence ?>
            <select id="_autoship_relative_next_occurrence_type" class="option-form-control" name="_autoship_relative_next_occurrence_type">

              <?php

              // Get the options
              $options = autoship_valid_relative_next_occurrence_types();

              ?>

              <?php foreach ($options as $key => $value): ?>

              <option value="<?php echo $key;?>" <?php selected( $key, $next_occurrence_type );?> ><?php echo $value;?></option>

              <?php endforeach; ?>

            </select>
          </div>
          <div class="option-form-group auto-flex-col">
            <input type="number" class="option-form-control" id="_autoship_relative_next_occurrence" name="_autoship_relative_next_occurrence" placeholder="Enter a Value" value="<?php echo absint( $next_occurrence );?>">
          </div>
        </div>

      </div><!-- Enable Autoship Relative Next Occurrence -->

    </div><!-- /.autoship-sync-active-option-group -->

    <?php do_action( 'autoship_after_print_product_custom_fields', $_product ); ?>

  </div><!-- All Autoship Custom Product Fields -->

  <?php
}
add_action( 'woocommerce_product_options_autoship_product_data', 'autoship_print_product_custom_fields' );