Support » Plugin: Import any XML or CSV File to WordPress » Divide and round price by value of other element or field

  • Resolved mikeeverett

    (@mikeeverett)


    Hello

    I need to divide a per week rental price by the number of bedrooms in the property. (this will give me a per person per week price)

    Number of bedrooms are imported from element {TotalBedrooms[1]}.

    Using a code snippet from WPAllImport that allowed me to do another calculation on a previous import, I have created the following and would like to know if this will work, or if there’s a better way to do this.

    PHP FUNCTION:

    <?php
    function round_price( $price = null, $divider = {TotalBedrooms[1]}, $nearest = .10, $minus = 0 ) {
        if ( !empty( $price ) ) {
    		// strip any extra characters from price
    		$price = preg_replace("/[^0-9,.]/", "", $price);
    		// perform calculations
            return ( round ( ( $price / $divider ) / $nearest ) * $nearest ) - $minus; 
        }
    }
    

    PRICE FIELD:

    [round_price({WeeklyRent[1]},”{TotalBedrooms[1]}”,”10″,”.01″)]

    Can anyone help with this?

    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author WP All Import

    (@wpallimport)

    Hi @mikeeverett,

    Using a code snippet from WPAllImport that allowed me to do another calculation on a previous import, I have created the following and would like to know if this will work, or if there’s a better way to do this.

    You can’t reference the import element {TotalBedrooms[1]} in the list of arguments like that, and I’d actually suggest just making your own custom function for this. Here’s an example:

    function my_div_price( $price, $beds ) {
        $price = preg_replace( "/[^0-9,.]/", "", $price );
        
        $div_price = $price / $beds;
        return round( $div_price, 2 );
        
    }

    You’d pass both the price and the bedroom elements to the function like so:

    [my_div_price({WeeklyRent[1]},{TotalBedrooms[1]})]

    Let me know if you have any questions.

    Plugin Author WP All Import

    (@wpallimport)

    Hey @mikeeverett,

    I’m marking this thread as resolved since it’s been inactive for a while now. Please open up a new topic if you still have questions.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Divide and round price by value of other element or field’ is closed to new replies.