wordpress WooCommerce中基于用户角色的不同产品变化价格

vwkv1x7d  于 12个月前  发布在  WordPress
关注(0)|答案(1)|浏览(263)

场景

2个类别的7个自定义角色被构建为服务于具有订阅的B2C客户和B2B分销商,它们是:

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

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

  • B2C类别(subcription_tier)中的用户可以查看其角色的折扣价格
  • B2B类别(distributor_tier)中的用户能够看到其角色的折扣价格,但仅限于我们可能为他们提供有竞争力的价格的产品,或告知他们由于我们没有资格提供优质价格而无法分发该产品的文本

取得的成就

通过添加本教程中的代码片段,我已经实现了简单产品的目标。该解决方案仅使我能够通过使用ACF和一些代码片段来完成简单产品的需求,而不是变量。

代码

经过几天的研究,LoicTheAztec的一个很好的解决方案来自this post的见解。然而,我发现自己陷入了建立自定义价格和角色之间的联系(* 我不是工程师,仍在学习如何编码以减少插件的依赖性 *)。

// 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;
}

字符串
自定义空白字段在后端设置,但在前端具有所需分层角色的登录用户看不到它们。
我已经尽了最大的努力来建立正确的逻辑,这是我有限的编码知识所能达到的最远的地方。我知道我一定错过了一些东西(或一些东西)或做错了什么,但我真的不知道在哪里审计以使其正确。
感谢您提前回复。期待您的帮助。

zqry0prt

zqry0prt1#

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

// 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;
}

字符串
应该能用

相关问题