WordpressWoocommerce Classes and adding new properties

admin2025-05-31  0

This is more a general question about classes, I am experimenting with small additions to Woocommerce via custom plugins and wanted to look at vendor functionality. To do this I imagine I will need to add a new property to the WC_Product class such as vendor (I believe this would become $product -> vendor; at object level)

Am I able to add new properties and methods to classes such as the WC_Product class without changing the Woocommerce core files?

My plan ultimately is to look at setting

$user = get_current_user_id();
$product -> vendor = $user;

That way I can set the additional property for the user that created the product, which we can refer to later when dealing with which users can edit which products etc.

This is more a general question about classes, I am experimenting with small additions to Woocommerce via custom plugins and wanted to look at vendor functionality. To do this I imagine I will need to add a new property to the WC_Product class such as vendor (I believe this would become $product -> vendor; at object level)

Am I able to add new properties and methods to classes such as the WC_Product class without changing the Woocommerce core files?

My plan ultimately is to look at setting

$user = get_current_user_id();
$product -> vendor = $user;

That way I can set the additional property for the user that created the product, which we can refer to later when dealing with which users can edit which products etc.

Share Improve this question edited Mar 18, 2019 at 21:40 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Mar 16, 2019 at 9:44 Dan SutherlandDan Sutherland 1732 gold badges2 silver badges11 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

This might not answer your question directly or fully. These are just some thoughts / ways to get the thing done, which came into my mind after reading your question.

I don't know if WC_Product can be modified directly, but you can at least extend it and create a custom product type. E.g. WC_Vendor_Product extends WC_Product. You would then add the custom properties and methods to your custom type class.

Another way that comes into my mind is to use a custom taxonomy for the vendors. They could then be created as terms with some custom meta data added to the terms.

Vendors could also be users or CPT. In this case you'd create a term for each user/post and use the user/post ID as the term slug (wp_set_object_terms). This way the term would only act as a medium between the product and the vendor, and you could do tax_queries for the products with the user/post ID (used as string).

Or if users are vendors then you could use the author field of the products to connect products with users.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748624445a313605.html

最新回复(0)