• Resolved zer0type

    (@zer0type)


    Hi there, we are using this plugin as intended. But we want to change pricing by role, programmatically. Do you have any snippets of code, or functions we can use?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author meowcrew

    (@meowcrew)

    Hi there,

    That’s a bit generic question to answer. What you mean by “change pricing by role, programmatically”? The plugin already changes the price based on global or product-level pricing rules, what else are you trying to do with it? Do you want to apply role-based pricing rules (global or product-level) stored in the database to some other parts that the plugin does not affect? Or do you plan to modify the price adjustment process, add more steps to price formation, etc? Or you want to create your own type of rule to then adjust price?
    Please elaborate on what exactly you are trying to achieve.

    Thread Starter zer0type

    (@zer0type)

    Hi meowcrow, thanks for the reply.
    Basically, we are integrating woocommerce to an API which involves updating product details and pricing. What i mean by programmatically is that, e.g. we have Product A, with a custom user role, Wholesaler; do we have a shortcode or a snippet of a code to update the regular/sale price of the custom role for the product using code (PHP)?

    • This reply was modified 1 year, 3 months ago by zer0type.
    Plugin Author meowcrew

    (@meowcrew)

    Hey,

    Sorry, it’s still not clear what is your use case and how the API involved. Basically how Role Role-based Pricing plugin works: it gives you UI to create pricing rules, store them in the database, then retrieve values from the db and apply them to products when the user with the corresponding user role visits the store. Doesn’t matter what page the user visits (product page, archive, or post where you simply display the product with shortcode), they will see their own adjusted price. What are you trying to change in this process? At what stage do you plan to ”?update the regular/sale price of the custom role”? And what is “update” in your case? Do you plan to replace regular/sale price for user role values in db at some point or what exactly? Or you plan to generate role-based pricing rules for product w/o interacting with UI, but using some function?

    Thread Starter zer0type

    (@zer0type)

    Hi,

    So basically, we are updating woocommerce product details and pricing, with information coming from an API through a script running on a cronjob. Basically, our aim is to update the price of a product in the DB by script/code.

    if we can use a line like this to set a products regular price in woocommerce,

    $product->set_price( $price );

    do we have a shortcode or function like this to set the regular/sale price of a custom role we’ve created, by code?



    Plugin Author meowcrew

    (@meowcrew)

    Hi there,

    Well, it’s a bit complicated and there’s no one hook that you can use to create whole rule. Since pricing rule should contain: product id, user role (or user account), type of pricing (flat or percentage), pricing values (regular+sale price or % discount), and if you need them – qty values (min, max, qty step).

    We wrote you a code snippet that you may use to create role-based pricing rule:

    		<?php
        
        // Some product
    		$productId = 10;
    		
    		// Type of product pricing rules we operate.
    		$rulesType = 'role'; // Can be either "role" or "customer"
    		
    		/**
    		 * Create role-based (or customer-based) pricing rule
    		 */
    		$customPricingRule = new \MeowCrew\RoleAndCustomerBasedPricing\Entity\PricingRule( 'percentage' ); // Can be either "flat" or "percentage";
    		
    		$customPricingRule->setRegularPrice( 100 ); // Will not be used, as pricing type set to percentage.
    		$customPricingRule->setSalePrice( 90 ); // Will not be used, as pricing type set to percentage.
    		
    		$customPricingRule->setDiscount( 10 ); // 10% discount
    		
    		$customPricingRule->setMinimum( 10 );
    		$customPricingRule->setMaximum( 999 );
    		$customPricingRule->setGroupOf( 2 );
    		
    		// Role slug we add the rule for
    		$role = 'administrator';
    		
    		// Get all product pricing rules (role-based)
    		$productPricingRules =  \MeowCrew\RoleAndCustomerBasedPricing\Admin\ProductPage\PricingRulesManager::getProductPricingRules( $productId, $rulesType );
    		
    		// Add our custom pricing rule
    		$productPricingRules[ $role ] = $customPricingRule;
    		
    		// Update all pricing rules
    		\MeowCrew\RoleAndCustomerBasedPricing\Admin\ProductPage\PricingRulesManager::updateProductPricingRules( $productId, $productPricingRules, $type );

    Here is formatted better: https://codeshare.io/yogr3n

    Let me know if you have any questions on this. Cheers!

    Thread Starter zer0type

    (@zer0type)

    Thank you meowcrew! This worked perfectly with a slight modification to the last line

    $rulesType instead of $type
        \MeowCrew\RoleAndCustomerBasedPricing\Admin\ProductPage\PricingRulesManager::updateProductPricingRules( $sku, $productPricingRules, $rulesType );
    Plugin Author meowcrew

    (@meowcrew)

    Oh, yeah, that should have been $rulesType.

    Glad you figured that out. Have a good one!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Change Pricing by Code’ is closed to new replies.