Skip to content

Instantly share code, notes, and snippets.

@agusmu
Last active January 18, 2019 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agusmu/b30006106fd01b9a240b to your computer and use it in GitHub Desktop.
Save agusmu/b30006106fd01b9a240b to your computer and use it in GitHub Desktop.
OnePageCheckout - Automatically Add To Cart Item
<?php
/**
* Template to display product selection fields in a table (with thumbnail etc.)
*
* @package WooCommerce-One-Page-Checkout/Templates
* @version 1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<?php // echo '<pre>';var_dump($products);echo '</pre>'; ?>
<table class="shop_table" cellspacing="0">
<?php foreach( $products as $product ) : ?>
<?php
if ( ! $product->in_cart ) {
WC()->cart->add_to_cart( $product->add_to_cart_id );
$product->in_cart = true;
$product->cart_item['quantity'] = 1;
}
?>
<tr class="product-item <?php if ( $product->in_cart ) echo 'selected'; ?>">
<td class="product-thumbnail">
<a href="<?php echo wp_get_attachment_url( $product->get_image_id() ); ?>" data-rel="prettyPhoto" itemprop="image" class="woocommerce-main-image zoom">
<?php echo $product->get_image(); ?>
</a>
</td>
<td class="product-name">
<?php echo $product->get_title(); ?>
<?php if ( $product->is_type( 'variation' ) ) : ?>
<?php $attribute_string = sprintf( '&nbsp;(%s)', wc_get_formatted_variation( $product->get_variation_attributes(), true ) ); ?>
<span class="attributes"><?php echo esc_html( apply_filters( 'wcopc_attributes', $attribute_string, $product->get_variation_attributes(), $product ) ); ?></span>
<?php else : ?>
<?php $attributes = $product->get_attributes(); ?>
<?php foreach ( $attributes as $attribute ) : ?>
<?php $attribute_string = sprintf( '&nbsp;(%s)', $product->get_attribute( $attribute['name'] ) ); ?>
<span class="attributes"><?php echo esc_html( apply_filters( 'wcopc_attributes', $attribute_string, $attribute, $product ) ); ?></span>
<?php endforeach; ?>
<?php endif; ?>
</td>
<td class="product-price">
<span itemprop="price" class="price"><?php echo $product->get_price_html(); ?></span>
</td>
<td class="product-quantity">
<?php wc_get_template( 'checkout/add-to-cart/opc.php', array( 'product' => $product ), '', PP_One_Page_Checkout::$template_path ); ?>
</td>
</tr>
<?php endforeach; // end of the loop. ?>
</table>
<?php // echo '<pre>';var_dump($products);echo '</pre>'; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment