1

I have products from some API and want to insert these products. I was able to insert products and use already existing attributes ( or added by UI ).

Adding new term (My Color) to the existing attribute (Color) :

// Add term to the attribute
wp_set_object_terms( $post_id, "My Color", 'pa_color' , true );

Use the added attribute with current product :

// Data for the term to be used
$theData = array(
                 'pa_color'=>
                    array( 
                       'name'=>'pa_color', 
                       'value'='My Color',
                       'is_visible' => '1',
                       'is_variation' => '1',
                       'is_taxonomy' => '1'
                    )
            );

// Add this attribute to this product
update_post_meta( $post_id,'_product_attributes',$theData);

How can I add new attribute and use it with the current product, for ex :

RAM : 4GB

I have tried this :

register_taxonomy(
    'pa_ram',
    'product',
    array(
            'label' => __( 'RAM' ),
            'rewrite' => array( 'slug' => 'size' ),
            'hierarchical' => true,
        )
    );
}

Here is the URL of the attributes I can see/add in UI :

wp-admin/edit.php?post_type=product&page=product_attributes

1 Answer 1

1

This did the job for me :

// Insert new attribute
function process_add_attribute($attribute) {
    global $wpdb;

    if (empty($attribute['attribute_type'])) { 
        $attribute['attribute_type'] = 'select'; 
    }
    if (empty($attribute['attribute_orderby'])) { 
        $attribute['attribute_orderby'] = 'name'; 
    }
    if (empty($attribute['attribute_public'])) { 
        $attribute['attribute_public'] = 1; 
    }

    if (empty($attribute['attribute_name']) || empty($attribute['attribute_label'])) {
        return new WP_Error('error', __('Please, provide an attribute name and slug.', 'woocommerce'));
    }
    elseif(($valid_attribute_name = valid_attribute_name($attribute['attribute_name'])) && is_wp_error($valid_attribute_name)) {
        return $valid_attribute_name;
    }

    $wpdb->insert($wpdb->prefix.'woocommerce_attribute_taxonomies', $attribute);

    do_action('woocommerce_attribute_added', $wpdb->insert_id, $attribute);

    flush_rewrite_rules();
    delete_transient('wc_attribute_taxonomies');

    return true;
}

function valid_attribute_name( $attribute_name ) {
    if ( strlen( $attribute_name ) >= 28 ) {
            return new WP_Error( 'error', sprintf( __( 'Slug "%s" is too long (28 characters max). Shorten it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ) );
    } elseif ( wc_check_if_attribute_name_is_reserved( $attribute_name ) ) {
            return new WP_Error( 'error', sprintf( __( 'Slug "%s" is not allowed because it is a reserved term. Change it, please.', 'woocommerce' ), sanitize_title( $attribute_name ) ) );
    }

    return true;
}

Call it like :

// Add new attribute
$status = process_add_attribute(array(
    'attribute_name' => 'myattribute', 
    'attribute_label' => 'My Attribute'
));
// Added successfully
if (!is_wp_error($status)) {
    // Continue 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.