Woocommerce Variable Product page

We hired a developer to create a custom bit of code which went in to Woocommerce > Single Product > Add To Cart > Variable.php

You can see how the page has been modified to look here: Example of Single Variable Product Page

Read More
   <?php
/**
 * Variable product add to cart
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates
 * @version     2.1.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

function omniwp_output_variation_row( $ref, $dimensions, $qty, $price1, $price2, $price3, $price4, $id ) {
?>
        <tr id="ctl00_ContentPlaceHolder1_rptPrices_ctl00_tr" class="">
          <td class="colRef"><?php echo $ref ?></td>
          <td class="colDimensions"><?php echo $dimensions ?></td>
          <td class="colPackQty"><?php echo $qty ?></td>
          <td class="colPPI1"><?php echo $price1 ?></td>
          <td class="colPPI2-4"><?php echo $price2 ?></td>
          <td class="colPPI5-9"><?php echo $price3 ?></td>
          <td class="colPPI10"><?php echo $price4 ?></td>
          <td class="colOrderQty"><input name="qty_variation_id[<?php echo $id ?>]" class="textbox txtQty" type="text"></td>
        </tr>
<?php
}

global $woocommerce, $product, $post;

$discounts              = '';
$_enable_discount       = get_post_meta( $product->post->ID, '_enablediscount', true);
$dpta_overall_discounts = get_option( 'dpta_overall_discounts' );

if ( $_enable_discount == 'yes' ) { 

    $_attached_discount = get_post_meta( $product->post->ID, '_attacheddiscount', true);
    $discounts          = array_shift( dpta_get_applicable_discount( $_attached_discount ) );

} elseif ( isset( $dpta_overall_discounts['common'] ) 
    && $dpta_overall_discounts['common'] == 'yes' 
    && isset( $dpta_overall_discounts['commondiscount'] ) )  {

    $_attached_discount = $dpta_overall_discounts['commondiscount']; 
    $discounts          = array_shift( dpta_get_applicable_discount( $_attached_discount ) );

}

$currency_symbol     = get_woocommerce_currency_symbol();
$currency_symbol_len = strlen( $currency_symbol );

$dimensions         = false;
$cartons            = false;
$product_attributes = $product->get_attributes();

if ( $product_attributes ) {

$dimensions_values  = explode( '|' , $product_attributes['dimensions']['value'] );
$dimensions_keys    = array_map( 'sanitize_title', $dimensions_values );
$dimensions         = array_combine( $dimensions_keys, $dimensions_values );

$cartons_values  = explode( '|' , $product_attributes['cartons']['value'] );
$cartons_keys    = array_map( 'sanitize_title', $cartons_values );
$cartons         = array_combine( $cartons_keys, $cartons_values );

}

?>

<?php do_action( 'woocommerce_before_add_to_cart_form' ); ?>

<form class="variations_form cart" method="post" enctype="multipart/form-data" data-product_id="<?php echo $post->ID; ?>" data-product_variations="<?php echo esc_attr( json_encode( $available_variations ) ) ?>">
    <?php if ( ! empty( $available_variations ) ) : ?>
    <table>
      <thead>
        <tr>
          <th class="colRef" rowspan="2" scope="col">Ref.</th>
          <th class="colDimensions" rowspan="2" scope="col">Dimensions</th>
          <th class="colPackQty" rowspan="2" scope="col">Pack Qty.</th>
          <th class="colPPI" colspan="4" scope="colgroup">Price per Pack</th>
          <th class="colOrderQty" rowspan="2" scope="col">Order Qty.</th>
        </tr>
        <tr>
          <th class="colPPI1" scope="col">1</th>
          <th class="colPPI2-4" scope="col">2-4</th>
          <th class="colPPI5-9" scope="col">5-9</th>
          <th class="colPPI10" scope="col">10+</th>
        </tr>
      </thead>
      <tbody>
<?php 


    foreach ( $available_variations as $variation ) {

        $name = false;
        $qty  = '';

        if ( $dimensions && array_key_exists( 'attribute_dimensions', $variation['attributes'] ) )
            $name = $dimensions[ $variation['attributes']['attribute_dimensions'] ];
        elseif ( $cartons && array_key_exists( 'attribute_cartons', $variation['attributes'] ) )
            $name = $cartons[ $variation['attributes']['attribute_cartons'] ];
        elseif ( array_key_exists( 'attribute_pa_dimensions', $variation['attributes'] ) ) {
            $term = get_term_by( 'slug', $variation['attributes']['attribute_pa_dimensions'], 'pa_dimensions');
            if ( $term ) 
                $name = $term->name;
        }

        if ( ! $name ) { 
            $option = 'Not found variation attribute for  `' . print_r( $variation['attributes'], true ) . '`';
            $qty    = '-';
        } else {
            $option = substr( $name, 0, strpos( $name, '(' ) - 1  ); 
            if ( strpos( $name, '(Qty.' ) )
                $qty = substr( $name, strpos( $name, '(Qty.' ) + 5, -1 ); 
            elseif ( strpos( $name, '(Qty' ) )
                $qty = substr( $name, strpos( $name, '(Qty' ) + 4, -1 );

            $qty = str_replace( ')', '', $qty ); 

        }

        $price1 = $variation['price_html'];

        if ( empty( $price1 ) ) 
            $price1 = '<span class="price">' . wc_price( $product->get_variation_price( 'min', $display = true ) ) . '</span>';
    //  $price1 = '<span class="price">' . wc_price( $product->get_price() ) . '</span>';

        if ( empty( $discounts ) ) {
            $price2 = $price3 = $price4 = $price1 . ' no discounts';
        } else {

            $price = substr( $price1, strpos( $price1, $currency_symbol ) + $currency_symbol_len  );
            $price = substr( $price, 0, strpos( $price, '<' )  );
            $price = str_replace( array( '.', ',' ), '', $price ); // remove any number formating
            $price = $price / 100;                                 // make price a float with 2 digits 


            $price2 = '<span class="price">' . wc_price( $price - ( $price / 100 ) * $discounts['rules'][1]['amount'] ) . '</span>';
            $price3 = '<span class="price">' . wc_price( $price - ( $price / 100 ) * $discounts['rules'][2]['amount'] ) . '</span>';
            $price4 = '<span class="price">' . wc_price( $price - ( $price / 100 ) * $discounts['rules'][3]['amount'] ) . '</span>';
        }

        omniwp_output_variation_row( 
            $ref = $variation['sku'], 
            $variation_dimensions = esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ), 
            $qty, 
            $price1, 
            $price2, 
            $price3, 
            $price4, 
            $field_name = $variation['variation_id'] 
            );
    }
?>
      </tbody>
      <tfoot>
        <tr>
          <th class="colRef" rowspan="2" scope="col">Ref.</th>
          <th class="colDimensions" rowspan="2" scope="col">Dimensions</th>
          <th class="colPackQty" rowspan="2" scope="col">Pack Qty.</th>
          <th class="colPPI" colspan="4" scope="colgroup">Price per Pack</th>
          <th class="colOrderQty" rowspan="2" scope="col">Order Qty.</th>
        </tr>
        <tr>
          <th class="colPPI1" scope="col">1</th>
          <th class="colPPI2-4" scope="col">2-4</th>
          <th class="colPPI5-9" scope="col">5-9</th>
          <th class="colPPI10" scope="col">10+</th>
        </tr>
      </tfoot>
    </table>

        <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

        <div class="multiple_variation_wrap">
            <div class="variations_button">
                <button type="submit" class="single_add_to_cart_button button alt"><?php echo $product->single_add_to_cart_text(); ?></button>
                <input type="hidden" name="multiple-add-to-cart" value="<?php echo $product->id; ?>" />
                <input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
                <input type="hidden" name="xvariation_id" value="multiple-add-to-cart" />
            </div>
        </div>

        <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>

    <?php else : ?>

        <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>

        <?php endif; ?>

        </form>

        <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>

However, when we try to update woocommerce to the latest version, it breaks this code (and Single Variable Product page) and no longer looks like it should. Can anyone spot anything glaring that needs changing. I was thinking maybe there were some deprecated functions that are no longer used/ need changing.

I’d really appreciate any help from someone that know PHP and Woocommerce 🙂

Paul

Related posts

1 comment

  1. Yes, a few changes have come in like, There is a need to update

    $product->id to $product->get_id() 
    

    and

    $product->product_type to $product->get_type() 
    

    since Woocommerce 3.0.0 update.

Comments are closed.