• Resolved ZE-Company

    (@zecompany)


    Hello,

    Is there a way to set settings for a product programmatically ?

    When I look to the code the methods used to save settings use $_POST variables so I don’t know how to achieve that.

    I hope you will be able to help me.

    Regards

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support dominikl65

    (@dominikl65)

    Hi,

    Thank you for reaching out to us.

    May I ask what kind of scenario you would like to accomplish with these programmatic settings, and what you mean by programmatic settings for the product?

    Thread Starter ZE-Company

    (@zecompany)

    I have a catalog build programmatically from a link with another software and I would like to enable the settings for the new unit of measurement for some of the products.

    I think I am able to do an “update_post_meta” on “flexible_quantity_settings” but if you change the way you store infos it will break. And with this solution it maybe doesn’t fire all the hooks like your function fq_price_calculator_process_product_meta_measurement.

    Thread Starter ZE-Company

    (@zecompany)

    Hi,

    So do you have any clue/advise to set settings for products through php ?

    Has I said, without other information, I will probably try to update the post metas according to the current code (with the “fq” array) hoping it will not break anything.

    Regards

    Plugin Support dominikl65

    (@dominikl65)

    Hi

    If you want to save settings programmatically then we don’t have an API for that with documentation in this plugin, but we do have a preview of how it’s done – it’s a relatively simple function. You can replace $_POST with your own array defined in your script – this array just needs to have the same names as those passed through the settings page in the form fields.

    /**
     * Save the custom fields
     *
     * @param int   $post_id post identifier
     * @param array $post    the post object
     */
    function fq_price_calculator_process_product_meta_measurement( $post_id, $post ) {
        if ( isset( $_POST['fq'] ) && is_array( $_POST['fq'] ) ) {
            $settings_data = [
                'fq' => wc_clean( $_POST['fq'] ),
            ];
            $container     = new Settings( $post_id );
            $container->set_raw_settings( $settings_data );
            $settings = $container->get_settings();
    
            if ( isset( $settings['fq']['enable'] ) && $settings['fq']['enable'] == 'yes' ) {
                $price   = ! empty( $settings['fq']['price'] ) ? abs( $settings['fq']['price'] ) : 0;
                $sale    = ! empty( $settings['fq']['sale_price'] ) ? abs( $settings['fq']['sale_price'] ) : '';
                $product = wc_get_product( $post_id );
                $product->set_price( $price );
                $product->set_regular_price( $price );
                $product->set_sale_price( $sale );
                $product->set_sold_individually( isset( $settings['fq']['sold_individually'] ) && $settings['fq']['sold_individually'] == 'yes' );
                $product->save();
            }
        }
    }

    If you save it the way it is in this function, nothing should blow up, because that’s exactly how the plugin saves it, although the values of these halves probably don’t particularly verify, so you also have to check what options are available and their names
    Here is the array that is passed in $_POST (the data you want to change)

    
    fq[enable]: yes
    fq[unit]: m
    fq[price]: 20
    fq[sale_price]:
    fq[increment]: 0.01
    fq[min_range]: 0.1
    fq[max_range]:
    fq[decimals_enabled]: yes
    fq[decimals][length][type]: user
    fq[decimals][length][fixed][label]: nnnn
    fq[decimals][length][fixed][unit]: cm
    fq[decimals][length][fixed][size]:
    fq[decimals][length][user][label]: centymetry
    fq[decimals][length][user][unit]: cm
    fq[decimals][length][user][increment]: 1
    fq[decimals][length][user][min_quantity]: 1
    fq[decimals][length][user][max_quantity]:
    fq[pricing_table][items][from][]:
    fq[pricing_table][items][to][]:
    fq[pricing_table][items][price][]:
    fq[pricing_table][items][sale_price][]:
    fq[shipping_table][items][from][]:
    fq[shipping_table][items][to][]:
    fq[shipping_table][items][shipping_class][]: -1
    Thread Starter ZE-Company

    (@zecompany)

    Hi

    Thank you for your response.

    If I understand well, I have to copy this function and adapt it replacing $_POST variable with an other passed in argument.

    I tried it and it works. The thing is if you change the function later I will have to change mine also.

    Am I correct ? I can’t call the plugin function directly ?

    Plugin Support dominikl65

    (@dominikl65)

    Hi,

    I would like to let you know since it is modifying and customizing the plugin this in general, it is not a function intended for external use (as I wrote, we do not issue any API).
    We cannot promise that the implementation of the function will not change
    whatever you do, you do so at your own risk when it comes to modifying the plugin.

    Plugin Support dominikl65

    (@dominikl65)

    Hi,

    I haven’t heard from you in a while, so I’m marking this thread as resolved. Please don’t hesitate to open a new one if you encounter other issues while using our plugin.

    Have a fantastic day,

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘programmatically set settings’ is closed to new replies.