第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將自定義字段價格作為產品價格分配給 WooCommerce 中的特定用戶角色

將自定義字段價格作為產品價格分配給 WooCommerce 中的特定用戶角色

PHP
翻翻過去那場雪 2023-06-18 16:23:54
我創(chuàng)建了一個名為“批發(fā)商”的新角色。該角色按預期工作。我還創(chuàng)建了一個名為“批發(fā)商價格”的自定義價格字段。它也按預期工作。我檢查用戶角色,如果他們被分配為批發(fā)商,我給他們定制產品價格(如果已填寫)。除了我無法弄清楚最后一塊,我一切正常。如何拉取當前產品ID,獲取批發(fā)價,并分配。我的 functions.php 代碼如下:/* Custom user roles */add_role( 'wholesaler', 'Wholesaler', get_role( 'customer' )->capabilities );add_action( 'woocommerce_product_options_pricing', 'action_woocommerce_product_options_pricing', 10, 0 );/* Add custom wholesaler price field to general page */function action_woocommerce_product_options_pricing() {     woocommerce_wp_text_input( array(        'id' => 'wholesaler_price',         'class' => 'wc_input_price short',         'label' => __( 'Wholesaler price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',    ) );}// Save Fieldsfunction action_woocommerce_admin_process_product_object( $product ) {    if( isset($_POST['wholesaler_price']) ) {        $product->update_meta_data( 'wholesaler_price', sanitize_text_field( $_POST[ 'wholesaler_price'] ) );    }}add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );/* Custom prices by user role */add_filter('woocommerce_get_price', 'custom_price_assign', 10, 2);function custom_price_assign($price, $product) {    if (!is_user_logged_in()) return $price;        // Check if the user has a role of wholesaler        if (check_user_role('wholesaler')){            $price = $price; // Assign wholesale price for product (if it is set for that product)        }    return $price;}/* Check user role */function check_user_role($role = '',$user_id = null){    if ( is_numeric( $user_id ) )        $user = get_user_by( 'id',$user_id );    else        $user = wp_get_current_user();    if ( empty( $user ) )        return false;    return in_array( $role, (array) $user->roles );}我可以給批發(fā)商一個固定百分比的報價,但這不是我的目標。
查看完整描述

1 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

你不需要你的功能check_user_role(),因為你可以用同樣的方式使用 WordPress?current_user_can()

自 WooCommerce 3 以來,該掛鉤也woocommerce_get_price已棄用。

要獲得自定義批發(fā)商價格,您只需使用元鍵,wholesaler_price例如:

1) 使用對象get_meta()上的方法(自 WooCommerce 3 起)WC_Product

$price?=?(float)?$product->get_meta(?'wholesaler_price'?);

get_post_meta()2) 或者在功能中使用產品 ID?(WordPress 舊方法)

$price?=?(float)?get_post_meta(?$product->get_id()??'wholesaler_price',?true?);

現在在你相關的鉤子代碼函數中:

/* Custom prices by user role */

add_filter('woocommerce_product_get_price', 'custom_price_assign', 10, 2);

add_filter('woocommerce_product_variation_get_price', 'custom_price_assign', 10, 2); // For product variations (optional)


function custom_price_assign( $price, $product ) {

? ? // Check if the user has a role of wholesaler

? ? if ( current_user_can('wholesaler') && $wholesaler_price = $product->get_meta('wholesaler_price') ){

? ? ? ? return $wholesaler_price;

? ? }

? ? return $price;

}

代碼進入您的活動子主題(或活動主題)的 functions.php 文件。測試和工作。


查看完整回答
反對 回復 2023-06-18
  • 1 回答
  • 0 關注
  • 136 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號