SUGGESTIONS AND PROPOSALS - by murmoon - on WordPress WooCommerce support

This topic has 2 replies, 2 voices, and was last updated 6 years, 1 months ago ago by Olga Barlow

  • Avatar: murmoon
    murmoon
    Participant
    April 2, 2018 at 06:06

    Can you add these options in theme settings?

    // Show price near variation
    add_filter( ‘woocommerce_variation_option_name’, ‘display_price_in_variation_option_name’ );
    function display_price_in_variation_option_name( $term ) {

    global $wpdb, $product;
    if ( empty( $term ) ) return $term;

    if ( empty( $product->id ) ) return $term;
    $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 . ‘ (‘ . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ‘)’;

    }

    return $term;
    }

    //Replace the Variable Price range by the chosen variation price in WooCommerce 3
    add_action( ‘woocommerce_before_single_product’, ‘check_if_variable_first’ );
    function check_if_variable_first(){
    if ( is_product() ) {
    global $post;
    $product = wc_get_product( $post->ID );
    if ( $product->is_type( ‘variable’ ) ) {
    // removing the price of variable products
    remove_action( ‘woocommerce_single_product_summary’, ‘woocommerce_template_single_price’, 10 );

    // Change location of
    add_action( ‘woocommerce_single_product_summary’, ‘custom_wc_template_single_price’, 10 );
    function custom_wc_template_single_price(){
    global $product;

    // Variable product only
    if($product->is_type(‘variable’)):

    // Main Price
    $prices = array( $product->get_variation_price( ‘min’, true ), $product->get_variation_price( ‘max’, true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( ‘от: %1$s’, ‘woocommerce’ ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    // Sale Price
    $prices = array( $product->get_variation_regular_price( ‘min’, true ), $product->get_variation_regular_price( ‘max’, true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( ‘от: %1$s’, ‘woocommerce’ ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price !== $saleprice && $product->is_on_sale() ) {
    $price = ‘‘ . $saleprice . $product->get_price_suffix() . ‘ <ins>’ . $price . $product->get_price_suffix() . ‘</ins>’;
    }

    ?>
    <style>
    div.woocommerce-variation-price,
    div.woocommerce-variation-availability,
    div.hidden-variable-price {
    height: 0px !important;
    overflow:hidden;
    position:relative;
    line-height: 0px !important;
    font-size: 0% !important;
    }
    </style>
    <script>
    jQuery(document).ready(function($) {
    $(‘select’).blur( function(){
    if( ” != $(‘input.variation_id’).val() ){
    $(‘p.price’).html($(‘div.woocommerce-variation-price > span.price’).html()).append(‘<p class=”availability”>’+$(‘div.woocommerce-variation-availability’).html()+'</p>’);
    console.log($(‘input.variation_id’).val());
    } else {
    $(‘p.price’).html($(‘div.hidden-variable-price’).html());
    if($(‘p.availability’))
    $(‘p.availability’).remove();
    console.log(‘NULL’);
    }
    });
    });
    </script>
    <?php

    echo ‘<p class=”price”>’.$price.'</p>
    <div class=”hidden-variable-price” >’.$price.'</div>’;

    endif;
    }

    }
    }
    }

    /**
    * Copy from here to your (child) themes functions.php
    * Recommended to do so via FTP.
    */
    /**
    * Hide all but the free shipping options when free is available.
    *
    * Shows ALL free options when available.
    * Does NOT take in account local pickup as free shipping – It does show local pickup rates in the list of free shipping rates
    * DOES consider non-‘free_shipping’ rates that have 0 cost as free shipping.
    */
    function custom_hide_all_shipping_when_free_is_available( $shipping_rates) {
    $free_rates = array();
    $free_shipping_available = false;
    foreach ( $shipping_rates as $key => $rate ) {
    // Check for free rates / don’t take in account local pickup
    if ( 0 == $rate->cost && $rate->method_id != ‘local_pickup’ ) {
    $free_shipping_available = true;
    }
    if ( 0 == $rate->cost ) {
    $free_rates[ $key ] = $rate;
    }
    }
    // Show all free rates
    if ( $free_shipping_available ) {
    return $free_rates;
    }
    return $shipping_rates;
    }
    add_filter( ‘woocommerce_package_rates’, ‘custom_hide_all_shipping_when_free_is_available’ );

    1 Answer
    Avatar: Olga Barlow
    Olga Barlow
    Support staff
    April 2, 2018 at 15:39

    Hello,

    Thank you for your message.
    Do you want to show “from: price” for the variable products instead of default WC price range?
    There are many solutions to achieve this, here you can find one more https://businessbloomer.com/disable-variable-product-price-range-woocommerce/

    Thank you for your suggestion but this additional customization would better to inmplement in child theme than the new option in theme settings.

    Regards

  • Viewing 2 results - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.