0

So I have this code that changes the variable price according to what the option the customer selected.

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( __( '<span class="from-price">From:</span> %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( __( '<span class="from-price">From:</span> %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price !== $saleprice && $product->is_on_sale() ) {
        $price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <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() {
        jQuery('select').blur( function(){
            if( '' != jQuery('input.variation_id').val() ){
                jQuery('p.price').html(jQuery('div.woocommerce-variation-price > span.price').html());
                jQuery("p.price").prepend('<span class="stack-price">Your Stack: </span>');
                console.log(jQuery('input.variation_id').val());
            } else {
                jQuery('p.price').html(jQuery('div.hidden-variable-price').html());
                if(jQuery('p.availability'))
                    jQuery('p.availability').remove();
                console.log('NULL');
            }
        });


    });
    </script>
    <?php

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

endif;
}

        }
    }
}

The code was found here: https://stackoverflow.com/questions/44912300/replace-the-variable-price-range-by-the-chosen-variation-price-in-woocommerce-3

However I only want that code to apply to two specific products. I tried adding $product_ids = array( 375, 39 ); after if ( $product->is_type( 'variable' ) ) { but it didn't work.

Any help?

1 Answer 1

1

you can use

if(is_product() && get_the_id() == 1253) {
// your code here
}

get product id first

3
  • thanks but that works for one product (one product ID).. But I have two products I need to apply that code to. Feb 26, 2018 at 18:46
  • 1
    It's pretty simple to adjust the "if" to check for either of 2 products. Do a bit of research on conditionals and "or" statements.
    – WebElaine
    Feb 26, 2018 at 19:27
  • use it like if(is_product() && (get_the_id() == 1253) || get_the_id() == 1254)) {
    – Atif Aqeel
    Feb 27, 2018 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.