Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vendor own product purchase restriction added #1890

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 68 additions & 26 deletions includes/wc-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
*
* @access public
*
* @param int $post_id
* @param int $post_id
* @param array $data
*
* @return void
* @throws WC_Data_Exception
*/
function dokan_process_product_meta( $post_id, $data = [] ) {
function dokan_process_product_meta( int $post_id, array $data = [] ) {
if ( ! $post_id || ! $data ) {
return;
}

global $wpdb, $woocommerce, $woocommerce_errors;
global $woocommerce_errors;

$product_type = empty( $data['product_type'] ) ? 'simple' : sanitize_text_field( $data['product_type'] );

Expand All @@ -37,11 +38,11 @@
update_post_meta( $post_id, '_product_image_gallery', implode( ',', $attachment_ids ) );
}

// Check product visibility and purchaces note
// Check product visibility and purchases note
$data['_visibility'] = isset( $data['_visibility'] ) ? sanitize_text_field( $data['_visibility'] ) : '';
$data['_purchase_note'] = isset( $data['_purchase_note'] ) ? sanitize_textarea_field( $data['_purchase_note'] ) : '';

// Set visibiliy for WC 3.0.0+
// Set visibility for WC 3.0.0+
$terms = [];

switch ( $data['_visibility'] ) {
Expand All @@ -65,7 +66,7 @@
$terms[] = 'featured';
}

wp_set_post_terms( $post_id, $terms, 'product_visibility', false );
wp_set_post_terms( $post_id, $terms, 'product_visibility' );
update_post_meta( $post_id, '_visibility', $data['_visibility'] );

// Update post meta
Expand Down Expand Up @@ -118,7 +119,7 @@
$attribute_position = array_map( 'absint', $data['attribute_position'] );
$attribute_names_max_key = max( array_keys( $attribute_names ) );

for ( $i = 0; $i <= $attribute_names_max_key; $i ++ ) {
for ( $i = 0; $i <= $attribute_names_max_key; $i++ ) {
if ( empty( $attribute_names[ $i ] ) ) {
continue;
}
Expand Down Expand Up @@ -351,10 +352,10 @@
$files = [];

$file_names = isset( $data['_wc_file_names'] ) ? array_map( 'wc_clean', $data['_wc_file_names'] ) : [];
$file_urls = isset( $data['_wc_file_urls'] ) ? array_map( 'esc_url_raw', array_map( 'trim', $data['_wc_file_urls'] ) ) : [];
aihimel marked this conversation as resolved.
Show resolved Hide resolved
$file_urls = array_map( 'esc_url_raw', array_map( 'trim', $data['_wc_file_urls'] ) );
$file_url_size = count( $file_urls );

for ( $i = 0; $i < $file_url_size; $i ++ ) {
for ( $i = 0; $i < $file_url_size; $i++ ) {
if ( ! empty( $file_urls[ $i ] ) ) {
$files[ md5( $file_urls[ $i ] ) ] = [
'name' => $file_names[ $i ],
Expand Down Expand Up @@ -395,7 +396,7 @@
$sku = trim( wp_unslash( $data['_sku'] ) ) !== '' ? sanitize_text_field( wp_unslash( $data['_sku'] ) ) : '';
try {
$product->set_sku( $sku );
} catch ( \WC_Data_Exception $e ) {
} catch ( WC_Data_Exception $e ) {
$product->set_sku( $old_sku );
$woocommerce_errors[] = __( 'Product SKU must be unique', 'dokan-lite' );
}
Expand All @@ -420,7 +421,7 @@
*
* @return void
*/
function dokan_process_product_file_download_paths( $product_id, $variation_id, $downloadable_files ) {
function dokan_process_product_file_download_paths( int $product_id, int $variation_id, array $downloadable_files ) {
global $wpdb;

if ( $variation_id ) {
Expand Down Expand Up @@ -466,13 +467,13 @@
}

/**
* Get discount coupon total from a order
* Get discount coupon total from an order
*
* @param int $order_id
*
* @return int
*/
function dokan_sub_order_get_total_coupon( $order_id ) {
function dokan_sub_order_get_total_coupon( int $order_id ): int {
global $wpdb;

$result = $wpdb->get_var(
Expand Down Expand Up @@ -501,7 +502,7 @@
* @return string $display_name
*/
function dokan_seller_displayname( $display_name ) {
if ( current_user_can( 'seller' ) && ! is_admin() ) {

Check warning on line 505 in includes/wc-functions.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Found unknown capability "seller" in function call to current_user_can(). Please check the spelling of the capability. If this is a custom capability, please verify the capability is registered with WordPress via a call to WP_Role(s)->add_cap(). Custom capabilities can be made known to this sniff by setting the "custom_capabilities" property in the PHPCS ruleset.
$seller_info = dokan_get_store_info( dokan_get_current_user_id() );
$display_name = ( ! empty( $seller_info['store_name'] ) ) ? $seller_info['store_name'] : $display_name;
}
Expand All @@ -516,7 +517,7 @@
*
* @param int $per_page
*
* @return \WP_Query
* @return WP_Query
*/
function dokan_get_featured_products( $per_page = 9, $seller_id = '', $page = 1 ) {
$args = [
Expand All @@ -543,7 +544,7 @@
*
* @param int $per_page
*
* @return \WP_Query
* @return WP_Query
*/
function dokan_get_latest_products( $per_page = 9, $seller_id = '', $page = 1 ) {
$args = [
Expand All @@ -568,7 +569,7 @@
*
* @param int $per_page
*
* @return \WP_Query
* @return WP_Query
*/
function dokan_get_best_selling_products( $per_page = 8, $seller_id = '', $page = 1, $hide_outofstock = false ) {
$args = [
Expand Down Expand Up @@ -617,13 +618,13 @@
}

/**
* Get top rated products
* Get top-rated products
*
* Shown on homepage
*
* @param int $per_page
*
* @return \WP_Query
* @return WP_Query
*/
function dokan_get_top_rated_products( $per_page = 8, $seller_id = '', $page = 1 ) {
$args = [
Expand All @@ -650,9 +651,9 @@
* @param int $paged
* @param int $seller_id
*
* @return \WP_Query
* @return WP_Query
*/
function dokan_get_on_sale_products( $per_page = 10, $paged = 1, $seller_id = 0 ) {
function dokan_get_on_sale_products( int $per_page = 10, int $paged = 1, int $seller_id = 0 ): WP_Query {
// Get products on sale
$product_ids_on_sale = wc_get_product_ids_on_sale();

Expand Down Expand Up @@ -754,7 +755,7 @@
* A hacky and dirty way to do this from this action. Because there is no easy
* way to do this by removing action hooks from WooCommerce. It would be easier
* if they were from functions. Because they are added from classes, we can't
* remove those action hooks. Thats why we are doing this from the phpmailer_init action
* remove those action hooks. That's why we are doing this from the phpmailer_init action
* by returning a fake phpmailer class.
*
* @param array $attr
Expand Down Expand Up @@ -806,7 +807,7 @@
/**
* A fake mailer class to replace phpmailer
*/
class DokanFakeMailer {

Check failure on line 810 in includes/wc-functions.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

A file should either contain function declarations or OO structure declarations, but not both. Found 29 function declaration(s) and 1 OO structure declaration(s). The first function declaration was found on line 14; the first OO declaration was found on line 810
public function Send() {
}
}
Expand Down Expand Up @@ -1064,16 +1065,15 @@
add_action( 'woocommerce_product_tabs', 'dokan_set_more_from_seller_tab', 10 );

/**
* Show more products from current seller
* Show more products from current seller
*
* @since 2.5
* @since DOKAN_LITE_SINCE added filter 'dokan_get_more_products_per_page'
*
* @param int $seller_id
* @param int $posts_per_page
* @param int|string $seller_id
* @param int|string $posts_per_page
*
* @global object $product
* @global object $post
* @return void
*/
function dokan_get_more_products_from_seller( $seller_id = 0, $posts_per_page = 6 ) {
global $product, $post;
Expand Down Expand Up @@ -1120,7 +1120,7 @@
* @return void
*/
function dokan_bulk_order_status_change() {
if ( ! current_user_can( 'dokan_manage_order' ) ) {

Check warning on line 1123 in includes/wc-functions.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Found unknown capability "dokan_manage_order" in function call to current_user_can(). Please check the spelling of the capability. If this is a custom capability, please verify the capability is registered with WordPress via a call to WP_Role(s)->add_cap(). Custom capabilities can be made known to this sniff by setting the "custom_capabilities" property in the PHPCS ruleset.
return;
}

Expand Down Expand Up @@ -1241,7 +1241,7 @@
return $counts;
}

if ( current_user_can( 'manage_woocommerce' ) ) {

Check warning on line 1244 in includes/wc-functions.php

View workflow job for this annotation

GitHub Actions / Run PHPCS inspection

Found unknown capability "manage_woocommerce" in function call to current_user_can(). Please check the spelling of the capability. If this is a custom capability, please verify the capability is registered with WordPress via a call to WP_Role(s)->add_cap(). Custom capabilities can be made known to this sniff by setting the "custom_capabilities" property in the PHPCS ruleset.
return $counts;
}

Expand All @@ -1265,3 +1265,45 @@
}

add_filter( 'wp_count_posts', 'dokan_modify_vendor_order_counts', 10, 1 );


/**
* @since DOKAN_SINCE
*
* @param boolean $is_purchasable
* @param object $product
*
* @return boolean
*/
function dokan_vendor_own_product_purchase_restriction( bool $is_purchasable, $product ): bool {
if ( false === $is_purchasable || dokan_is_product_author( $product->get_id() ) ) {
$is_purchasable = false;
}
return $is_purchasable;
}

add_filter( 'woocommerce_is_purchasable', 'dokan_vendor_own_product_purchase_restriction', 10, 2 );

/**
* Restricts vendor from reviewing own product
*
* @since DOKAN_SINCE
*
* @param array $data
* @return array
*/
function dokan_vendor_product_review_restriction( array $data ): array {
global $product;
if ( ! is_user_logged_in() ) {
return $data;
}
if ( dokan_is_product_author( $product->get_id() ) ) {
$data['title_reply'] = __( 'Reviews cannot be posted for products that you own.', 'dokan-lite' );
$data['comment_field'] = '';
$data['fields'] = [];
$data['submit_field'] = '';
$data['submit_button'] = '';
}
return $data;
}
add_filter( 'woocommerce_product_review_comment_form_args', 'dokan_vendor_product_review_restriction' );
Loading