• Resolved vlasovdmitri

    (@vlasovdmitri)


    Hi.
    How is an attribute value SLUG created? For example.
    wp_set_object_terms( $post_id, '30 mm', 'pa_height');
    Here the value of the slug will be a int ID, but you need this 30-mm ?(sanitize_title())

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Paulo P – a11n

    (@paulostp)

    Hello,

    Thanks for reaching out!

    Code customization is outside our scope of support, but let me try and point you in the right direction.

    If I understood correctly, you’re having issues with WordPress’s wp_set_object_terms function while trying to create attributes. Check this Stack Overflow topics as reference:

    I’m going to leave this topic open for a bit to see if anyone from the community is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Hope this helps!

    Thread Starter vlasovdmitri

    (@vlasovdmitri)

    Thank You!

    Thread Starter vlasovdmitri

    (@vlasovdmitri)

    Yes, I am assigning global attributes to the product, here is my code.

    $attributes = wc_get_attribute_taxonomies();
    	foreach($product_data['atrebuts'] as $key=>$val){
    		foreach($attributes as $value){
    			if($value->attribute_label == $key){
    				if($val[0]){				wp_set_object_terms( $post_id, $val[0], 'pa_'.$value->attribute_name);
    	             $slug = $value->attribute_name;  
    $terms = get_terms( array('taxonomy' => 'pa_' . $slug,'hide_empty' => false,));	
    				foreach($terms as $term_id){
    					if ($term_id->slug==sanitize_title($val[0])){$term_id_id=$term_id->term_id;$term_tax_id=$term_id->term_id;}
    		}	
                   	$new_attr = new WC_Product_Attribute();
                    $new_attr->set_id( $term_id_id ); 
                    $new_attr->set_name('pa_'.$slug);
                    $new_attr->set_options([$term_tax_id]);
                    $new_attr->set_visible( true );
                    $parent_attributes['pa_' . $slug] = $new_attr;
                    $product->set_attributes( array_values($parent_attributes) );
    		$arts['pa_'.$slug] = array(
    			'name' => 'pa_'.$slug,
    			'value' =>[$term_tax_id],
    			'postion' => '0',
    			'is_visible' => '1',
    			'is_variation' => '0',
    			'is_taxonomy' => '1'
    			);
    								}
    				}
    			}
    		}		
                    $product->save();
                   update_post_meta( $post_id, '_product_attributes', $arts);

    But there is a nuance here, when determining the term_id, I cannot correctly determine this ID for all parameters.

    If you check by name, then there is a problem with a dot and a comma, you get a different name for the same values. You can try to bring it to one type, but the slug has exactly a unique meaning and you don’t need to connect various functions, and here I can’t understand why the slug began to turn out as an ID, and not sanitize_title($name)

    Thread Starter vlasovdmitri

    (@vlasovdmitri)

    Perhaps something in the WordPress settings, because when I enter the value of the attribute in the admin panel, I also get slug = $tag_id and not slug = sanitize_title($name) ??

    • This reply was modified 2 years, 11 months ago by vlasovdmitri.

    Hello,

    Kindly note that in our forum the support for custom code is really limited. We recommend taking a look at the resources previously shared by Paulo to look for further assistance.

    Of course, this forum topic will still be open for a while just in case anyone else wants to chime in to help out.

    Cheers.

    • This reply was modified 2 years, 11 months ago by Igor H.
    Thread Starter vlasovdmitri

    (@vlasovdmitri)

    Oh, and how long did it take me. It turned out that the slug was created incorrectly due to the empty name field in the database (

    https://ibb.co/Pjxg7xs

    • This reply was modified 2 years, 11 months ago by vlasovdmitri.

    Glad to know you detected that!

    Cheers

    This thread has been inactive for a bit, so I’m going to mark it as closed.

    Please feel free to open a new thread if you have any other questions.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How is an attribute value SLUG created?’ is closed to new replies.