Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author DesignLoud

    (@designloud)

    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;
    
    }
    ?>
    Thread Starter squash247

    (@squash247)

    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..?

    Plugin Author DesignLoud

    (@designloud)

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

    Thread Starter squash247

    (@squash247)

    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 [email protected] 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

    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: https://stackoverflow.com/questions/12272733/woocommerce-get-variation-product-price

    Thread Starter squash247

    (@squash247)

    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..

    Plugin Author DesignLoud

    (@designloud)

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

    This solved it for me: https://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;

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

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘adding prices next to radio buttons’ is closed to new replies.