Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function eps_variable_add_to_cart(){
- global $product, $post;
- // Check if the special 'price_grid' meta is set, if it is, load the default template:
- if ( get_post_meta( get_the_ID(), 'eps_add_to_cart_active',true) === 'true' ) {
- // Enqueue variation scripts
- wp_enqueue_script( 'wc-add-to-cart-variation' );
- // Load the template
- wc_get_template( 'single-product/add-to-cart/variable.php', array(
- 'available_variations' => $product->get_available_variations(),
- 'attributes' => $product->get_variation_attributes(),
- 'selected_attributes' => $product->get_variation_default_attributes()
- ) );
- return;
- }
- // Cool, lets do our own template!
- $variations = find_valid_variations();
- $vb_attrs = $product->get_variation_attributes();
- ?>
- <form class="cart" action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" method="post" enctype='multipart/form-data'>
- <?php wp_nonce_field( 'multi_add_to_cart_nonce', 'multi_add_to_cart_nonce' ); ?>
- <table class="variations variations-grid table" cellspacing="0">
- <thead>
- <tr>
- <th>Type</th>
- <th>Price</th>
- <th>Quantity</th>
- </tr>
- </thead>
- <tbody>
- <?php
- // Loop through each valid variation:
- $show_thumbnails = get_post_meta( get_the_ID(), 'eps_add_to_cart_thumbnails', true);
- $show_skus = get_post_meta( get_the_ID(), 'eps_add_to_cart_skus', true);
- foreach ($variations as $key => $value) {
- if( !$value['variation_is_visible'] ) continue;
- ?>
- <tr>
- <?php
- if( $show_thumbnails === 'true' || $show_thumbnails === false ) {
- if ( isset( $value['image_src'] ) && !empty( $value['image_src'] )) {
- echo sprintf( '<td><img src="%s" alt="%s" width="50"></td>', $value['image_src'], $value['image_title'] );
- } else {
- echo sprintf( '<td><img src="%s" alt="%s" width="50"></td>', wc_placeholder_img_src(), $value['image_title'] );
- }
- }
- ?>
- <td>
- <?php
- // Echo the variation info
- foreach($value['attributes'] as $key => $val ) {
- $val = str_replace(array('-','_'), ' ', $val);
- $attr_key = contains( $val, $vb_attrs );
- //printaj($attr_key);
- if($attr_key || $attr_key === 0 ) {
- foreach($vb_attrs as $my_v) {
- //printaj($my_v);
- printf( '<span class="attr attr-%s">%s</span>', $key, $my_v[$attr_key] );
- }
- }
- else {
- printf( '<span class="attr attr-%s">%s</span>', $key, $val );
- }
- } ?>
- </td>
- <td>
- <?php
- // Echo the price
- echo $value['price_html'];
- ?>
- </td>
- <?php
- if( $show_skus === 'true' || $show_skus === false ) {
- printf( '<td><span class="sku">#%s</span></td>', $value['sku'] );
- }
- ?>
- <td>
- <?php if( $value['is_in_stock'] ) { ?>
- <?php // Do QTY box: ?>
- <div class="quantity buttons_added">
- <input type="button" value="-" class="minus"/>
- <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"/>
- <input type="button" value="+" class="plus"/>
- </div>
- <?php
- // Add the variation attribute data
- if(!empty($value['attributes'])){
- foreach ($value['attributes'] as $attr_key => $attr_value) {
- ?>
- <input type="hidden" name="multi_add_to_cart[<?php echo $value['variation_id']?>][attributes][<?php echo $attr_key?>]" value="<?php echo $attr_value?>">
- <?php
- }
- }
- // Add the add-to-cart info:
- ?>
- <input type="hidden" name="multi_add_to_cart[<?php echo $value['variation_id']?>][variation_id]" value="<?php echo $value['variation_id']?>" />
- <input type="hidden" name="multi_add_to_cart[<?php echo $value['variation_id']?>][product_id]" value="<?php echo esc_attr( $post->ID ); ?>" />
- <input type="hidden" name="multi_add_to_cart[<?php echo $value['variation_id']?>][add-to-cart]" value="<?php echo esc_attr( $post->ID ); ?>" />
- <?php } else { ?>
- <p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
- <?php } ?>
- </td>
- </tr>
- <?php } ?>
- </tbody>
- <tfoot>
- <tr>
- <td colspan="3"><small class="pull-left">You will be taken to PayPal's secure checkout to complete the purchase.</small>
- <button type="submit" class="btn-alt pull-right">Add to Clipboard</button></td>
- </tr>
- </tfoot>
- </table>
- </form>
- <?php
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement