Dropdown for product price based on category of product

Hi, my client has a request to provide a fixed set of pricing based on the category of the product. e.g:

Games: £20
CDs: £10
DVDs: £7
BlueRay: £25

Is this possible with MarketPress and if not could someone point me in the right direction for how to customise the page (wp-admin/post-new.php?post_type=product) Due to the url-rewriting I don't know where to look to find the add product page.

Thanks

Adam

  • aseabridge
    • Flash Drive

    Hi David,

    That would be great if one of the devs could give me a few pointers.
    Allthough I don't have much experience of wordpress I have 8yrs experience as a .Net developer and have built 3 sites from scratch with PHP so am willing to have a go at it.

    Thanks

    Adam

  • Aaron
    • CTO

    Probably the easiest thing would be to tie into the metabox saving function (save_product_meta()) and override the prices.

    This is the action hook you could tie into:

    //for any other plugin to hook into
          do_action( 'mp_save_product_meta', $post_id, $meta );

    You would be updating the price like:
    update_post_meta($post_id, 'mp_price', '20.00');

  • aseabridge
    • Flash Drive

    Hi Aaron,

    Thanks for you assistance with this. I have manged to limit the categories as user can assign a product to a single category by altering the category listing to use radio buttons.

    However based on the category of the product I need to set the price but I can't work out how to get the category from the product. I have tried the following 2 in marketpress.php and neither of them work:

    1)
    foreach((get_the_category()) as $category) {
    echo $category . ",";
    }

    2)
    echo($post->post_category);

    Thanks

    Adam

  • aseabridge
    • Flash Drive

    Aaron, you are a star, that worked a treat!.

    If anyone else is interested in doing this, this is what I did:

    //get the cats for a product (should only be one due to radio button restriction)
    $arrprodcats = get_the_terms($post->ID, "product_category");
    //cast this to a stdclass object
    $prodcats = (object)$arrprodcats;
    $prodcatid = 0;
    //get the category id so I can use this to set a price
    foreach ($prodcats as $cat) {
    //echo("cat:".$cat->name. "
    \n");
    //echo("prodcatid:".$prodcatid. "
    \n");
    $prodcatid = $cat->term_id;
    }

    Aaron, Is there a way of doing this and maintaining an upgradable solution. I.E overriding the function or something?

    Thanks

    Adam

  • Aaron
    • CTO

    Again:
    Probably the easiest thing would be to tie into the metabox saving function (save_product_meta()) and override the prices.

    This is the action hook you could tie into:

    //for any other plugin to hook into
    do_action( 'mp_save_product_meta', $post_id, $meta );

    You would be updating the price like:
    update_post_meta($post_id, 'mp_price', array('20.00'));

  • aseabridge
    • Flash Drive

    Hi Aaron,

    I have come back to this today and realized what you meant. To hook into the update_post_meta() method in the functions.php file in \wp-content\themes\framemarket.

    So I added the code you provided:

    //get the post id
    $post_id = (int) $_GET['post'];

    //update the price
    update_post_meta($post_id, 'mp_price', array('20.00'));

    This works fine and updates the price. However If I try and get the categories for the post within the functions.php file I don't get anything back? e.g:

    //get the post id
    $post_id = (int) $_GET['post'];

    //try and get the categories
    $arrprodcats = get_the_terms($post_id, "product_category");

    Am I doing something wrong here as this same code works fine in \wp-content\plugins\marketpress\marketpress.php

    Thanks

    Adam

  • Aaron
    • CTO

    That should work. I think you're getting the post ID wrong. It should be getting passed to your function.

    As the hook is do_action( 'mp_save_product_meta', $post_id, $meta ); you would have a function like this:

    add_action('mp_save_product_meta', 'my_function', 10, 2);
    function my_function( $post_id, $meta ) {
         $arrprodcats = get_the_terms($post_id, "product_category");
         var_dump($arrprodcats);
    }
  • aseabridge
    • Flash Drive

    Aaron,

    Thank you for your patience, It took me a while to work out what I was meant to be doing here (especially as it seems hooks don't "echo" anything out. I had to add exit(); to be able to test it was getting called. There was an issue with the $post_id variable somehow not being set when it got to the part of the function where is updates the price but I solved that by getting the id again from the querystring. Below is my working function if anyone is interested:

    add_action('mp_save_product_meta', 'set_product_price',$post_id, $meta);

    function set_product_price($post_id, $meta) {

    $arrprodcats = get_the_terms($post_id, "product_category");
    $prodcats = (object)$arrprodcats;
    $prodcatid = 0;
    $prodcatprice = "20.00";

    foreach ($prodcats as $cat) {
    $prodcatid = $cat->term_id;
    //echo("cat:".$prodcatid);
    }

    if($prodcatid!=0){
    $prodcatprice = (getPriceByCat($prodcatid));
    }

    if ( isset($_GET['post']) )
    $postid = (int) $_GET['post'];
    elseif ( isset($_POST['post_ID']) )
    $postid = (int) $_POST['post_ID'];
    else{
    $postid = 0;
    }
    update_post_meta($postid, 'mp_price', array($prodcatprice));
    }

    Thanks again for your help with this.

    Adam

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.