Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'woocommerce_product_import_before_process_item', function($data) {
- if(empty($data['category_ids'])) return;
- if(wcfm_is_vendor()) {
- $vendor_id = apply_filters('wcfm_current_vendor_id', get_current_user_id());
- } elseif(isset($data['store'])) {
- $vendor_id = $data['store'];
- } elseif(isset($data['meta_data'])) {
- foreach($data['meta_data'] as $meta) {
- if($meta['key'] == '_wcfm_product_author') {
- $vendor_id = $meta['value'];
- break;
- }
- }
- }
- if($vendor_id) {
- $cache_key = 'wcfm-vendor-capabilities-' . $vendor_id;
- $vendor_capability_options = wp_cache_get( $cache_key, 'wcfm-vendor-capabilities' );
- if( empty( $vendor_capability_options ) ) {
- $vendor_capability_options = (array) apply_filters( 'wcfmgs_user_capability', get_option( 'wcfm_capability_options' ), $vendor_id );
- wp_cache_set( $cache_key, $vendor_capability_options, 'wcfm-vendor-capabilities' );
- }
- $allowed_categories = $vendor_capability_options['allowed_categories'] ?? [];
- if(empty($allowed_categories)) return;
- foreach($data['category_ids'] as $cat_id) {
- $category = get_term($cat_id, 'product_cat');
- if (is_wp_error($category) || !$category) {
- continue;
- }
- if ($category->parent == 0 && in_array($cat_id, $allowed_categories)) {
- return;
- }
- while ($category->parent != 0) {
- $category = get_term($category->parent, 'product_cat');
- }
- if(in_array($category->term_id, $allowed_categories)) {
- return;
- }
- }
- throw new Exception( 'category not allowed' );
- }
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement