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相关问答推荐

Laravel 10查询中的多个where子句

PHP DOMDocument忽略第一个表S结束标记

在ShipStation for WooCommerce中使用自定义订单项目元数据

WooCommerce购物车中仅允许一个可下载产品

自定义WooCommerce帐户下载列表

RediSearch PHP希望我在每次使用时定义所有字段

在WooCommerce中禁用特定日期之前的免费送货

如何使用这种格式构建XML?

将购买限制在WooCommerce中具有相同产品属性的项目

根据产品类型显示或隐藏WooCommerce管理产品自定义字段

为什么在我的PHP联系人脚本中开机自检后出现未定义变量错误?

如果为布尔值,则 for each 循环重新启动

是否使用联系人表单7 wpcf7_Feedback_Response输出答复中的html?

在WooCommerce中以编程方式添加特定产品后,将价格设置为零

从 Laravel 机制中排除目录并直接访问该目录

如何搜索和替换 XML 文件中的一段文本?

在WooCommerce中显示 checkout 的高级自定义字段(Advanced Custom Fields)并保存其值

Laravel Docker 几个数据库

Eloquent 的模型更新了它应该 laravel php 的更多行

基于其他数组创建多维数组 struct