阻止 post-作者在 WooCommerce 中购买和竞标他们自己的产品
Prevent post-authors from buying and bidding their own products in WooCommerce
对于 WooCommerce,我正在使用 WC Vendors Pro 插件 + Rehub 主题 + 拍卖插件(wp giene)+ Simple Auctions 插件(WC Vendors)。
如何防止 post 作者在 WooCommerce 中购买和竞标他们自己的产品?
我不希望 post 作者对他们 post 编辑的产品出价,以防止价格上涨(这是不正确的)。
从哪里开始,有什么想法吗?
谢谢
我想您的 "post authors"(供应商)具有自定义角色(或某些特定功能)。因此,您可以针对用户角色 "post authors"(或他的特定功能之一)检查并删除结帐页面上的购物车项目。
代码如下:
add_action( 'woocommerce_before_checkout_form', 'checking_allowed_cart_items_on_checkout', 100, 1);
function checking_allowed_cart_items_on_checkout() {
// Allow only "administrator" and "customer" user roles to buy all products
if( ! current_user_can('administrator') || ! current_user_can('customer') ){
// Ititializing variables
$found = false;
$cart_object = WC()->cart;
$current_user_id = get_current_user_id();
// Checking cart items
foreach($cart_object->get_cart() as $cart_item_key => $cart_item){
// get the WC_Product object and the product ID
$product = $cart_item['data'];
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Get the product post object to get the post author
$post_obj = get_post( $product_id );
$post_author = $post_obj->post_author;
if( $post_author == $current_user_id ){
// removing the cart item
$cart_object->remove_cart_item( $cart_item_key );
$found = true; // set to true if a product of this author is found
}
}
// Display a custom message in checkout page when items are removed
if( $found ){
// Display an error message
wc_add_notice( __( 'Items removed - As a Vendor, you are not allowed to buy your own products.' ), 'error' );
}
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。
对于 WooCommerce,我正在使用 WC Vendors Pro 插件 + Rehub 主题 + 拍卖插件(wp giene)+ Simple Auctions 插件(WC Vendors)。
如何防止 post 作者在 WooCommerce 中购买和竞标他们自己的产品?
我不希望 post 作者对他们 post 编辑的产品出价,以防止价格上涨(这是不正确的)。
从哪里开始,有什么想法吗?
谢谢
我想您的 "post authors"(供应商)具有自定义角色(或某些特定功能)。因此,您可以针对用户角色 "post authors"(或他的特定功能之一)检查并删除结帐页面上的购物车项目。
代码如下:
add_action( 'woocommerce_before_checkout_form', 'checking_allowed_cart_items_on_checkout', 100, 1);
function checking_allowed_cart_items_on_checkout() {
// Allow only "administrator" and "customer" user roles to buy all products
if( ! current_user_can('administrator') || ! current_user_can('customer') ){
// Ititializing variables
$found = false;
$cart_object = WC()->cart;
$current_user_id = get_current_user_id();
// Checking cart items
foreach($cart_object->get_cart() as $cart_item_key => $cart_item){
// get the WC_Product object and the product ID
$product = $cart_item['data'];
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Get the product post object to get the post author
$post_obj = get_post( $product_id );
$post_author = $post_obj->post_author;
if( $post_author == $current_user_id ){
// removing the cart item
$cart_object->remove_cart_item( $cart_item_key );
$found = true; // set to true if a product of this author is found
}
}
// Display a custom message in checkout page when items are removed
if( $found ){
// Display an error message
wc_add_notice( __( 'Items removed - As a Vendor, you are not allowed to buy your own products.' ), 'error' );
}
}
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效。