phpwordpresswoocommerceattributesproduct

Getting a Woocommerce product attribute value in a Quform hook


I'm trying to get the value of a woocommerce product product attribute (Which name is dwnfile).

That value is an url to be passed into hooked function that will populate a form re-direct. The form I'm using is maid with Quform commercial plugin.

I have received the code from the authors of the plugin regarding my form settings, but they couldn't advise me on how to get the attribute value.

This is the initial code they gave me:

function my_populate_wc_attribute($form)
{
    $value = 'some value'; // Get the value from WooCommerce somehow

    $form->setValue('iphorm_3_2', $value);
}
add_action('iphorm_pre_display_3', 'my_populate_wc_attribute');

Here's what I tried:

function my_populate_wc_attribute($form)
{
    global $product;
    $value = array_shift( wc_get_product_terms( $product->id, 'pa_dwnfile', array( 'fields' => 'names' ) ) );
    $form->setValue('iphorm_3_2', $value);
}
add_action('iphorm_pre_display_3', 'my_populate_wc_attribute');

But the value seems to be empty.

Any help on this please?


Solution

  • Update (at the end):

    Problem remains to $product global object (that you don't get in this hook).

    You should try instead:

    function my_populate_wc_attribute( $form ) {
        global $post;
        $term = wc_get_product_terms( $post->id, 'pa_nopeus', array( 'fields' => 'names' ) );
        $value = current( $term );
        $form->setValue('iphorm_3_2', $value);
    }
    add_action('iphorm_pre_display_3', 'my_populate_wc_attribute');
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    This should work.

    But this is not the best way to do it. You should add a custom field in the product "General" setting tab, instead of using a product attribute to get an URL value.


    UPDATE: Your main problem is that your form is NOT in your product page, so you can't get the post ID (or the product ID).

    You can try to do it with product custom field (instead of product attributes).

    Here is the code for the custom fields:

    // Add Fields
    add_action( 'woocommerce_product_options_general_product_data', 'add_product_general_custom_field' );
    function add_product_general_custom_field() {
    
        woocommerce_wp_text_input( array(
            'id'            => 'dwnfile',
            'label'         => __( 'Download link', 'woocommerce' ),
            'placeholder'   => 'https://',
            'desc_tip'      => 'true',
            'description'   => __( 'Add your download link …', 'woocommerce' ),
            'data_type'     => 'url', // Added the correct data type
        ) );
    }
    
    // Save Fields
    add_action( 'woocommerce_admin_process_product_object', 'save_product_general_custom_field' );
    function save_product_general_custom_field( $product ){
    
        if( isset( $_POST['dwnfile'] ) ) {
            $product->update_meta_data( '_dwnfile', sanitize_url( $_POST['dwnfile'] ) );
        }
    }
    

    You will get your "Download link" custom field (in the product general options):

    enter image description here

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    The form should be on the product page to get the product ID. if is not the case nothing will work.

    To get the url value, you will use: