Scenario

构建了两个类别的7个自定义角色,以服务于具有订阅和B2B总代理商的B2C客户,它们是:

  • B2C subcription_tier1/subcription_tier2
  • B2B distributor_tier1/distributor_tier2/distributor_tier3/distributor_tier4/distributor_tier5

当用户登录时,用户可以看到他们的层的不同折扣:

  • B2C类别(subcription_tier)中的用户可以查看其角色的折扣价格
  • B2B类别(DRANSPOR_TIER)中的用户能够看到其角色的折扣价格,但仅限于我们可能为他们提供有竞争力的价格的产品,或者文本通知他们该产品不可用于分销,因为我们没有资格提供高质量的价格

What Has Been Achieved

通过添加本教程中的代码片段,我已经用简单的产品实现了这一目标.该解决方案只能使我的解决方案通过将ACF个和一些代码片段放在一起来实现简单代码的需求,而不是使用可变的代码片段.

The Code

经过几天的研究,LoicTheAztec的伟大解决方案来自this post的洞察力.然而,我发现自己陷入了在定制价格和角色之间建立联系的困境(I'm NOT an engineer and still learning how to code to reduce the dependences for plugins).

// Add custom fields to variations option pricing
add_action( 'woocommerce_variation_options_pricing', 'add_variation_wholesale_options_pricing', 20, 3 );
function add_variation_wholesale_options_pricing( $loop, $variation_data, $post_variation )
{
    $value1  = get_post_meta( $post_variation->ID, '_sp_price_1', true ); // subscription tier 1
    $value2  = get_post_meta( $post_variation->ID, '_sp_price_2', true ); // subscription tier 2
    $value3  = get_post_meta( $post_variation->ID, '_sp_price_3', true ); // distributor tier 1
    $value4  = get_post_meta( $post_variation->ID, '_sp_price_4', true ); // distributor tier 2
    $value5  = get_post_meta( $post_variation->ID, '_sp_price_5', true ); // distributor tier 3
    $value6  = get_post_meta( $post_variation->ID, '_sp_price_6', true ); // distributor tier 4
    $value7  = get_post_meta( $post_variation->ID, '_sp_price_7', true ); // distributor tier 5

    $symbol = ' (' . get_woocommerce_currency_symbol() . ')';

    $key_1 = '_sp_price_1[' . $loop . ']';
    $key_2 = '_sp_price_2[' . $loop . ']';
    $key_3 = '_sp_price_3[' . $loop . ']';
    $key_4 = '_sp_price_4[' . $loop . ']';
    $key_5 = '_sp_price_5[' . $loop . ']';
    $key_6 = '_sp_price_6[' . $loop . ']';
    $key_7 = '_sp_price_7[' . $loop . ']';

    // Field Label For Subscription Tier 1
    echo '<div><p class="form-row form-row-first">
        <label>' . __( "Subscription Tier 1", "woocommerce" ) . $symbol . '</label>
        <input type="text" size="5" name="' . $key_1 .'" value="' . esc_attr( $value1 ) . '" />
    </p></div>';

    // Field Label For Subscription Tier 2
    echo '<div><p class="form-row form-row-first">
        <label>' . __( "Subscription Tier 2", "woocommerce" ) . $symbol . '</label>
        <input type="text" size="5" name="' . $key_2 .'" value="' . esc_attr( $value2 ) . '" />
    </p></div>';

    // Field Label For Distributor Tier 1
    echo '<div><p class="form-row form-row-first">
        <label>' . __( "Distributor Tier ", "woocommerce" ) . $symbol . '</label>
        <input type="text" size="5" name="' . $key_3 .'" value="' . esc_attr( $value3 ) . '" />
    </p></div>';

    // Field Label For Distributor Tier 2
    echo '<div class="variable_wholesale-price"><p class="form-row form-row-first">
        <label>' . __( "Distributor Tier 2", "woocommerce" ) . $symbol . '</label>
        <input type="text" size="5" name="' . $key_4 .'" value="' . esc_attr( $value4 ) . '" />
    </p></div>';

    // Field Label For Distributor Tier 3
    echo '<div class="variable_wholesale-price"><p class="form-row form-row-first">
        <label>' . __( "Distributor Tier 3", "woocommerce" ) . $symbol . '</label>
        <input type="text" size="5" name="' . $key_5 .'" value="' . esc_attr( $value5 ) . '" />
    </p></div>';

    // Field Label For Distributor Tier 4
    echo '<div class="variable_wholesale-price"><p class="form-row form-row-first">
        <label>' . __( "Distributor Tier 4", "woocommerce" ) . $symbol . '</label>
        <input type="text" size="5" name="' . $key_6 .'" value="' . esc_attr( $value6 ) . '" />
    </p></div>';

    // Field Label For Distributor Tier 5
    echo '<div class="variable_wholesale-price"><p class="form-row form-row-first">
        <label>' . __( "Distributor Tier 5", "woocommerce" ) . $symbol . '</label>
        <input type="text" size="5" name="' . $key_7 .'" value="' . esc_attr( $value7 ) . '" />
    </p></div>';
}


// Save variations wholesale prices custom fields values
add_action( 'woocommerce_save_product_variation', 'save_product_variation_wholesale_price', 20, 2 );
function save_product_variation_wholesale_price( $variation_id, $i )
{
    if ( isset( $_POST['_sp_price_1'][$i] ) )
    {
        update_post_meta( $variation_id, '_sp_price_1', floatval( $_POST['_sp_price_1'][$i] ) );
    }
}

// Variable product price range
add_filter('woocommerce_variation_prices_price', 'wholesale_variation_price', 900, 2 );
add_filter('woocommerce_variation_prices_sale_price', 'wholesale_variation_price', 900, 2 );

// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_price', 'wholesale_variation_price', 900, 2 );
add_filter('woocommerce_product_variation_get_sale_price', 'wholesale_variation_price', 900, 2 );
function wholesale_variation_price( $price, $object )
{
    if ( is_user_logged_in() ) {

        // For Wholesale user levels 1 and 2
        if( in_array($level, [1]) ) {
            $new_price = (float) get_post_meta( $object->get_id(), '_sp_price_1', true );
            $price     = empty($new_price) ? $price : $new_price;
        }

    }
    return $price;
}

// Handling custom variable price range caching
add_filter( 'woocommerce_get_variation_prices_hash', 'wholesale_variation_performances_caching_prices', 99, 1 );
function wholesale_variation_performances_caching_prices( $hash ) {
    if ( is_user_logged_in() ) {
        $level = (int) get_user_meta( get_current_user_id(), 'mokeeper_keen', true );
        // For Wholesale user levels 1 and 2
        if( in_array($level, [1]) ) {
            $hash[] = 'level_' . $level;
        }
        // For customers
        else {
            $hash[] = 'level_0'; // Set the user level to zero here
        }
    }
    return $hash;
}

自定义空白字段是在后端设置的,但在前端具有所需分层角色的登录用户看不到它们.

我已经尽了最大的努力来建立正确的逻辑,这是我有限的编码知识所能达到的最远的地方.我知道我一定是错过了什么(或一些事情)或做错了什么,但我真的不知道在哪里审计,使其正确.

感谢您提前回复.期待您的帮助.

推荐答案

也可以try 以下处理用户角色的优化和完整的代码版本:

// Field settings (field key / label name)
function custom_field_settings() {
    return array(
        '_st_price_1' => __( "Subscription Tier 1", "woocommerce" ),
        '_st_price_2' => __( "Subscription Tier 2", "woocommerce" ),
        '_dt_price_1' => __( "Distributor Tier 1", "woocommerce" ),
        '_dt_price_2' => __( "Distributor Tier 2", "woocommerce" ),
        '_dt_price_3' => __( "Distributor Tier 3", "woocommerce" ),
        '_dt_price_4' => __( "Distributor Tier 4", "woocommerce" ),
        '_dt_price_5' => __( "Distributor Tier 5", "woocommerce" ),
    );
}

// Settings: custom field keys by user role
function custom_field_key_by_user_role() {
    return array(
        '_st_price_1' => 'subcription_tier1',
        '_st_price_2' => 'subcription_tier2',
        '_dt_price_1' => 'distributor_tier1',
        '_dt_price_2' => 'distributor_tier2',
        '_dt_price_3' => 'distributor_tier3',
        '_dt_price_4' => 'distributor_tier4',
        '_dt_price_5' => 'distributor_tier5',
    );
}

// Add custom fields to variations option pricing
add_action( 'woocommerce_variation_options_pricing', 'add_variation_custom_options_pricing', 20, 3 );
function add_variation_custom_options_pricing( $loop, $variation_data, $post_variation ) {
    $symbol  = ' (' . get_woocommerce_currency_symbol() . ')';

    // Loop through field key / label name pairs
    foreach ( custom_field_settings() as $key => $label ) {
        printf( '<div><p class="form-row form-row-first"> <label>%s</label>
        <input type="text" size="5" name="%s[%s]" value="%s" /></p></div>', 
        $label.$symbol, $key, $loop, floatval(get_post_meta($post_variation->ID, $key, true)) );
    }
}

// Save variations prices custom fields values
add_action( 'woocommerce_admin_process_variation_object', 'save_variation_custom_options_pricing', 20, 2 );
function save_variation_custom_options_pricing( $variation, $i ) {
    // Loop through field key / label name pairs
    foreach ( custom_field_settings() as $key => $label ) {
        if ( isset( $_POST[$key][$i] ) ){
            $variation->update_meta_data( $key, floatval($_POST[$key][$i]) );
        }
    }
}

// Variable product price range
add_filter('woocommerce_variation_prices_price', 'wholesale_variation_price', 900, 2 );
add_filter('woocommerce_variation_prices_sale_price', 'wholesale_variation_price', 900, 2 );
// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_price', 'wholesale_variation_price', 900, 2 );
add_filter('woocommerce_product_variation_get_sale_price', 'wholesale_variation_price', 900, 2 );
function wholesale_variation_price( $price, $object ) {
    global $current_user;

    if ( ! is_user_logged_in() ) return $price;

    // Loop through field key / user role pairs
    foreach ( custom_field_key_by_user_role() as $key => $role ) {
        if( in_array($role, $current_user->roles) ) {
            $new_price = (float) $object->get_meta($key);
            $price     = empty($new_price) ? $price : $new_price;
        }
    }
    return $price;
}

// Handling custom variable price range caching
add_filter( 'woocommerce_get_variation_prices_hash', 'wholesale_variation_performances_caching_prices', 99, 1 );
function wholesale_variation_performances_caching_prices( $hash ) {
    global $current_user;

    if ( ! is_user_logged_in() ) return $hash;

    // Loop through field key / user role pairs
    foreach ( custom_field_key_by_user_role() as $key => $role ) {
        if( in_array($role, $current_user->roles) ) {
            $hash[] = $key;
        }
    }
    return $hash;
}

应该能行得通.

Php相关问答推荐

如何在PHP中出现弃用警告时触发错误?

如何保存基于用户 Select 的复选框 Select 模式

根据WooCommerce中的客户计费国家/地区更改产品价格

如何使用索引加速和优化MySQL搜索

SFTP在PHP(phpseclib3\Net\SFTP)-过滤目录列表结果服务器端可能吗?类似于psftp中的LS命令?

如果WooCommerce购物车在 checkout 时被清空,请重定向至store 页面

在PHP项目中安装OpenAI PHP SDK时出现问题

Regex|PHP捕获json字符串中的每个非法双引号

如何使用 PHP 创建简化的 twig 替换?

Symfony:从控制器内部调用自定义命令

根据小计向 WooCommerce Checkout 添加佣金

批量更新 WooCommerce 中所有产品的特定自定义字段

如何为自定义帖子类型的自定义角色提供功能

安装 Herd 并返回 Valet 后为 502

带有分类术语数组的 WordPress Elementor 自定义查询 - 如何要求所有术语都在返回的帖子中

htaccess Rewriterule 无法以任何方式工作

从图像URL中获取文件扩展名?

在WooCommerce可变产品中显示所选变体的自定义字段

Laravel 查询关系是否为 bool

遇到特定键时修改二维数组以创建嵌套数据集