I'm using woocommerce for my website, but as I only have one main product and two accessories (related products), I don't need a classic shop page, neither single-product pages for each of my product. My main product has a color variation.
I want to have an add-to-cart button, with the color variation dropdown and the quantity field in one of my regular post page. Exactly like on a single-product page, but embedded in my own page, and without the parts of the single-product page I don't need (description, ...).
I finally decided to achieve this using two custom shortcodes I created: [my_vc_product_price id="xxx"] and [my_vc_add2cart_variable_product id="xxx"]. So I can put them where I want.
But my problem is that the behavior of the dropdown menu + variation availability + add-to-cart button is not the same that this elements have in the single-product page:
- the availability of he variation doesn't show up when I choose the color in the dropdown menu;
- the add-to-cart button is not disable when no color is chosen in the dropdown menu (it should be disable and active only when a color is chosen).
Displaying the price was easy, using some code found on internet:
/**
* Add shortcode to allow to display product price in a page
*/
function my_vc_display_product_price( $args ) {
$product_id = $args['id'];
$product = wc_get_product( $product_id );
echo '<p class="price">' . $product->get_price_html() . '</p>';
}
add_shortcode( 'my_vc_product_price', 'my_vc_display_product_price');
To get the same graphical result, I just had to add some CSS classes on the row: "woocommerce" and "product".
The code to display the dropdown menu + the quantity and the add-to-cart button is almost the same than in the variable.php file found in plugins/woocommerce/templates/single-product/add-to-cart/. The only things really change is that you need to get the "variation attributes" of the product, and not the attributes.
$attributes = $product->get_variation_attributes();
$attribute_keys = array_keys( $attributes );
So the full function code is:
/**
* Add shortcode to allow to display an add to cart button with dropdown menu for variation attributes
*/
function my_vc_add_to_cart_button_variable_product( $args ) {
global $product;
$product_id = $args['id'];
$product = wc_get_product( $product_id );
if( $product->is_type( 'variable' )) {
$attributes = $product->get_variation_attributes();
$attribute_keys = array_keys( $attributes );
$available_variations = array( $product->get_available_variations() );
do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo htmlspecialchars( wp_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.', 'woocommerce' ); ?></p>
<?php else : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php foreach ( $attributes as $attribute_name => $options ) : ?>
<tr>
<td class="value">
<?php
$selected = isset( $_REQUEST[ 'attribute_' . sanitize_title( $attribute_name ) ] ) ? wc_clean( stripslashes( urldecode( $_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 ) );
?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap">
<?php
/**
* woocommerce_before_single_variation Hook.
*/
do_action( 'woocommerce_before_single_variation' );
/**
* woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
* #since 2.4.0
* #hooked woocommerce_single_variation - 10 Empty div for variation data.
* #hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
*/
do_action( 'woocommerce_single_variation' );
?>
<script type="text/template" id="tmpl-variation-template">
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
<div class="woocommerce-variation-price">{{{ data.variation.price_html }}}</div>
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
</script>
<script type="text/template" id="tmpl-unavailable-variation-template">
<p><?php _e( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ); ?></p>
</script>
<?php
/**
* woocommerce_after_single_variation Hook.
*/
do_action( 'woocommerce_after_single_variation' );
?>
</div>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<?php endif; ?>
<?php do_action( 'woocommerce_after_variations_form' ); ?>
</form>
<?php
do_action( 'woocommerce_after_add_to_cart_form' );
}
}
add_shortcode( 'my_vc_add2cart_variable_product', 'my_vc_add_to_cart_button_variable_product');
Any idea what is going wrong? I don't understand, if the code is the same, why it didn't execute the same. Is there something missing because this code is outside woocommerce pages?
try using following code
function add_to_cart_form_shortcode( $atts ) {
if ( empty( $atts ) ) {
return '';
}
if ( ! isset( $atts['id'] ) && ! isset( $atts['sku'] ) ) {
return '';
}
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
);
if ( isset( $atts['sku'] ) ) {
$args['meta_query'][] = array(
'key' => '_sku',
'value' => sanitize_text_field( $atts['sku'] ),
'compare' => '=',
);
$args['post_type'] = array( 'product', 'product_variation' );
}
if ( isset( $atts['id'] ) ) {
$args['p'] = absint( $atts['id'] );
}
$single_product = new WP_Query( $args );
$preselected_id = '0';
if ( isset( $atts['sku'] ) && $single_product->have_posts() && 'product_variation' === $single_product->post->post_type ) {
$variation = new WC_Product_Variation( $single_product->post->ID );
$attributes = $variation->get_attributes();
$preselected_id = $single_product->post->ID;
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'p' => $single_product->post->post_parent,
);
$single_product = new WP_Query( $args );
?>
<script type="text/javascript">
jQuery( document ).ready( function( $ ) {
var $variations_form = $( '[data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>"]' ).find( 'form.variations_form' );
<?php foreach ( $attributes as $attr => $value ) { ?>
$variations_form.find( 'select[name="<?php echo esc_attr( $attr ); ?>"]' ).val( '<?php echo esc_js( $value ); ?>' );
<?php } ?>
});
</script>
<?php
}
$single_product->is_single = true;
ob_start();
global $wp_query;
$previous_wp_query = $wp_query;
$wp_query = $single_product;
wp_enqueue_script( 'wc-single-product' );
while ( $single_product->have_posts() ) {
$single_product->the_post()
?>
<div class="single-product" data-product-page-preselected-id="<?php echo esc_attr( $preselected_id ); ?>">
<?php woocommerce_template_single_add_to_cart(); ?>
</div>
<?php
}
$wp_query = $previous_wp_query;
wp_reset_postdata();
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
add_shortcode( 'add_to_cart_form', 'add_to_cart_form_shortcode' );
/*Example Usage [add_to_cart_form id=147]*/
That's just perfect! Many thanks.
Now the hardest to come: I need to understand your code and compare it with mine ;-)
Related
I recently installed a plugin for wordpress/woocommerce that allows you to choose a primary category for a product when it's in multiple categories. It works well to change the breadcrumbs, but my theme (peakshops) also displays the primary category under the corresponding thumbnails on the the shop page.
This is the core function that is built into the theme to display the category (and link to the category) under the thumbnail:
// Add Title with Link.
function thb_template_loop_product_title() {
global $product;
$product_url = apply_filters( 'woocommerce_loop_product_link', get_the_permalink(), $product );
?>
<?php
if ( 'on' === ot_get_option( 'shop_product_listing_category', 'on' ) ) {
$shop_product_listing_category_single = ot_get_option( 'shop_product_listing_category_single', 'on' );
?>
<div class="product-category">
<?php
if ( 'on' === $shop_product_listing_category_single ) {
$product_cats = wc_get_product_category_list( get_the_ID(), '\n', '', '' );
if ( $product_cats ) {
list( $first_part ) = explode( '\n', $product_cats );
echo wp_kses(
$first_part,
array(
'a' => array(
'href' => array(),
'rel' => array(),
),
)
);
}
} else {
echo wc_get_product_category_list( $product->get_id(), ', ' );
}
?>
</div>
<?php } ?>
<h2 class="<?php echo esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ); ?>"><?php the_title(); ?></h2>
<?php
}
add_action( 'woocommerce_shop_loop_item_title', 'thb_template_loop_product_title', 10 );
This core function seems to grab the first category rather than the primary category assigned by the plugin. Is there a way to change this action (or remove it and create a new one) so that it references the primary category assigned. For reference, the primary category plugin stores the value as a post meta field like this:
I am using the code generated based on Add a custom multi-select field to admin product options settings in Woocommerce
// Display a multiselect field in "Linked Products" section
add_action( 'woocommerce_product_options_related', 'display_handles_product_field' );
function display_handles_product_field() {
global $product_object, $post;
?>
<p class="form-field">
<label for="handles_product"><?php _e( 'Handles Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" id="handles_product_ids" name="_handles_product_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
$product_ids = $product_object->get_meta( '_handles_product_ids' );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select>
</p>
<?php
}
// Save the values to the product
add_action( 'woocommerce_admin_process_product_object', 'save_handles_product_field_value', 10, 1 );
function save_handles_product_field_value( $product ){
$data = isset( $_POST['_handles_product_ids'] ) ? array_map( 'intval', (array) $_POST['_handles_product_ids'] ) : array();
$product->update_meta_data( '_handles_product_ids', $data );
}
This code adds a custom search box and adds related products in the admin area when creating or editing a product.
I also created a new tab on the product page to show the added products.
/* New Handles Product Tab */
add_filter( 'woocommerce_product_tabs', 'new_handles_product_tab' );
function new_handles_product_tab( $tabs ) {
/* Add new tab */
$tabs['new_handles_product_tab'] = array(
'title' => __( 'Handles Product', 'woocommerce' ),
'priority' => 50,
'callback' => 'new_handles_product_tab_content'
);
return $tabs;
}
/* Display content in new tab */
function new_handles_product_tab_content() {
// Content
}
Tell me how to add these products to the new tab correctly? And is my code correct?
I will be glad for your help!
Using [products] shortcode with ids argument, the function displaying the product handles in a custom product tab will be:
function new_handles_product_tab_content() {
global $product;
$product_ids = $product->get_meta( '_handles_product_ids' ); // Get handles
if( ! empty($product_ids) )
$product_ids = implode(',', $product_ids);
echo do_shortcode( "[products ids='$product_ids']" ); // Using [products] shortcode for display.
}
Untested it should work.
I would like to be able to change the Page Attribute "Templates" dropdown to radio buttons, to allow me to have corresponding thumbnails next to them.
It looks like this question was already asked here - Is it possible to make WordPress page attribute meta box select option display as images? - however no code was ever supplied and looking at the comments it appears that the user answered their own question?
I have already removed and replaced the Page Attributes meta box on functions.php, as per below:
add_action( 'add_meta_boxes', 'wpse44966_add_meta_box' );
function wpse44966_add_meta_box( $post_type ){
remove_meta_box(
'pageparentdiv',
'page',
'side');
add_meta_box(
'wpse44966-meta-box',
'page' == $post_type ? __('Page Style Templates') : __('Attributes'),
'wpse44966_meta_box_cb',
'page',
'side',
'low');
}
I am then able to call back the "Templates" dropdown in my own meta box - I have also included the page_template_dropdown function (renamed 'page_template_dropdown_show_it').
function page_template_dropdown_show_it( $default = '', $post_type = 'page' ) {
$templates = get_page_templates( null, $post_type );
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) {
$selected = selected( $default, $templates[ $template ], false );
echo "\n\t<option value='" . esc_attr( $templates[ $template ] ) . "' $selected>" . esc_html( $template ) . '</option>';
}
}
function wpse44966_meta_box_cb( $post ){
echo 'Please select from the below';
if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
$template = ! empty( $post->page_template ) ? $post->page_template : false; ?>
<p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label><?php do_action( 'page_attributes_meta_box_template', $template, $post ); ?></p>
<select name="page_template" id="page_template">
<?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?>
<option value="default"><?php echo esc_html( $default_title ); ?></option>
<?php page_template_dropdown_show_it( $template, $post->post_type ); ?>
</select>
<?php endif;
}
However, when I then amend both page_template_dropdown_show_it and wpse44966_meta_box_cb to show radio buttons, the changes are applied visually but nothing happens when you select them?
function page_template_dropdown_show_it( $default = '', $post_type = 'page' ) {
$templates = get_page_templates( null, $post_type );
ksort( $templates );
foreach ( array_keys( $templates ) as $template ) {
$checked = checked( $default, $templates[ $template ], false );
echo "\n\t<input type='radio' name='page_template' value='" . esc_attr( $templates[ $template ] ) . "' $checked>" . esc_html( $template );
}
}
function wpse44966_meta_box_cb( $post ){
echo 'Please select from the below';
if ( count( get_page_templates( $post ) ) > 0 && get_option( 'page_for_posts' ) != $post->ID ) :
$template = ! empty( $post->page_template ) ? $post->page_template : false; ?>
<p class="post-attributes-label-wrapper"><label class="post-attributes-label" for="page_template"><?php _e( 'Template' ); ?></label><?php do_action( 'page_attributes_meta_box_template', $template, $post ); ?></p>
<?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?>
<input type='radio' name='page_template' value="default"><?php echo esc_html( $default_title ); ?>
<?php page_template_dropdown_show_it( $template, $post->post_type ); ?>
<?php endif;
}
It's obviously not as simple as a straight swap-out (as it isn't working), but the only thing that I can see that is missing is now there is nothing carrying the id="page_template", whereas before it was included in the below:
<select name="page_template" id="page_template">
So, it appears that I was correct that the issue was that the id "page_template" was no longer being passed.
To solve this, all I have done is applied some Javascript (using the admin_enqueue_scripts function) which adds the id "page_template" to the radio button that has been selected.
I am trying to add some functionality to woocommerce as per a clients request. The functionality involves me having to modify woocommerce source code. I have explained to the client why this is a bad idea, however they insist, so I do it.
But I am having some trouble adding some values to POST.
This is all done from the woocommerce backend >> orders.
Essentially I am adding a width and height to a product that is added via the back-end by a shop admin.
The only problem that I have is getting the width and height into POST.
I have this code: (backbone modal popup)
<script type="text/template" id="wc-modal-add-products">
<div class="wc-backbone-modal">
<div class="wc-backbone-modal-content">
<section class="wc-backbone-modal-main" role="main">
<header class="wc-backbone-modal-header">
<a class="modal-close modal-close-link" href="#"><span class="close-icon"><span class="screen-reader-text">Close media panel</span></span></a>
<h1><?php _e( 'Add products', 'woocommerce' ); ?></h1>
</header>
<article style="min-height:70px;">
<form action="" method="post">
<input type="hidden" id="add_item_id" name="add_order_items" class="wc-product-search" style="width: 100%;" data-placeholder="<?php _e( 'Search for a product…', 'woocommerce' ); ?>" data-multiple="true" />
<input name="wpti_x" placeholder="Width" class="wpti-product-size" id="wpti-product-x" type="number">
<input name="wpti_y" placeholder="Height" class="wpti-product-size" id="wpti-product-y" type="number">
</form>
</article>
<footer>
<div class="inner">
<button id="btn-ok" class="button button-primary button-large"><?php _e( 'Add', 'woocommerce' ); ?></button>
</div>
</footer>
</section>
</div>
</div>
<div class="wc-backbone-modal-backdrop modal-close"> </div>
</script>
and this is the PHP which controls the data:
<?php
public static function add_order_item() {
function new_prices($backend_prod_id){
$pluginpath = "/home/#####/wp-content/plugins/codecanyon-7104096-woo-table-based-pricing/";
include_once $pluginpath . 'woocommerce-price-table.php';
$height = $_POST['wpti_x']; //This is empty??? Hardcoded works fine
$width = $_POST['wpti_y']; //This is empty??? Hardcoded works fine
$prices = get_prices($width, $height, $backend_prod_id);
$json_string = json_encode($prices); //json encode prices
$obj = json_decode($json_string, true);
return $obj['product_price'];
}
check_ajax_referer( 'order-item', 'security' );
$item_to_add = sanitize_text_field( $_POST['item_to_add'] );
$order_id = absint( $_POST['order_id'] );
// Find the item
if ( ! is_numeric( $item_to_add ) ) {
die();
}
$post = get_post( $item_to_add );
if ( ! $post || ( 'product' !== $post->post_type && 'product_variation' !== $post->post_type ) ) {
die();
}
$_product = wc_get_product( $post->ID );
$order = wc_get_order( $order_id );
$order_taxes = $order->get_taxes();
$class = 'new_row';
// Set values
$item = array();
$item['product_id'] = $_product->id;
$item['variation_id'] = isset( $_product->variation_id ) ? $_product->variation_id : '';
$item['variation_data'] = $item['variation_id'] ? $_product->get_variation_attributes() : '';
$item['name'] = $_product->get_title();
$item['tax_class'] = $_product->get_tax_class();
$item['qty'] = 1;
$item['line_subtotal'] = new_prices($_product->id);
$item['line_subtotal_tax'] = '';
$item['line_total'] = new_prices($_product->id);
$item['line_tax'] = '';
// Add line item
$item_id = wc_add_order_item( $order_id, array(
'order_item_name' => $item['name'],
'order_item_type' => 'line_item'
) );
// Add line item meta
if ( $item_id ) {
wc_add_order_item_meta( $item_id, '_qty', $item['qty'] );
wc_add_order_item_meta( $item_id, '_tax_class', $item['tax_class'] );
wc_add_order_item_meta( $item_id, '_product_id', $item['product_id'] );
wc_add_order_item_meta( $item_id, '_variation_id', $item['variation_id'] );
wc_add_order_item_meta( $item_id, '_line_subtotal', $item['line_subtotal'] );
wc_add_order_item_meta( $item_id, '_line_subtotal_tax', $item['line_subtotal_tax'] );
wc_add_order_item_meta( $item_id, '_line_total', $item['line_total'] );
wc_add_order_item_meta( $item_id, '_line_tax', $item['line_tax'] );
// Since 2.2
wc_add_order_item_meta( $item_id, '_line_tax_data', array( 'total' => array(), 'subtotal' => array() ) );
// Store variation data in meta
if ( $item['variation_data'] && is_array( $item['variation_data'] ) ) {
foreach ( $item['variation_data'] as $key => $value ) {
wc_add_order_item_meta( $item_id, str_replace( 'attribute_', '', $key ), $value );
}
}
do_action( 'woocommerce_ajax_add_order_item_meta', $item_id, $item );
}
$item = apply_filters( 'woocommerce_ajax_order_item', $item, $item_id );
include( 'admin/meta-boxes/views/html-order-item.php' );
// Quit out
die();
}
?>
I cant figure out why it does not add the width ($_POST['wpti_x']) and height ($_POST['wpti_x']) to POST. If the values are hardcoded, it works fine.
Any help would be appreciated.
You will need to edit meta-boxes-order-min.js, as that is where the functionality you are trying to use exists in woocommerce.
If you want to POST those values, you may need to edit the add_item function.
I hope this helps, and remember if you update woocommerce, you edits will be lost. so this is really not advised.
I am creating a woocommerce theme and I have product variations i.e. size which is displayed on product details page but problem is that I want to get all variations in my custom php page by using product id, can any one help me.
Thanks in advance.
You can use: $available_variations = $product->get_available_variations();
If this is a single style template, make sure you add global $product; near the top.
From there, you can foreach through the variations and do as you wish... since you didn't have any specific output I hope that this sets you on the right track.
You can use the code below:
global $woocommerce, $product, $post;
// test if product is variable
if ($product->is_type( 'variable' ))
{
$available_variations = $product->get_available_variations();
foreach ($available_variations as $key => $value)
{
//get values HERE
}
}
The following code:
$variations = $product->get_available_variations();
will return all variantion of product
This function is put where you show all variation:
\wp-content\plugins\woocommerce\templates\single-product\add-to-cart\variable.php
A common problem often starts from changing "In stock" or "Out of stock" labels for a single product. There are few solutions out there, where you need to change functions.php file and add a new filter.
The problems get more complicated when you need to modify this for product variations.
This may be your solution: http://bucketpress.com/changing-stock-availability-text-for-product-variations
In order to display available variations of the product you need to modify variable.php file, which can be found in
/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/
Find this:
<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 ) ) ?>">
and before tag paste this code:
foreach( $available_variations as $i => $variation ) {
//check if variation has stock or not
if ( $variation['is_in_stock'] ) {
// Get max qty that user can purchase
$max_qty = $variation['max_qty'];
// Prepare availability html for stock available instance
$availability_html = '<p class="stock in-stock">' . $max_qty . ' units available for your purchase.' . '</p>';
} else {
// Prepare availability html for out of stock instance
$availability_html = '<p class="stock out-of-stock">Oops, we have no stock left.</p>';
}
$available_variations[$i]['availability_html'] = $availability_html; }
Don't forget to move php end tag "?>" from this line
do_action( 'woocommerce_before_add_to_cart_form' ); ?>
after your new code.
So the complete variable.php file should like something like this (WooCommerce 3.2.4):
<?php
/**
* Variable product add to cart
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/add-to-cart/variable.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* #see https://docs.woocommerce.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 3.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
global $product;
$attribute_keys = array_keys( $attributes );
do_action( 'woocommerce_before_add_to_cart_form' );
// Your New Code goes here:
foreach( $available_variations as $i => $variation ) {
// check if variation has stock or not
if ( $variation['is_in_stock'] ) {
// Get max qty that user can purchase
$max_qty = $variation['max_qty'];
// Prepare availability html for stock available instance
$availability_html = '<p class="stock in-stock">Available: ' . $max_qty . '</p>';
} else {
// Prepare availability html for out of stock instance
$availability_html = '<p class="stock out-of-stock">Out of stock!</p>';
}
$available_variations[$i]['availability_html'] = $availability_html;
} ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo htmlspecialchars( wp_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.', 'woocommerce' ); ?></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( stripslashes( urldecode( $_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', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap">
<?php
/**
* woocommerce_before_single_variation Hook.
*/
do_action( 'woocommerce_before_single_variation' );
/**
* woocommerce_single_variation hook. Used to output the cart button and placeholder for variation data.
* #since 2.4.0
* #hooked woocommerce_single_variation - 10 Empty div for variation data.
* #hooked woocommerce_single_variation_add_to_cart_button - 20 Qty and cart button.
*/
do_action( 'woocommerce_single_variation' );
/**
* woocommerce_after_single_variation Hook.
*/
do_action( 'woocommerce_after_single_variation' );
?>
</div>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<?php endif; ?>
<?php do_action( 'woocommerce_after_variations_form' ); ?>
</form>
<?php
do_action( 'woocommerce_after_add_to_cart_form' );
All credits to "Kevin" from: http://bucketpress.com/author/base-admin
Try this,
<?php
$name_size = get_post_meta($_GET['pr_id'],'product_size', true);
$t_shirt_sizes_array = wp_get_post_terms($_GET['pr_id'],'pa_size');
$t_shirt_size = array();
$t_shirt_price = array();
for($scnt = 0; $scnt < count($t_shirt_sizes_array); $scnt++){
$t_shirt_size[] = $t_shirt_sizes_array[$scnt]->name;
$t_shirt_price[] = $t_shirt_sizes_array[$scnt]->slug;
$t_shirt_size_id[] = $t_shirt_sizes_array[$scnt]->term_id;
}
$cnt = 1;
for($i = 0; $i < count($t_shirt_size); $i++){
$name_size = $t_shirt_size[$i];
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name =
'".$t_shirt_size[$i]."'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = ".$_GET['pr_id'];
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
echo $name_size;
$_product = new WC_Product_Variation( $variation_id[0] );
echo $_product->get_price();
}
$price = $t_shirt_price[$i];
$cnt++;
}
?>