WordPress.org

Ready to get started?Download WordPress

Forums

Woocommerce Radio Buttons
[resolved] adding prices next to radio buttons (10 posts)

  1. squash247
    Member
    Posted 1 year ago #

    Is there any way I could add a code to show the prices next to each radio button so the customer can see them all without having to press on the button to see the price?

    http://wordpress.org/extend/plugins/woocommerce-radio-buttons/

  2. DesignLoud
    Member
    Plugin Author

    Posted 1 year ago #

    Yes, add this to your functions.php file

    <?php
    //show price next to variations in woocommerce
    add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
    
    function display_price_in_variation_option_name( $term ) {
    global $wpdb, $product;
    
    $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
    
    $term_slug = ( !empty( $result ) ) ? $result[0] : $term;
    
    $query = "SELECT postmeta.post_id AS product_id
    FROM {$wpdb->prefix}postmeta AS postmeta
    LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
    WHERE postmeta.meta_key LIKE 'attribute_%'
    AND postmeta.meta_value = '$term_slug'
    AND products.post_parent = $product->id";
    
    $variation_id = $wpdb->get_col( $query );
    
    $parent = wp_get_post_parent_id( $variation_id[0] );
    
    if ( $parent > 0 ) {
    $_product = new WC_Product_Variation( $variation_id[0] );
    return $term . ' (' . woocommerce_price( $_product->get_price() ) . ')';
    }
    return $term;
    
    }
    ?>
  3. squash247
    Member
    Posted 1 year ago #

    Thanks for that,,I have added the code into the file but no prices are showing?

    Does the code have to be in a certain position..?

  4. DesignLoud
    Member
    Plugin Author

    Posted 1 year ago #

    Just in your themes functions.php file, may need to take out the php opening and closing tag depending on your theme

  5. squash247
    Member
    Posted 1 year ago #

    Hi Derek,,
    thanks for your help, but I still can not get the prices to show next to the radio buttons. could you possible email me tickets247@gmail.com and I can give you a sign in for a test wordpress website and see if you can get it working..see where I am going wrong.

    Regards

    Chris

  6. JBIRD1111
    Member
    Posted 1 year ago #

    This code does not work for me either. I need to place the variation prices next to each radio button, and the above code does not seem to have any effect on the price. I have searched and have not found another solution. Any ideas?

    I also implemented these changes with no effect: http://stackoverflow.com/questions/12272733/woocommerce-get-variation-product-price

  7. squash247
    Member
    Posted 1 year ago #

    We got the code to work fine in the end with help from Design-Loud.

    The problem I had was that the product variations can not have any characters apart from numbers and letters, and also no spaces, so hyphens must be used. once the variations followed this rule the prices are shown next to each item..

  8. DesignLoud
    Member
    Plugin Author

    Posted 1 year ago #

    Hey guys, you can check out http://designloud.com/add-prices-next-to-woocommerce-variations/ for details. Hope this solves your problem ;)

  9. JBIRD1111
    Member
    Posted 1 year ago #

    This solved it for me: http://webroxtar.com/2013/07/get-regular-and-sale-prices-of-product-variations-in-woocommerce/

    #Step 1: Get product varations
    $available_variations = $product->get_available_variations();

    #Step 2: Get product variation id
    $variation_id=$available_variations[0]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.

    #Step 3: Create the variable product object
    $variable_product1= new WC_Product_Variation( $variation_id );

    #Step 4: You have the data. Have fun :)
    $regular_price = $variable_product1 ->regular_price;
    $sales_price = $variable_product1 ->sale_price;

  10. Tevya
    Member
    Posted 7 months ago #

    Why not add this to the plugin as an option that can be turned on or off?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.