• Is it possible to create a temporarily variable data from the session variable ?

    In My project (Custom sizeable Mattress or Bed), Every product have custom range of prices for its corresponding attributes (ie, Combination of Length, width & height make the product price) we have the range of product prices that is like,

    Length width height price (Inch)
    72 32 6 $200
    72 36 6 $220
    72 48 6 $250
    72 32 10 $300
    72 36 10 $330
    72 48 10 $370
    75 32 6 $250
    75 36 6 $280
    75 48 6 $300
    75 32 10 $350
    75 36 10 $380
    75 48 10 $410

    All the custom sizes are available and those price will make dynamically with the variance table,
    like a product with attributes 73 35 9 then price is same the upper attributes combination, so here the combination is : 75 36 10 $380

    for this type of product, i used some ways and now need the temporarily creation of a variable product data.

    I write some codes for this project, that is given bellow,

    Override this function
    function wc_dropdown_variation_attribute_options( $args = array() ) {
    $args = wp_parse_args( apply_filters( ‘woocommerce_dropdown_variation_attribute_options_args’, $args ), array(
    ‘options’ => false,
    ‘attribute’ => false,
    ‘product’ => false,
    ‘selected’ => false,
    ‘name’ => ”,
    ‘id’ => ”,
    ‘class’ => ”,
    ‘show_option_none’ => __( ‘Choose an option’, ‘woocommerce’ )
    ) );

    $options = $args[‘options’];
    $product = $args[‘product’];
    $attribute = $args[‘attribute’];
    $name = $args[‘name’] ? $args[‘name’] : ‘attribute_’ . sanitize_title( $attribute );
    $id = $args[‘id’] ? $args[‘id’] : sanitize_title( $attribute );
    $class = $args[‘class’];

    if ( empty( $options ) && ! empty( $product ) && ! empty( $attribute ) ) {
    $attributes = $product->get_variation_attributes();
    $options = $attributes[ $attribute ];
    }

    $html = ‘<select id=”‘ . esc_attr( $id ) . ‘” class=”‘ . esc_attr( $class ) . ‘” name=”‘ . esc_attr( $name ) . ‘” data-attribute_name=”attribute_’ . esc_attr( sanitize_title( $attribute ) ) . ‘”>’;

    if ( $args[‘show_option_none’] ) {
    $html .= ‘<option value=””>’ . esc_html( $args[‘show_option_none’] ) . ‘</option>’;
    }

    if ( ! session_id() )
    session_start();
    if(isset( $_SESSION[‘attr’] ))
    foreach($_SESSION[‘attr’] as $attr => $opt )
    if($attribute ==$attr ){
    //term_fun($attr, $opt , $product )
    $html .= ‘<option selected value=”‘.$opt.'”>’ . esc_html( $opt ) . ‘</option>’;
    }

    if ( ! empty( $options ) ) {
    if ( $product && taxonomy_exists( $attribute ) ) {
    // Get terms if this is a taxonomy – ordered. We need the names too.
    $terms = wc_get_product_terms( $product->id, $attribute, array( ‘fields’ => ‘all’ ) );

    foreach ( $terms as $term ) {
    if ( in_array( $term->slug, $options ) ) {
    $html .= ‘<option value=”‘ . esc_attr( $term->slug ) . ‘” ‘ . selected( sanitize_title( $args[‘selected’] ), $term->slug, false ) . ‘>’ . esc_html( apply_filters( ‘woocommerce_variation_option_name’, $term->name ) ) . ‘</option>’;
    }
    }
    } else {
    foreach ( $options as $option ) {
    // This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
    if(!(isset( $_SESSION[‘attr’] )))
    $selected = sanitize_title( $args[‘selected’] ) === $args[‘selected’] ? selected( $args[‘selected’], sanitize_title( $option ), false ) : selected( $args[‘selected’], $option, false );
    $html .= ‘<option value=”‘ . esc_attr( $option ) . ‘” ‘ . $selected . ‘>’ . esc_html( apply_filters( ‘woocommerce_variation_option_name’, $option ) ) . ‘</option>’;
    }
    }
    }

    $html .= ‘</select>’;

    echo apply_filters( ‘woocommerce_dropdown_variation_attribute_options_html’, $html, $args );
    }

    // and write this for find the upper value of attribute terms
    function term_fun($attr, $val, $product ){
    $t_arry =wc_get_product_terms( $product->id, $attr , array( ‘fields’ => ‘names’ , ‘orderby’ => ‘name_num’ ) );
    foreach($t_arry as $term){
    if($term >= $val){
    break;
    }
    }
    if($val>$term)
    $term;
    return $term;
    }

    //on single-product/add-to-cart/variable.php file add some codes also,

    ….//
    global $product;

    $attribute_keys = array_keys( $attributes );

    do_action( ‘woocommerce_before_add_to_cart_form’ );

    $variation_id=$available_variations[0][‘variation_id’];
    $vari= new WC_Product_Variation( $variation_id);

    //$vari=array();
    if(isset( $_SESSION[‘attr’] )){
    $res=$_SESSION[‘attr’];
    foreach( $res as $attr => $opt )
    $res[‘attribute_’.$attr]=array($opt ,term_fun($attr, $opt, $product));

    foreach($available_variations as $key=>$val){
    $flag=true;
    foreach($val[‘attributes’] as $attribute=>$val2)
    if($res[$attribute][1]!=$val2)
    $flag=false;
    if($flag==true){
    echo ‘; ‘.$key.’ :’;
    $vari=$available_variations[$key]; }
    }

    $vari[‘variation_id’]=1;
    foreach($vari[‘attributes’] as $attribute=>$val2)
    if($res[$attribute][1]==$val2)
    $vari[‘attributes’][$attribute]= $res[$attribute][0];
    //echo htmlspecialchars( json_encode( $vari) ) ;
    $available_variations[$key+1]=$vari;
    }
    ?>
    <form class=”variations_form cart” method=”post” enctype=’multipart/form-data’ data-product_id=”<?php echo absint( $product->id ); ?>” data-product_variations=”<?php echo htmlspecialchars( json_encode( $available_variations ) ) ?>”>
    <?php do_action( ‘woocommerce_before_variations_form’ ); ?>

    <?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
    <p class=”stock out-of-stock”><?php _e( ‘This product is currently out of stock and unavailable.’, ‘flatastic’ ); ?></p>
    <?php else : ?>
    <table class=”variations” cellspacing=”0″>
    <tbody>
    <?php foreach ( $attributes as $attribute_name => $options ) :
    ?>
    <tr>
    <td class=”label”><label for=”<?php echo sanitize_title( $attribute_name ); ?>”><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
    <td class=”value”>
    <?php
    $selected = isset( $_REQUEST[ ‘attribute_’ . sanitize_title( $attribute_name ) ] ) ? wc_clean( $_REQUEST[ ‘attribute_’ . sanitize_title( $attribute_name ) ] ) : $product->get_variation_default_attribute( $attribute_name );
    wc_dropdown_variation_attribute_options( array( ‘options’ => $options, ‘attribute’ => $attribute_name, ‘product’ => $product, ‘selected’ => $selected ) );
    echo end( $attribute_keys ) === $attribute_name ? apply_filters( ‘woocommerce_reset_variations_link’, ‘‘ . __( ‘Clear’, ‘flatastic’ ) . ‘‘ ) : ”;
    ?>
    </td>
    </tr>
    <?php endforeach;?>
    </tbody>
    </table>

    <?php do_action( ‘woocommerce_before_add_to_cart_button’ ); ?>

    <div class=”single_variation_wrap”>
    <?php
    /**
    //……………….

    Please help me

    thanks & Regards

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Is it possible to create a temporary variable data ?’ is closed to new replies.