简体   繁体   中英

WooCommerce Product Vendors - Assigning Vendor to a product programmatically

Backstory: I am automatically generating a product after a property submission is performed using the Realteo plugin(baked into the Findeo theme). That is working in its basic form but what I'm having trouble with is assigning a Product Vendor automatically to this newly-generated product.

When I look through the Product Vendors code(official one from WooCommerce.com) I believe the plugin stores the vendor ID as product meta data, the key being _wcpv_vendor . However, this doesn't appear to be true; even if I manually edit a product and assign a booking, that key cannot be found when viewing the product's postmeta.

If _wcpv_vendor is blank and no vendor is assigned, manually editing that entry does not assign a vendor to a product.

Example from the code of how it attaches the vendor ID to an attachment:

public function process_attachment( $post_id ) {
    if ( WC_Product_Vendors_Utils::auth_vendor_user() ) {
        update_post_meta( $post_id, '_wcpv_vendor', WC_Product_Vendors_Utils::get_logged_in_vendor() );
    }

    return true;
}

I know that I'm making the assumption here that this works the same for products(which is wrong, I know), but I've tried searching through the Product Vendors plugin code and cannot find where exactly the plugin assigns the Vendor ID to a product when a Vendor is added to a product.

Other quick question: Should it be okay to use WC_Product_Vendors_Utils::get_logged_in_vendor() in this scenario to automatically pull the Vendor ID for a user logged into the front-end of the site? I've been trying it but the returned result is blank.

Can anyone help with this?

After digging at this some more, Vendors does not actually directly update the post meta since a Vendor is actually a taxonomy. Instead, when a post is saved, it automatically saves the Vendor ID using wp_set_object_terms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM