Update automatically a form inside a function of a plugin

Question

I tried several ways to do it but it seems I can do it only actually submitting that form (price_form). But how can I do it automatically and without being in that page?

Here the code

    function woocommerce_gold_price_display_config_tab() {
        if ( ! isset( $_REQUEST['settings-updated'] ) ) {

            $_REQUEST['settings-updated'] = false;

        }

        if ( false !== $_REQUEST['settings-updated'] ) {

            ?>
                <div id="message" class="updated notice">
                    <p><strong><?php esc_html_e( 'Your settings have been saved.', 'woocommerce-gold-price' ) ?></strong></p>
                </div>
            <?php

        }
        ?>

        <h1><?php esc_html_e( 'Gold Price Settings', 'woocommerce-gold-price' )?></h1>

        <form method="post" action="options.php">
            <?php
                settings_fields( 'woocommerce_gold_price_options' );
                do_settings_sections( 'woocommerce_gold_price' );
            ?>
            <p class="submit">
                <input id="price_form" type="submit" class="button-primary" value="<?php esc_html_e( 'Save changes', 'woocommerce-gold-price' ) ?>" />
            </p>
        </form>
        <hr />

        <h1><?php esc_html_e( 'Gold priced products', 'woocommerce-gold-price' )?></h1>

        <?php

        $karats = get_option( 'woocommerce_gold_price_options' );

        if ( ! $karats ) {

            $karats = array( '24k' => 0, '22k' => 0, '18k' => 0, '14k' => 0 );

        }

        foreach( $karats as $key => $value ) {

            $value     = floatval( str_replace( ',', '', $value ) );

            $the_query = new WP_Query( array(
                'post_type'      => 'product',
                'posts_per_page' => -1,
                'meta_key'       => 'is_gold_price_product',
                'meta_value'     => 'yes',
                'meta_query'     => array(
                    'key'     => 'gold_price_karats',
                    'value'   =>  $key,
                    'compare' => '=',
                    ),
                ) ); 

            ?>

            <h2><?php echo $key ?></h2>

            <?php

            if ( 0 == $the_query->found_posts ) {

                echo '<p>' . __( 'No gold products found.', 'woocommerce-gold-price' ) . '</p>';

            } else {

                echo '
                    <ol>';

                // The Loop
                while ( $the_query->have_posts() ) {

                    $the_query->the_post();

                    $the_product = wc_get_product( $the_query->post->ID );

                    $edit_url    = admin_url( 'post.php?post=" . $the_product->get_id() . "&action=edit' );
                    $message="";
                    
                    $spread = empty(get_post_meta( $the_product->get_id(), 'gold_price_product_spread', true )) ? 0 : get_post_meta( $the_product->get_id(), 'gold_price_product_spread', true );

                    //$spread      = get_post_meta( $the_product->get_id(), 'gold_price_product_spread', true );

                    $fee         = get_post_meta( $the_product->get_id(), 'gold_price_product_fee', true );
                    $fee         = floatval( str_replace( ',', '', $fee   ) );

                    echo '
                        <li><a href="' . $edit_url. '">' . get_the_title(). '</a>';

                    if ( ! $the_product->has_weight() ) {
                        
                        $message = __( 'Product has zero weight, can't calculate price based on weight.', 'woocommerce-gold-price' );

                    } else {

                        if ( $the_product->is_on_sale() ) {

                            $message = __( 'Product was on sale, can't calculate sale price.', 'woocommerce-gold-price' );

                        }

                        $weight_price = $the_product->get_weight() * $value;
                        $spread_price = $weight_price * ( ( $spread / 100 ) + 1 );
                        $gold_price   = $spread_price +  $fee;

                        echo ': ' . $the_product->get_weight() . woocommerce_gold_price_weight_description()  . ' * ' .  wc_price( str_replace(',', '', $karats[ $key ] ) ) ;


                        echo ' (' . wc_price( $weight_price )  . ') ';

                        if ( $spread ) {

                            echo ' + ' . wc_price( $spread_price - $weight_price  ) . ' (' . $spread . '%)' ;

                        }

                        if ( $fee ) {

                            echo ' + ' . wc_price( $fee );

                        }

                        echo ' = <strong>' . wc_price( $gold_price ) . '</strong>';

                        if ( false === $_REQUEST['settings-updated'] ) {

                            if ( wc_price( $gold_price ) != wc_price( $the_product->get_regular_price() ) ) {

                            $message .= ' | ' . sprintf( __( 'Warning! This product price (%s) is not based on Gold Price, press the "Save changes" button to update it.', 'woocommerce-gold-price' ), wc_price( $the_product->get_regular_price() ) );
                            }

                        } else {

                            $the_product->set_price( $gold_price );
                            $the_product->set_regular_price( $gold_price );
                            $the_product->set_sale_price( '' );
                            $the_product->set_date_on_sale_from( '' );
                            $the_product->set_date_on_sale_to( '' );

                            $the_product->save();

                            update_post_meta( $the_product->get_id(), '_price',         $gold_price );
                            update_post_meta( $the_product->get_id(), '_regular_price', $gold_price );
                            update_post_meta( $the_product->get_id(), '_sale_price', '' );
                            update_post_meta( $the_product->get_id(), '_sale_price_dates_from', '' );
                            update_post_meta( $the_product->get_id(), '_sale_price_dates_to', '' );

                            $log_message = sprintf( __( 'Updated price for %1$s', 'woocommerce-gold-price' ), $the_product->get_title() );

                            woocommerce_gold_price_log( $log_message );


                        }
                    }

                    echo ' ' . $message . '</li>';
                }

                echo '
                    </ol>';
            }

            // Restore original Query & Post Data
            wp_reset_query();
            wp_reset_postdata();

        }
    }

0
sangkavr 1 month 2021-10-24T09:12:47-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse