Adding product attributes/variations from the frontend on woocommerce [on hold]

I already have the template pages and functions for adding a variable product! I also have an edit page, where the frontend user can add attributes and then create variations. What i would like to do is force them to fill only a attribute (for example: size) when they go to the edit page.

This is how i am adding products:

wp_set_object_terms( $product_id, (int) $_POST['product_cat'], 'product_cat' );
    wp_set_object_terms( $product_id, 'variable', 'product_type' );
    wp_set_object_terms( $post_id, $avail_attributes, 'pa_size' );
$thedata = Array('pa_size'=>Array(
'name'=>'pa_size',
'value'=>'',
'is_visible' => '1', 
'is_variation' => '1',
'is_taxonomy' => '1'
));
update_post_meta( $post_id,'_product_attributes',$thedata);

    update_post_meta( $product_id, '_regular_price', $price );
    update_post_meta( $product_id, '_sale_price', '' );
    update_post_meta( $product_id, '_price', $price );
    update_post_meta( $product_id, '_visibility', 'visible' );
    update_post_meta( $product_id, 'A0', 'pa_size' );

    do_action( 'dokan_new_product_added', $product_id, $post_data );

And this is on my attributes edit page they have:

    // Array of defined attribute taxonomies
$attribute_taxonomies = wc_get_attribute_taxonomies();

 // Product attributes - taxonomies and custom, ordered, with visibility and variation           attributes set
 $attributes = maybe_unserialize( get_post_meta( $thepostid, '_product_attributes', true ) );

 $i = -1;

 // var_dump($attributes, $attribute_taxonomies);

 // Custom Attributes
if ( ! empty( $attributes ) ) {
foreach ( $attributes as $attribute ) {
    // var_dump($attribute);

    if ( $attribute['is_taxonomy'] ) {
        $tax = get_taxonomy( $attribute['name'] );

        $attribute_name = $tax->labels->name;
        $options = wp_get_post_terms( $thepostid, $attribute['name'], array('fields' => 'names') );
    } else {
        $attribute_name = $attribute['name'];
        $options = array_map( 'trim', explode('|', $attribute['value'] ) );

I’m quite new to all this wordpress functions, sorry for being unclear!

EDIT: There’s the code for adding attributes on the same page:

<div class="inputs-box woocommerce_attribute" data-count="<?php echo $i; ?>">

        <div class="box-header">
            <?php if ( $attribute['is_taxonomy'] ) { ?>

                <?php echo $attribute_name; ?>

                <input type="hidden" name="attribute_names[<?php echo $i; ?>]" value="<?php echo esc_attr( $attribute['name'] ); ?>">
                <input type="hidden" name="attribute_is_taxonomy[<?php echo $i; ?>]" value="1">

            <?php } else { ?>

                <input type="text" class="category-name" placeholder="Category name" name="attribute_names[<?php echo $i; ?>]" value="<?php echo esc_attr( $attribute_name ); ?>">
                <input type="hidden" name="attribute_is_taxonomy[<?php echo $i; ?>]" value="0">

            <?php } ?>

            <input type="hidden" name="attribute_position[<?php echo $i; ?>]" class="attribute_position" value="<?php echo esc_attr( $position ); ?>" />


            <span class="actions">
                <button class="row-remove btn pull-right btn-danger btn-sm">Remove</button>
            </span>
        </div>

        <div class="box-inside clearfix">

            <div class="attribute-config">
                <ul class="list-unstyled ">
                    <li>
                        <label class="checkbox-inline">
                            <input type="checkbox" class="checkbox" <?php
                            $tax = '';
                            // $i = 1;
                            if ( isset( $attribute['is_visible'] ) )
                                checked( $attribute['is_visible'], 1 );
                            else
                                checked( apply_filters( 'default_attribute_visibility', false, $tax ), true );

                            ?> name="attribute_visibility[<?php echo $i; ?>]" value="1" /> <?php _e( 'Visible on the product page', 'woocommerce' ); ?>
                        </label>
                    </li>

                    <li class="enable_variation show_if_variable">
                        <label class="checkbox-inline">
                        <input type="checkbox" class="checkbox" <?php

                        if ( isset( $attribute['is_variation'] ) )
                            checked( $attribute['is_variation'], 1 );
                        else
                            checked( apply_filters( 'default_attribute_variation', false, $tax ), true );

                    ?> name="attribute_variation[<?php echo $i; ?>]" value="1" /> <?php _e( 'Used for variations', 'woocommerce' ); ?></label>
                    </li>
                </ul>
            </div>

            <div class="attribute-options">
                <ul class="option-couplet list-unstyled ">
                    <?php

                    if ($options) {
                        foreach ($options as $count => $option) {
                            ?>

                            <li>
                                <input type="text" class="option" placeholder="Option..." name="attribute_values[<?php echo $i; ?>][<?php echo $count; ?>]" value="<?php echo esc_attr( $option ); ?>">

                                <span class="item-action actions">
                                    <a href="#" class="row-add">+</a>
                                    <a href="#" class="row-remove">-</a>
                                </span>
                            </li>

                            <?php
                        }
                    } else {
                        ?>

                        <li>
                            <input type="text" class="option" name="attribute_values[<?php echo $i; ?>][0]" placeholder="Option...">

                            <span class="item-action actions">
                                <a href="#" class="row-add">+</a>
                                <a href="#" class="row-remove">-</a>
                            </span>
                        </li>

                        <?php
                    }
                    ?>
                </ul>

            </div> <!-- .attribute-options -->

Read more here: Adding product attributes/variations from the frontend on woocommerce [on hold]

Leave a Reply

Your email address will not be published. Required fields are marked *