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++;
}
?>
Related
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 ;-)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
In WooCommerce, how to avoid image change on selecting variations (of a variable products)?
I modified the core add-to-cart-variation.js and add-to-cart-variation.min.js and changed two parts of the code that caused the image slide after variation change. It might not be a good idea to modify the core js file you might be able to dequeue the original and enqueue your new file version that you uploaded in the functions.php so the change you made will still be there on a WooCommerce update.
add-to-cart-variation.min.js :
replace: r.trigger("click"),
WITH-->> /*r.trigger("click"),*/
also replace: &&i.trigger("woocommerce_gallery_reset_slide_position")
WITH-->> /*&&i.trigger("woocommerce_gallery_reset_slide_position")*/
add-to-cart-variation.js :
replace: slideToImage.trigger( 'click' );
WITH-->> //slideToImage.trigger( 'click' );
also replace: $product_gallery.trigger( 'woocommerce_gallery_reset_slide_position' );
WITH-->> /*$product_gallery.trigger( 'woocommerce_gallery_reset_slide_position' );*/
If you don’t need image changed for variations you should remove images from variations assigned. I suppose it is embedded with plugin or theme features
As the image change is mainly handle by Woocommerce gallery javascript (jQuery) as it's a live event on client side.
The only possible way, should be to remove all images from variations, adding them only in the parent variable product.
If you need to display a variation product image in cart and email notifications, you will use the following code:
// Parent product variation image on cart
add_filter( 'woocommerce_cart_item_thumbnail', 'filter_order_item_thumbnail', 20, 2 );
function filter_order_item_thumbnail( $image, $cart_item, $cart_item_key ) {
if( $cart_item['variation_id'] > 0 ) {
$parent = wc_get_product($cart_item['product_id']);
$image = $parent->get_image();
}
return $image;
}
// Parent product variation image on email notifications
add_filter( 'woocommerce_order_item_thumbnail', 'filter_order_item_thumbnail', 20, 2 );
function filter_order_item_thumbnail( $image, $item ) {
$product = $item->get_product();
if( $product->is_type('variation') ) {
$parent = wc_get_product($item->get_product_id());
$image = $parent->get_image(array(32, 32));
}
return $image;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Create a new file in this theme directory wp-content/themes/<YOUR_THEME_NAME>/woocommerce/single-product/add-to-cart/variable.php and add this code
<?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/
* #package WooCommerce/Templates
* #version 3.5.5
*/
defined( 'ABSPATH' ) || exit;
global $product;
$new_available_variations = $available_variations;
for($i=0;$i<sizeof($new_available_variations);$i++){
$image_url = wp_get_attachment_url( $product->get_image_id() );
$image_src = wp_get_attachment_image_src( $product->get_image_id() );
$new_available_variations[$i]['image']['url'] = $image_url;
$new_available_variations[$i]['image']['src'] = $image_url;
$new_available_variations[$i]['image']['full_src'] = $image_url;
$new_available_variations[$i]['image']['gallery_thumbnail_src'] = $image_url;
$new_available_variations[$i]['image']['thumb_src'] = $image_url;
$new_available_variations[$i]['image']['url'] = $image_url;
$new_available_variations[$i]['image']['url'] = $image_url;
$new_available_variations[$i]['image']['full_src_w'] = $image_src[1];
$new_available_variations[$i]['image']['full_src_h'] = $image_src[2];
$new_available_variations[$i]['image']['thumb_src_w'] = $image_src[1];
$new_available_variations[$i]['image']['thumb_src_h'] = $image_src[2];
$new_available_variations[$i]['image']['src_w'] = $image_src[1];
$new_available_variations[$i]['image']['src_h'] = $image_src[2];
}
$attribute_keys = array_keys( $attributes );
$variations_json = wp_json_encode( $new_available_variations );
$variations_attr = function_exists( 'wc_esc_json' ) ? wc_esc_json( $variations_json ) : _wp_specialchars( $variations_json, ENT_QUOTES, 'UTF-8', true );
// echo '<pre>', print_r($new_available_variations,1), '</pre>';
do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" action="<?php echo esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); ?>" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo $variations_attr; // WPCS: XSS ok. ?>">
<?php do_action( 'woocommerce_before_variations_form' ); ?>
<?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
<p class="stock out-of-stock"><?php echo esc_html( apply_filters( 'woocommerce_out_of_stock_message', __( '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 esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); // WPCS: XSS ok. ?></label></td>
<td class="value">
<?php
wc_dropdown_variation_attribute_options(
array(
'options' => $options,
'attribute' => $attribute_name,
'product' => $product,
)
);
echo end( $attribute_keys ) === $attribute_name ? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) ) : '';
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="single_variation_wrap">
<?php
/**
* Hook: woocommerce_before_single_variation.
*/
do_action( 'woocommerce_before_single_variation' );
/**
* Hook: woocommerce_single_variation. 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' );
/**
* Hook: woocommerce_after_single_variation.
*/
do_action( 'woocommerce_after_single_variation' );
?>
</div>
<?php endif; ?>
<?php do_action( 'woocommerce_after_variations_form' ); ?>
</form>
<?php
do_action( 'woocommerce_after_add_to_cart_form' );
The following code adds editable short description of the product as order item metadata in Woocommerce. The code is working as expected for simple products but for variable products the $item->get_formatted_meta_data('') returns empty array at first and only after editing the item, the short description shows as expected.
<?php
/**
* Add product description meta to order item
*/
add_action( 'woocommerce_before_order_itemmeta', 'add_order_item_description_meta', 10, 3 );
function add_order_item_description_meta( $item_id, $item, $product ) {
// Add only if not present
$product_description_meta = wc_get_order_item_meta( $item_id, '_product_short_desc', true );
if( empty( $product_description_meta ) ) {
if( $product->is_type('variation') ) {
$parent_product = wc_get_product( $product->get_parent_id() );
$excerpt = $product->get_description();
$excerpt = empty($excerpt) ? $parent_product->get_short_description() : $excerpt;
} else {
$excerpt = $product->get_short_description();
}
wc_add_order_item_meta( $item_id, '_product_short_desc', $excerpt );
}
}
/**
* Hide product description meta
*/
add_filter( 'woocommerce_hidden_order_itemmeta', 'custom_hidden_order_itemmeta' );
function custom_hidden_order_itemmeta( $hidden_order_itemmeta ) {
$hidden_order_itemmeta[] = '_product_short_desc';
return $hidden_order_itemmeta;
}
/**
* Add custom column to display product description meta
*/
add_action( 'woocommerce_admin_order_item_headers', 'custom_admin_order_items_headers', 20, 1 );
function custom_admin_order_items_headers( $order ) {
echo '<th class="item_short_description">';
echo __('Short Description', 'woocommerce') . '</th>';
}
/**
* Custom column content in table
*/
add_action( 'woocommerce_admin_order_item_values', 'custom_admin_order_item_values', 20, 3 );
function custom_admin_order_item_values( $product, $item, $item_id ) {
?>
<td class="product_short_desc">
<?php
if( $meta_data = $item->get_formatted_meta_data( '' ) ) {
$meta = array_filter( $meta_data, function( $value, $key ) {
return '_product_short_desc' === $value->key;
}, ARRAY_FILTER_USE_BOTH );
if( ! empty( $meta ) ) {
// Get the key of first value
$meta_id = 0;
foreach( $meta as $key => $value) {
$meta_id = $key;
break;
}
$product_desc_meta = $meta[$meta_id];
?>
<div class="view">
<?php echo wp_kses_post( force_balance_tags( $product_desc_meta->display_value ) ); ?>
</div>
<div class="edit" style="display: none;">
<input type="hidden" placeholder="<?php esc_attr_e( 'Name (required)', 'woocommerce' ); ?>" name="meta_key[<?php echo esc_attr( $item_id ); ?>][<?php echo esc_attr( $meta_id ); ?>]" value="<?php echo esc_attr( $product_desc_meta->key ); ?>" />
<textarea placeholder="<?php esc_attr_e( 'Short Description', 'woocommerce' ); ?>" name="meta_value[<?php echo esc_attr( $item_id ); ?>][<?php echo esc_attr( $meta_id ); ?>]"><?php echo esc_textarea( rawurldecode( $product_desc_meta->value ) ); ?></textarea>
</div>
<?php
}
} else {
echo '-';
} ?>
</td>
<?php
}
For simple product the Short Description column is displaying value when adding the item to order but for variable product it shows - as it goes into else condition of function custom_admin_order_item_values at first and then after editing the item, it displays the short description correctly.
Here's screenshot for the simple and variable product added without being edited:
Please help to find the underlying bug.
UPDATE: Anyone trying to help on this, I can confirm that problem lies either in first hooked function i.e. add_order_item_description_meta or in the Woocommerce core because even if you comment out the later hooks and their respective functions the same problem is there.
So the meta data for the variable product is not loaded first time when product is added to the order, but when it is refreshed such as by editing it, the meta data loads fine. Also note as I mentioned earlier the meta data loads as expected for simple products at first time without need of refresh of order items.
I have tried your code and it works using
$meta = array_filter( $meta_data, function( $value ) {
return '_product_short_desc' === $value->key;
} );
instead of
$meta = array_filter( $meta_data, function( $value, $key ) {
return '_product_short_desc' === $value->key;
}, ARRAY_FILTER_USE_BOTH );
and adding if statement in add_order_item_description_meta
function add_order_item_description_meta( $item_id, $item, $product ) {
if ( !$product ) return;
/* … rest of the code … */
Here's a screenshot:
Since I don't know how to disable the quantity hover. I'm going to use the dropdown quantity. I have the code but the quantity dropdown doesn't update the right number in the cart page. Do you know why? For example if I my quantity is 3, it will say 1 in my cart page.
Also when I choose my quantity 3, and then I click the "add to cart button" , it will say "Please choose product options" because I didn't choose a size. The quantity start from 1 again, it doesn't stay the same what I chose before.
Also this have to work in mobile too. Can you help me with that? Thanks
// override the quantity input with a dropdown
function woocommerce_quantity_input() {
global $product;
global $post;
global $prod_quant_default;
$prod_quant_default = 1; // <----------- Default Amount
$category_ID = '26'; // <----------- Case Category
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
// Sets QTY for Cases (Cat 26)
if ($product_cat_id == $category_ID){
$prod_quant_default = 1;
//break;
}
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left; margin-right:10px;', $product )
);
if ( ! empty( $defaults['min_value'] ) )
$min = $defaults['min_value'];
else $min = 1;
if ( ! empty( $defaults['max_value'] ) )
$max = $defaults['max_value'];
else $max = 10;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
global $prod_quant_default;
if ($count == $prod_quant_default) {
$selected = ' selected="selected" ';
}
else {
$selected = null;
}
$options .= '<option' . $selected . ' value="' . $count . '">' . $count . '</option>';
}
echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
}
I think the best way is to override the quantity-input.php template that is called by the woocommerce_quantity_input() function.
Your theme's new woocommerce/global/quantity-input.php template override:
<?php
/**
* Product quantity inputs
*
* This template can be overridden by copying it to yourtheme/woocommerce/global/quantity-input.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 http://docs.woothemes.com/document/template-structure/
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
?>
<div class="quantity">
<?php
$options = '';
for ( $count = $args['min_value']; $count <= $args['max_value']; $count = $count+$args['step'] ) {
$options .= '<option' . selected( $args['input_value'], $count, false ) . ' value="' . $count . '">' . $count . '</option>';
}
if ( $options ){ ?>
<select name="<?php echo esc_attr( $args['input_name'] ); ?>" title="<?php echo esc_attr_x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) ?>" class="qty" /><?php echo $options;?></select>
<?php } else {
printf( '%s <input type="hidden" name="%s" value="%s" />', $args['input_value'], $args['input_name'], $args['input_value'] );
} ?>
</div>
Note: All my local testing is on bleeding-edge WooCommerce, I'm not sure if the 2.5 version listed here will work with current WC2.4.X. But the idea would be similar.
If you need to modify the min/max defaults, you can do so via filter in your theme's functions.php:
add_filter( 'woocommerce_quantity_input_max', 'so_input_max', 20, 2 );
function so_input_max( $max, $product ){
return 10;
}
add_filter( 'woocommerce_quantity_input_min', 'so_input_min', 20, 2 );
function so_input_min( $min, $product ){
return 1;
}
Remember, these are just the defaults in the case where no min/max are directly specified when calling the woocommerce_quantity_input() function.
EDIT
I improved the template above to have the right input names/values ( <select> definitely had the wrong name attribute) and to show a hidden quantity input in the case where there are no options (this is what Woo does when a product is sold individually so that so that the quantity is not lost when updating the cart).
There are no options on the cart page because the cart.php template doesn't set a max if the item's stock isn't managed, or if backorders are allowed. A null max kills the for loop in our template. With the min at 0 and the max at null, there's nothing to loop through and no <option> tags are created.
To fix that we can filter the woocommerce_cart_item_quantity in the cart.php template. This would also need to be added to your functions.php.
add_filter( 'woocommerce_cart_item_quantity', 'so_cart_item_quantity', 10, 3 );
function so_cart_item_quantity( $product_quantity, $cart_item_key, $cart_item ){
$_product = $cart_item['data'];
if ( ! $_product->is_sold_individually() ) {
$max_value = $_product->backorders_allowed() ? '' : $_product->get_stock_quantity();
// set a max of 10 as default if max is null
$max_value = $max_value != '' ? $max_value : 10;
$product_quantity = woocommerce_quantity_input( array(
'input_name' => "cart[{$cart_item_key}][qty]",
'input_value' => $cart_item['quantity'],
'max_value' => $max_value,
'min_value' => '0'
), $_product, false );
}
return $product_quantity;
}
There is an issue where on cart update using Woo 2.6 with AJAX, the select breaks. If you remove the trailing / here:
...', 'woocommerce' ) ?>" class="qty" />
To:
', 'woocommerce' ) ?>" class="qty">
It will now work.
I've recently been working with Rohil_PHPBeginner who has been excellent and solved a big problem i had.
I need to now remove a particular category from the category list on the woo commerce product page.
Example: https://www.artgiftedbygod.co.uk/online-art-shop/jane-brighton/dawn-2/
You will see the Title, Artist name (which Rohil_PHPBeginner) solved, Price, Description, Availability, Buy Button, and then categories.
I would like to show all categories except the Artist name as this is now displayed under its own section.
This is the code that is found on the PHP page:
<?php
/**
* Single Product Meta
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $product;
$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
?>
<div class="product_meta">
<?php do_action( 'woocommerce_product_meta_start' ); ?>
<?php if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variable' ) ) ) : ?>
<span class="sku_wrapper"><?php _e( 'SKU:', 'woocommerce' ); ?> <span class="sku" itemprop="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : __( 'N/A', 'woocommerce' ); ?></span>. </span>
<?php endif; ?>
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', $cat_count, 'woocommerce' ) . ' ', '.</span>' ); ?>
<?php echo $product->get_tags( ', ', '<span class="tagged_as">' . _n( 'Tag:', 'Tags:', $tag_count, 'woocommerce' ) . ' ', '.</span>' ); ?>
<?php do_action( 'woocommerce_product_meta_end' ); ?>
`
Try this :
<?php
/**
* Single Product Meta
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $product;
$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
?>
<div class="product_meta">
<?php do_action( 'woocommerce_product_meta_start' ); ?>
<?php if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variable' ) ) ) : ?>
<span class="sku_wrapper"><?php _e( 'SKU:', 'woocommerce' ); ?> <span class="sku" itemprop="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : __( 'N/A', 'woocommerce' ); ?></span>.</span>
<?php endif; ?>
<?php
$cat_array = array();
$term_list = wp_get_post_terms($post->ID, 'product_cat', array("fields" => "all")); //get array containing category details
foreach($term_list as $cat_list)
{
array_push($cat_array, $cat_list->term_id);
}
$cat_id = ($term_list[0]->parent); //get parent category ID from the above generated array
$termchildren = get_term_children( '90' , 'product_cat' ); //New Line in Updattion -1
$final_result = array_diff($cat_array,$termchildren);
$new_ary = array_values($final_result);
$final_result_size = sizeof($new_ary);
$i=0;$j=0;
for($i=0;$i<$final_result_size;$i++){
$new_cat_id = $new_ary[$i];
$cat_url = get_term_link ($new_cat_id, 'product_cat'); //get link of parent ID
$term = get_term( $new_cat_id, 'product_cat' ); //Get Name of the parent from the parent ID
$name = $term->name; //Store it into an varialbe
if($j == 0):
echo "Categories: ";
endif;
echo "<a href='".esc_url($cat_url)."'>".$name."</a>";
if($i == ($final_result_size-1)):
echo "";
else:
echo ", ";
endif;
$j++;
}
?>
<?php echo $product->get_tags( ', ', '<span class="tagged_as">' . _n( 'Tag:', 'Tags:', $tag_count, 'woocommerce' ) . ' ', '.</span>' ); ?>
<?php do_action( 'woocommerce_product_meta_end' ); ?>
</div>