Okay
  Public Ticket #3731303
Auto discount for specific products
Closed

Comments

  •  2
    Lukasz started the conversation

    How can I set auto discount for specific product buying by agent as a customer?

    For example:

    Agent is being the product for his client. 

    Basic price is 50%, Agent has 10$ commission and client has 5$ discount on product

    if client is buying on hisself - there is no discount.

    I was tryin with plugin Role Based Pricing for WooCommerce, and set fixed price of salesagent user role,  but it only works when agent is making order for his own.


  •  2,212
    WebWizards replied

    Hello Lukasz,

    In PHP, we have a function that can return true or false depending on whether it is an agent buying for a customer. It is:

    function is_agent_shopping(){
    	// check if switch cookie is set
    	if (isset($_COOKIE['salesking_switch_cookie'])){
        	$switch_to = sanitize_text_field($_COOKIE['salesking_switch_cookie']);
        	$current_id = get_current_user_id();
    
        	if (!empty($switch_to) && is_user_logged_in()){
        		// show bar
    			$udata = get_userdata( get_current_user_id() );
    			$name = $udata->first_name.' '.$udata->last_name;
    
    			// get agent details
    			$agent = explode('_',$switch_to);
    			$customer_id = intval($agent[0]);
    			$agent_id = intval($agent[1]);
    			$agent_registration = $agent[2];
    			// check real registration in database
    			$udataagent = get_userdata( $agent_id );
                $registered_date = $udataagent->user_registered;
    
                // if current logged in user is the one in the cookie + agent cookie checks out
                if ($current_id === $customer_id && $agent_registration === $registered_date){
                	return true;
                }
            }
        }
        return false;
    }

     

    We would need a hook / filter / code snippet to basically inactivate or activate a discount programmatically.

    If we had such a hook in the role-based pricing plugin, we could apply the above function to disable the discount if the user is not an agent shopping.

     

    Do you have access to support for the 'Role Based Pricing for WooCommerce' plugin? If so, could you ask their team whether there's any way to enable or disable a discount programmatically?

     

    Kind regards,

    Stefan

  •  2
    Lukasz replied

    HI i have response from 'Role Based Pricing for WooCommerce'  support. Can You help me now.

    We use  add_filter('woocommerce_get_price_html', array( $this, 'af_csp_custom_price_html' ), 100, 2);   hook to change the prices according to plugin configuration settings.

     Below is the code we are using for getting the user roles.

    if ( empty( $user ) && 'guest' == $user_role ) {

    if ( is_user_logged_in() ) {

    $user = wp_get_current_user();

    if ( $user ) {

    $user_role = current( $user->roles );

    }

    } else {

    $user_role = 'guest';

    }

    Below is the code in which we change the price.

     We did not change the prices in the database.

     public function get_product_price_html( $product, $replace_original ) {

    $tax_display_mode = get_option( 'woocommerce_tax_display_shop' );

    $active_price     = 'incl' === $tax_display_mode ?  wc_get_price_including_tax( $product ) : wc_get_price_excluding_tax( $product );

    $args             = array('price' => $product->get_regular_price() );

    $regular_price    = 'incl' === $tax_display_mode ?  wc_get_price_including_tax( $product, $args ) : wc_get_price_excluding_tax( $product, $args );

    if ( $active_price >= $regular_price ) {

    return wc_price( $active_price ) . $product->get_price_suffix();

    }

    if ( $replace_original ) {

    return wc_price( $active_price ) . $product->get_price_suffix();

    }

    return wc_format_sale_price( wc_price( $regular_price ), wc_price( $active_price ) ) . $product->get_price_suffix();

    }

  •  2,212
    WebWizards replied

    Hi Lukasz,

    Sorry, this code is not very helpful, because this only modifies the price html display. This does not affect the actual price, just the HTML / formatting display of the price, so it doesn't actually change the price in any way (it's just visual, doesn't change the price in cart).

    Maybe they misunderstood the request. We need a hook to actually stop their plugin from changing the price. 

    Could you send me the .zip of their plugin? I can try to check the code directly.

  •   Lukasz replied privately
  •  2,212
    WebWizards replied

    Thank you,

    I looked over their plugin's code. Unfortunately I do not see any hook or filter we can use to modify price.

    Would it be possible for them to add a filter, as in this image?

    8248829630.png

    If they can add that, I believe we can use it to set that a specific rule is not valid, unless the current user is an agent or agent buying as customer.

     

    Kind regards,

    stefan

  •  2
    Lukasz replied

    I can add it manually, can You help me then ?

  •  2,212
    WebWizards replied

    We can add that code manually. The only concern then is that it would be lost when making a plugin update. Therefore if we make that change, the plugin cannot be updated again, or we would lose this functionality.

     

    If that is fine, certainly we can send you a snippet to make it work with that filter I described. Let me know if you would like to proceed that way.