• Hello Support Team,

    I am currently working on integrating a bulk price update feature into my WooCommerce store, which needs to update both regular and wholesale prices across all products. While I have been successful in updating the regular prices, I’m facing challenges with updating the wholesale prices, which are managed by your WooCommerce Tiered Price Table plugin.

    Here’s a brief overview of what I am trying to accomplish:

    • I have an admin page where I can input a percentage by which to increase or decrease prices.
    • I want this percentage change to apply to both regular and wholesale prices for all products.
    • I am using a batch processing approach to avoid server timeouts for large numbers of products.

    So far, I’ve attempted to use your plugin’s provided classes and methods (\TierPricingTable\PriceManager::getPricingRule) to retrieve and update the pricing rules. However, my AJAX request to trigger this update results in a “critical error” message without further specifics.

    Could you provide guidance or documentation on the correct method to programmatically update wholesale prices via PHP? Specifically, I am looking for the best way to retrieve the current wholesale pricing rules, apply a percentage-based adjustment, and then save these updated prices back to the system.

    For your reference, here is the snippet of code where I’m attempting to update the wholesale prices:

    // Update wholesale prices if the plugin is active and the product has wholesale prices
    if (class_exists('\TierPricingTable\PriceManager')) {
        $pricingRule = \TierPricingTable\PriceManager::getPricingRule($product_id);
        if ($pricingRule && !empty($pricingRule->getRules())) {
            $rules = $pricingRule->getRules();
            $updated_rules = [];
            foreach ($rules as $quantity => $price) {
                $updated_price = ceil($price * $increase_factor * 4) / 4; // rounding up to the nearest 0.25
                $updated_rules[$quantity] = $updated_price;
            }
            $pricingRule->setRules($updated_rules);
            $pricingRule->save();
        }
    }

    If there are any known issues or additional steps required for this process to work correctly, your assistance would be greatly appreciated. Furthermore, if there are specific log files or debugging methods that could reveal more detailed error messages from the plugin, that information would be very helpful as well.

    Thank you for your time and assistance.

    Best regards,

    Alex

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mykola Lukin

    (@bycrik)

    Hello there,

    By “wholesale prices” you mean role-based functionality, right?

    \TierPricingTable\PriceManager::getPricingRule

    This class is responsible for the end pricing for a product. Here you can get the final pricing rules, filtered by role-based rules and global pricing rules.

    If you want to update pricing for specific role, you should either update prices directly in the database like the following:

    <?php
    
    // Example for "wholesale" role
    
    $fixedPricesRules = array(
    10 => 50, // 10 pieces = $50.00 per piece
    20 => 45 // 20 pieces = $45.00 per piece
    );
    
    $percentageBasedRules = array(
    10 => 10, // 10 pieces = 10% discount
    20 => 15 // 20 pieces 15% discount
    );
    
    $tieredPricingType = 'percentage'; // percentage or fixed
    
    // Update
    
    $productId = 999;
    $role = "wholesale";
    
    update_post_meta( $productId, "_{$role}_percentage_price_rules", $percentageBasedRules );
    
    update_post_meta( $productId, "_{$role}_fixed_price_rules", $percentageBasedRules );
    
    update_post_meta( $productId, "_{$role}_tiered_price_rules_type", $tieredPricingType );

    Or use the following class:

     /TierPricingTable\Addons\RoleBasedPricing/RoleBasedPricingRule 

    Example:

    <?php
    
    use \TierPricingTable\Addons\RoleBasedPricing/RoleBasedPricingRule;
    
    $productId = 999;
    $role = "wholesale";
    
    // Example for "wholesale" role
    
    $fixedPricesRules = array(
    10 => 50, // 10 pieces = $50.00 per piece
    20 => 45 // 20 pieces = $45.00 per piece
    );
    
    $percentageBasedRules = array(
    10 => 10, // 10 pieces = 10% discount
    20 => 15 // 20 pieces 15% discount
    );
    
    $tieredPricingType = 'percentage'; // percentage or fixed
    
    $rolePricingRule = RoleBasedPricingRule::build($productId,$role);
    
    $roleBasedPricingRule->setTieredPricingType($tieredPricingType);
    $roleBasedPricingRule->setPercentageTieredPricingRules($percentageBasedRules);
    $roleBasedPricingRule->setFixedTieredPricingRules($fixedPricesRules);
    
    $roleBasedPricingRule->save();
    

    I hope this helps.

    Thread Starter astralex

    (@astralex)

    Hi again!
    I’ve attempted to adjust the wholesale prices using various methods based on the documentation and suggestions from you, but I am facing persistent issues with the wholesale prices not updating.

    Here is a brief overview of what I have tried:

    • Directly updating the meta values for the wholesale pricing rules in the database.
    • Using methods from the \TierPricingTable\Addons\RoleBasedPricing\RoleBasedPricingRule class as suggested in your previous communications.

    Despite these efforts, the updates are not reflecting on the WooCommerce front end. Could you provide guidance on the correct approach or function calls to update wholesale prices effectively? Additionally, if there are any specific hooks or conditions that must be met for these updates to take effect, that information would be greatly helpful.

    Here’s a snippet of the code I’m currently using to update the prices:

    function update_wholesale_prices_directly($product_id, $percentage) {
        $role = 'wholesale';
        $increase_factor = 1 + $percentage / 100;
    
        $fixed_price_rules = get_post_meta($product_id, "_{$role}_fixed_price_rules", true);
        if ($fixed_price_rules && is_array($fixed_price_rules)) {
            foreach ($fixed_price_rules as $quantity => $price) {
                $fixed_price_rules[$quantity] = round($price * $increase_factor, 2);
            }
            update_post_meta($product_id, "_{$role}_fixed_price_rules", $fixed_price_rules);
        }
    
        $percentage_price_rules = get_post_meta($product_id, "_{$role}_percentage_price_rules", true);
        if ($percentage_price_rules && is_array($percentage_price_rules)) {
            foreach ($percentage_price_rules as $quantity => $discount) {
                $percentage_price_rules[$quantity] = max(0, $discount * $increase_factor);
            }
            update_post_meta($product_id, "_{$role}_percentage_price_rules", $percentage_price_rules);
        }
    }

    Any insights or examples you could provide would be greatly appreciated.

    Thank you for your support.

    Plugin Author Mykola Lukin

    (@bycrik)

    Hello @astralex,

    You can check the \TierPricingTable\Addons\RoleBasedPricingpackage to check how tiered pricing saves role-based rules itself.

    I hope this helps

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Programmatically Updating Wholesale Prices’ is closed to new replies.