Avatar of Robert Granlund
Robert Granlund
Flag for United States of America asked on

WordPress Change Class Function Select Field

I have a plugin that turns products into rentals in Woo.
The Product Data has the ability to show if a product is Rentable or not and the Default is No:

class WCRP_Rental_Products_Product_Fields {
public function product_data_panel() {

         global $post;

         ?>

         <div id="wcrp-rental-products-panel" class="panel woocommerce_options_panel">

            <?php

            echo '<div class="wcrp-rental-products-panel-heading"><span class="dashicons dashicons-calendar-alt"></span>' . esc_html__( 'Rental', 'wcrp-rental-products' ) . '</div>';
            echo '<div class="notice notice-info inline"><span class="dashicons dashicons-info"></span>' . esc_html__( 'Set whether this product is a rental.', 'wcrp-rental-products' ) . '</div>';
            
            woocommerce_wp_select(
               array(
                  'id'           => '_wcrp_rental_products_rental',
                  'value'         => get_post_meta( $post->ID, '_wcrp_rental_products_rental', true ),
                  'label'         => esc_html__( 'Rental product', 'wcrp-rental-products' ),
                  'desc_tip'      => true,
                  'description'   => esc_html__( 'Enables rentals of this product. Default is no.', 'wcrp-rental-products' ),
                  'options'      => array(
                     ''            => __( 'No', 'wcrp-rental-products' ),
                     'yes'         => __( 'Yes - Rental only', 'wcrp-rental-products' ),
                     'yes_purchase'   => __( 'Yes - Rental or purchase', 'wcrp-rental-products' )
                  ),
                  'selected'      => true,
               )
            );

            ?>

I would like to extend this Class and filter the function so as that the Rental option is set to  Yes-Rental or Purchase:
    'options'      => array(
                     ''            => __( 'No', 'wcrp-rental-products' ),
                     'yes'         => __( 'Yes - Rental only', 'wcrp-rental-products' ),  //// MAKE THIS SELECTED, Not NO
                     'yes_purchase'   => __( 'Yes - Rental or purchase', 'wcrp-rental-products' )
                  ),
                  'selected'      => true,

PHPWordPress

Avatar of undefined
Last Comment
Scott Fell

8/22/2022 - Mon
Scott Fell

Just a good guess, viewing the source when the page is rendered, you see
<select>
   <option value="">No</option>
   <option value="yes">Yes - Rental only</option>
    <option value="yes_purchase ">Yes - Rental or purchase </option>
</select>

Open in new window

What happens is the first item is the default. The easy option then may be to change the order of the array.

'options' => array(
                 
    'yes' => __( 'Yes - Rental only', 'wcrp-rental-products' ),  //// MAKE THIS SELECTED, Not NO
   'yes_purchase'   => __( 'Yes - Rental or purchase', 'wcrp-rental-products' ),
   ''            => __( 'No', 'wcrp-rental-products' ),
  ),
'selected'      => true,

Open in new window


Robert Granlund

ASKER
@scottt, OK, but how do I extend that class from the plugin and then filter the function?
Scott Fell

Your only option that I know of is to duplicate the plug in and change the name of it, then manually update the classes to what you want.

Being that is a paid extension, check out what your rights to the code are before making any modifications.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Scott Fell

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.