Problems, need help? Have a tip or advice? Post it here.
10 posts Page 1 of 1
Hi Couchers,
I have being learning Couch a lot lately and I need help implementing a feature.
I want to do this:
I am using couch shopping feature. The whole process starts with getting some products from suppliers and delivering them to buyers so I earn some commission.
- When adding the products at the backend, I want to enter the original price of the item but on page save I want the pp_price to be changed to (10% of the pp_price + pp_price). Calculating the final pp_price manually might be a lot of work especially if I am to enter 1000s of products.

Any lead on how to achieve this? Thank you all. Happy couching. :D
In my opinion, if you create some code to change the same input, then it will be recalculated every save, which is not desired. Therefore, it is better to have 2 editable fields - original price and pp_price, where you enter the price into original price and attached javascript fills original price +10% inside pp_price's input.
As @trendoman suggested, it'd be better to use two separate fields.
However, if you still prefer to go with your existing setup try using the following (place it in your 'couch/addons/kfunctions.php' file) -
Code: Select all
$FUNCS->add_event_listener( 'page_prevalidate', function(&$fields, &$pg){
    global $FUNCS, $CART;
    if( $pg->tpl_name !== $CART->config['tpl_products'] ) return; // only interested in products page

    $f = $pg->_fields['pp_price'];
    if( $f->modified ){
        if( !$FUNCS->is_error( KFuncs::validate_non_zero_numeric($f)) ){
            $percent_increase = 10;
            $pp_price = trim( $f->get_data() );
            $increase = round( ($percent_increase/100)*$pp_price, 2 );
            $new_price = $pp_price + $increase;

            $f->store_posted_changes( $new_price );
        }
    }
});

In case you decide to use two fields, it shouldn't be difficult to tweak the code above a little to fetch the value from first field and then post the modified value into the second.

@trendoman - by checking for the 'modified' attribute (as we do above), we can avoid calculating a new value at each save. However, I'd still prefer using two fields just to keep things in perspective.


Hope this helps.
Thanks trendoman and KK for the heads-up. I will try your solution and let you know the outcome.

Yes iny current products clonable template I have an editable region named cost_price but was finding it difficult doing the calculations into the pp_price. Neber knew I could use JavaScript/jQuery inside the template tags.
Hi KK,
I got an Error that read:
ERROR: function add_event_listener(); "page_prevalidate" - listener function is not callable
Which version of Couch are you using??

Anyway, please use the following version -
Code: Select all
$FUNCS->add_event_listener( 'page_prevalidate', 'add_percentage_to_price' );
function add_percentage_to_price(&$fields, &$pg){
    global $FUNCS, $CART;
    if( $pg->tpl_name !== $CART->config['tpl_products'] ) return; // only interested in products page

    $f = $pg->_fields['pp_price'];
    if( $f->modified ){
        if( !$FUNCS->is_error( KFuncs::validate_non_zero_numeric($f)) ){
            $percent_increase = 10;
            $pp_price = trim( $f->get_data() );
            $increase = round( ($percent_increase/100)*$pp_price, 2 );
            $new_price = $pp_price + $increase;

            $f->store_posted_changes( $new_price );
        }
    }
};

It worked. Thank you very very much. I'm using version 2.0. will the previous solution work in 2.2?
You are welcome :)
As for the previous code, it required at least PHP 5.3 (which is mandatory by the later versions of Couch).
No difference in functionality so you may keep using the second one.
Ok boss, thank you very much
Hello KK,
I have a few follow up questions to the on page save function:
1. On an e-commerce website I a building now, when a user places an order, the order details including the cart items are persisted to the database.
The order is given an initial status of 'Order received' (there are 3 other statuses: Order processed, Order dispatched and Order delivered). Once the buyer makes payment, I will manually change the status to 'Order processed' at the backend using an order UID. What I want to achieve is this: once I make a modification to the order status (radio buttons) and save the page again, an email notification should be sent to the buyer with details of the current status of the order. How do I achieve this?

I tried doing this after you showed me how to alter page fields on page save but I realised I can't and that brings me to my next question.

2. Can I use couch tags inside the kfunctions.php file in the addons folder? Any further information on how to do it only if possible will make me happy :D

Thank you and will be looking forward to your expert advice.
10 posts Page 1 of 1

Who is online

In total there are 10 users online :: 0 registered, 0 hidden and 10 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 10 guests