• i want to make a webhook when adding a product or an attribute or updating an attribute.
    For example:

    function add_this_to_new_products( $new_status, $old_status, $post ) {
        $defaults = array ( 'shopid' => array (
                                      'name' => 'shopid',
                                      'value' => 'mahdi',
                                      'position' => 1,
                                      'is_visible' => 1,
                                      'is_variation' => 1,
                                      'is_taxonomy' => 1,
                                   ),
    
        );
    
    update_post_meta( $post->ID , '_product_attributes', $defaults );
    
    }
    
    add_action( 'transition_post_status', 'add_this_to_new_products', 10, 3 );

    Or

    function add_this_to_new_products( $new_status, $old_status, $post ) {
        $defaults = get_bloginfo();
    update_post_meta( $post->ID , '_product_attributes', $defaults );
    
    }
    
    add_action( 'transition_post_status', 'add_this_to_new_products', 10, 3 );
    

    But none of these works. please guide me. Thank you

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    First of all, ‘transition_post_status’ may not be the best hook because all posts trigger this. You’re adding post meta to regular posts and pages too. You should at least confirm the post_type before adding post meta. Better yet, use something like ‘publish_product’ which is unique to product post types, so you do not need to verify post_type. This fires for both new and updated products. The do_action() call passes 2 parameters: $post->ID, $post This is a dynamic hook, the generic form is “{$new_status}_{$post->post_type}”

    Setting post meta is only part of the puzzle. Each attribute is actually a separate taxonomy and each attribute value is a taxonomy term. Assuming the attribute and value already exist, you only need to assign the value/term to the product post, similar to how you would add a category term to a post. Since products and attributes are custom posts and taxonomies, we use the generic wp_set_object_terms() Note that attribute taxonomies take the form “pa_{$attribute_slug}”. This function by default replaces all terms with the ones specified. Be sure to pass true as the fourth parameter to simply add values instead of replacing.

    When you do update post meta, you may wish to first get any existing values. By updating the one attribute without checking, you may be wiping out any previously assigned attributes or values. Depending on what is returned, it may be more appropriate to either do nothing or add your attribute/value to the existing array.

    That covers when a product is added or updated. You also mentioned capturing added or updated attributes. You would need to identify the proper taxonomy/term hooks for this. I don’t understand what should happen during one of these actions. There may not be a product involved. Adding an attribute/taxonomy is also very different than adding a value/term.

    I hope I’ve given you enough to work out what it is you need to do. If you need more information, just ask!

Viewing 1 replies (of 1 total)
  • The topic ‘How to create new attribute in woocommerce with webhook automatically’ is closed to new replies.