Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.04 KB | None | 0 0
  1. function eps_variable_add_to_cart(){
  2.     global $product, $post;
  3.            
  4.    
  5.     // Check if the special 'price_grid' meta is set, if it is, load the default template:
  6.     if ( get_post_meta( get_the_ID(), 'eps_add_to_cart_active',true) === 'true' ) {
  7.         // Enqueue variation scripts
  8.         wp_enqueue_script( 'wc-add-to-cart-variation' );
  9.  
  10.         // Load the template
  11.         wc_get_template( 'single-product/add-to-cart/variable.php', array(
  12.                 'available_variations'  => $product->get_available_variations(),
  13.                 'attributes'            => $product->get_variation_attributes(),
  14.                 'selected_attributes'   => $product->get_variation_default_attributes()
  15.             ) );
  16.         return;
  17.     }      
  18.    
  19.    
  20.     // Cool, lets do our own template!
  21.     $variations = find_valid_variations();
  22.     $vb_attrs   = $product->get_variation_attributes();
  23.  
  24.     ?>
  25.     <form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" method="post" enctype='multipart/form-data'>
  26.     <?php wp_nonce_field( 'multi_add_to_cart_nonce', 'multi_add_to_cart_nonce' ); ?>
  27.     <table class="variations variations-grid table" cellspacing="0">
  28.         <thead>
  29.             <tr>
  30.                 <th>Type</th>
  31.                 <th>Price</th>
  32.                 <th>Quantity</th>
  33.             </tr>
  34.         </thead>
  35.         <tbody>
  36.             <?php
  37.             // Loop through each valid variation:
  38.             $show_thumbnails = get_post_meta( get_the_ID(), 'eps_add_to_cart_thumbnails', true);
  39.             $show_skus       = get_post_meta( get_the_ID(), 'eps_add_to_cart_skus', true);
  40.                
  41.             foreach ($variations as $key => $value) {
  42.                 if( !$value['variation_is_visible'] ) continue;
  43.             ?>
  44.             <tr>
  45.                 <?php
  46.                 if( $show_thumbnails === 'true' || $show_thumbnails === false ) {
  47.                     if ( isset( $value['image_src'] ) && !empty( $value['image_src'] )) {
  48.                         echo sprintf( '<td><img src="%s" alt="%s" width="50"></td>', $value['image_src'], $value['image_title'] );
  49.                     } else {
  50.                         echo sprintf( '<td><img src="%s" alt="%s" width="50"></td>', wc_placeholder_img_src(), $value['image_title'] );
  51.                     }
  52.                 }
  53.                 ?>
  54.                 <td>
  55.                     <?php
  56.                     // Echo the variation info
  57.                     foreach($value['attributes'] as $key => $val ) {
  58.                         $val = str_replace(array('-','_'), ' ', $val);
  59.  
  60.                         $attr_key = contains( $val, $vb_attrs );
  61.                        
  62.                         //printaj($attr_key);
  63.  
  64.                         if($attr_key || $attr_key === 0 ) {
  65.                             foreach($vb_attrs as $my_v) {
  66.                                 //printaj($my_v);
  67.                                 printf( '<span class="attr attr-%s">%s</span>', $key, $my_v[$attr_key] );
  68.                             }
  69.                         }
  70.                         else {
  71.                            printf( '<span class="attr attr-%s">%s</span>', $key, $val );
  72.                         }
  73.                        
  74.                        
  75.  
  76.                     } ?>
  77.                 </td>
  78.                 <td>
  79.                     <?php
  80.                     // Echo the price
  81.                     echo $value['price_html'];
  82.                     ?>
  83.                 </td>
  84.                 <?php
  85.                     if( $show_skus === 'true' || $show_skus === false  ) {
  86.                         printf( '<td><span class="sku">#%s</span></td>', $value['sku'] );
  87.                     }
  88.                 ?>
  89.                 <td>
  90.                     <?php if( $value['is_in_stock'] ) { ?>
  91.                        
  92.                         <?php // Do QTY box: ?>
  93.                         <div class="quantity buttons_added">
  94.                             <input type="button" value="-" class="minus"/>
  95.                             <input type="number" step="1" name="multi_add_to_cart[<?php echo $value['variation_id']; ?>][quantity]" value="0" title="Qty" class="input-text qty text" size="4"/>
  96.                             <input type="button" value="+" class="plus"/>
  97.                         </div>
  98.  
  99.                         <?php
  100.                         // Add the variation attribute data
  101.                         if(!empty($value['attributes'])){
  102.                             foreach ($value['attributes'] as $attr_key => $attr_value) {
  103.                             ?>
  104.                             <input type="hidden" name="multi_add_to_cart[<?php echo $value['variation_id']?>][attributes][<?php echo $attr_key?>]" value="<?php echo $attr_value?>">
  105.                             <?php
  106.                             }
  107.                         }
  108.                         // Add the add-to-cart info:
  109.                         ?>
  110.                         <input type="hidden" name="multi_add_to_cart[<?php echo $value['variation_id']?>][variation_id]" value="<?php echo $value['variation_id']?>" />
  111.                         <input type="hidden" name="multi_add_to_cart[<?php echo $value['variation_id']?>][product_id]" value="<?php echo esc_attr( $post->ID ); ?>" />
  112.                         <input type="hidden" name="multi_add_to_cart[<?php echo $value['variation_id']?>][add-to-cart]" value="<?php echo esc_attr( $post->ID ); ?>" />
  113.                     <?php } else { ?>
  114.                         <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
  115.                     <?php } ?>
  116.                 </td>
  117.             </tr>
  118.             <?php } ?>
  119.         </tbody>
  120.         <tfoot>
  121.             <tr>
  122.                 <td colspan="3"><small class="pull-left">You will be taken to PayPal's secure checkout to complete the purchase.</small>
  123.                 <button type="submit" class="btn-alt pull-right">Add to Clipboard</button></td>
  124.             </tr>
  125.         </tfoot>
  126.     </table>
  127.    
  128.    
  129.  
  130.     </form>
  131.  
  132.     <?php
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement