Display Woocommerce variable product dimensions - wordpress

Have Woocommerce setup with a range of variable products. In the variable tab I've setup a unique price, image, description, weight & dimensions for each item.
All variable data displays as expected on the front-end except the dimensions & weight.
Despite hours of searching, I cannot find any documentation, tutorials, hints on how to hook into it.
Have Woocommerce templates setup and know that I will need to hook into the do_action( 'woocommerce_single_variation' ); in variable.php.
Anyone know how to get each variable's dimensions & weight to display beneath the variable description?

If you have the variation ID, you can use it to create a new WC_Product(). This object will then have properties available on it for the $length, $width, and $height. See the docs here (at the bottom under "Magic Properties").
To get the variations for a given product, you can use the global $product and then the get_available_variations() function.
global $product
$variations = $product->get_available_variations();
foreach ( $variations as $variable_array ){
$variation = new WC_Product( $variable_array['variation_id'] );
echo "The length is {$variation->length}.";
}

If you want to display additional information regarding your variable product add this function to your child theme’s function.php (or plugin). You’ll probably want to alter the html tags to fit your theme:
add_filter( 'woocommerce_product_additional_information', 'tim_additional_tab', 9 );
function tim_additional_tab( $product ){
$variations = $product->get_available_variations();
//print the whole array in additional tab and examine it
//echo '<pre>';
//print_r($variations);
//echo '</pre>';
//html and style to your likings
foreach ( $variations as $key ){
echo $key['image']['title'].'<br>';
echo $key['weight_html'].'<br>';
echo $key['dimensions_html'].'<br>';
}
}

Related

How do I get upsell for variable product in Woocommerce?

Short version: How do I get upsell for variable product in Woocommerce?
Longer version:
I need the product ids for upsells.
My old code contains depracated code:
$upsells = $product->get_upsells(); // $product is instace of WC_Product_Variable::
The call should be the following:
$upsells = $product->get_upsells_ids(); // $product is WC_Product::
But a different class.
I tried to get the parent instance using wc_get_product($product->get_parent_id()) - but fail.
So, given the instance of WC_Product_Variable how do I get to the parent method WC_Product::$product->get_upsells_ids() ??
Thanks
The get_upsells-ids() method does not exist. Try get_upsell_ids().
The following code will show you all the upsell ids for the product you are currently visiting:
// quick test to check upsell ids
add_action( 'woocommerce_before_single_product', 'echo_upsell_ids' );
function echo_upsell_ids() {
global $product;
$product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();
$product = wc_get_product( $product_id );
$upsell_ids = $product->get_upsell_ids();
echo '<pre>' . print_r( $upsell_ids, true ) . '</pre>';
}
I have tested the code and it works. The snippet goes into your child theme's functions.php file.

Woocommerce adding custom product count to products only in specific category and in specific page

I have caught onto some of the logic but battling with how to implement:
display my custom product count only on products in a specific product category
also display product count only on a specific custom WP page (which I used the product_category shortcode)
My code in functions.php is as follows and it does add the $top50_counter value before the product thumbnail but it is doing it site-wide, hence why I need to narrow it down as per my points above.
/* ADD NUMBERING TO TOP 50 LIST PRODUCTS */
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_before_shop_loop_item', 5);
$top50_counter=1;
function custom_before_shop_loop_item() {
global $top50_counter;
echo '<h1>'.$top50_counter.'</h1>';
$top50_counter++;
}
I'm assuming I have to use the $terms = get_the_terms function in there somehow?
You need to use is_page and has_term conditionals. Try re-factoring the code to the following.
/* ADD NUMBERING TO TOP 50 LIST PRODUCTS */
add_action( 'woocommerce_before_shop_loop_item_title', 'custom_before_shop_loop_item', 5);
$top50_counter=1;
function custom_before_shop_loop_item() {
global $top50_counter;
/* Replace 42 with the actual page ID and "your-category" with the actual category slug */
if( ( is_page( 42 ) ) || ( has_term( 'your-category' , 'product_cat') ) ):
echo '<h1>'.$top50_counter.'</h1>';
$top50_counter++;
endif;
}
P.S: untested code.
Try this. Use corresponding category name and custom wp page slug in the following function.
function custom_before_shop_loop_item(){
global $post, $term, $top50_counter;
$id = $post->ID;
$taxonomy = 'product_cat';
$terms = get_the_terms( $id, $taxonomy );
if( ($terms[0]->name == 'Category Name') || ($post->post_name == 'custom-wp-page-slug') ){
echo '<h1>'.$top50_counter.'</h1>';
}
$top50_counter++;
}
Hope this helps.

Get a sidebar widget that show products of the same categories in Woocommerce

I’m trying to set a sidebar in the single product page that show all products of the same categories as the product displayed.
That's how I proceed:
1) First I’ve created a sidebar called “Products_of_same_Category” to put in there a widget to show what I needed, then in function.php of my child theme I added the following snippet to execute php code in text widget:
// Enable PHP in widgets
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
2) Then when I saw that the snippet runs ok I added that code to test it:
<?php
$prod=get_the_term_list( $post->ID, 'product_cat');
echo $prod;
?>
And all worked fine, it gave me the exact name of the current category of the product displayed in the single product page.
3) So I've tried another test, deleting the previous code, to view if a shortcode translated in php should works in a widget too (writing at this time the exact category name requested, in this case "towels" - in the code below I substitute it with THE-CATEGORY-I-LIKE):
<?php echo do_shortcode('[product_category category=“THE-CATEGORY-I-LIKE” per_page="20" columns="1" orderby="title" order="desc"]'); ?>`
And all was again well done!
4) Finally I mixed all in this code to show the list of products of same categories but something goes wrong:
<?php $prod=get_the_term_list( $post->ID, 'product_cat', '', '', '' );
echo do_shortcode('[product_category category="'.$prod.'" per_page="20" columns="1" orderby="title" order="desc"]'); ?>
In last case the code doesn't display anything. I don't understand where I made mistakes, the syntax is wrong or the solving approach is illogical?
I really appreciate any help about this.
The problem is how you get the category slug. get_the_term_list will give you a formatted linked list of the categories, so it will display category names, not category slugs, which are different things. "Towels" would be your category name, but the category slug would be "towels". And product_category shortcode expect a slug, not a name.
The correct approach to get the category product slug is the following:
$terms = get_the_terms($post, 'product_cat');
if($terms && ! is_wp_error($terms)) {
foreach($terms as $term) {
echo do_shortcode('[product_category category="'.$term->slug.'" per_page="20" columns="1" orderby="title" order="desc"]');
}
}
This will display the products of all the categories associated to your product. See get_the_terms doc for reference.
In order to remove from the results the current product shown, you can make use of the woocommerce_shortcode_products_query filter. It isn't documented, but you can find it by looking at the product_category shortcode code located in includes/class-wc-shortcodes.php. In the product_category() method you will find the following line:
$return = self::product_loop( $query_args, $atts, 'product_cat' );
Where $query_args is a WP_Query parameters array. In the same class you will find the method product_loop() called here and see the following:
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $query_args, $atts ) );
So the query arguments are filtered - you will be able to work with that to add the desirated behavour. What you want to do is to use the post__not_in parameter to the query like this:
function remove_current_product_from_wc_shortcode($args, $atts) {
if(is_product()) { // check if we're on a single product page
$args['post__not_in'] = array(get_queried_object_id());
}
return $args;
}
add_filter('woocommerce_shortcode_products_query', 'remove_current_product_from_wc_shortcode');
This code should go in your theme functions.php - please not this is untested, so if it doesn't work look at the get_queried_object_id() return if it contain the current product ID.

Woocommerce Conditional Tags in Functions.php

I have the following code to echo an announcement when a customer visits a specific product category, but I can't get it to work:
function gvi_announcement() {
if ( is_product_category( 'accessories' ) ) {
echo '<p class="my-alert"><span>This is an accessory</span></p>';
};
}
add_action ('woocommerce_single_product_summary', 'gvi_announcement' , 10);
It works fine without the conditional tag, but I've exhausted every other way to make this work using conditions.
UPDATE:
Thanks for your suggestion #Ibrahim, but I still cannot get it to work. Here's my code now:
function gvi_announcement() {
global $post;
$terms = get_the_terms( $post->ID, 'accessories' );
if ( $terms ) {
echo '<p class="my-alert"><span>This is an accessory</span></p>';
};
}
add_action ('woocommerce_single_product_summary', 'gvi_announcement' , 10);
The condition is_product_category is used to check if the page you are viewing is a product category page or not and the action woocommerce_single_product_summary is used on single product page. So the condition will invariably fail.
In a single product page, you will have to use get_the_terms to get the product categories to which the product belongs to. Something like this :
global $post;
$categories = get_the_terms( $post->ID, 'product_cat' );
You can then apply your conditions.

Prevent WooCommerce brand from being sellable

Hi there we have an online store running on WooCommerce and using the WooCommerce brands plugin (http://docs.woothemes.com/document/wc-brands/) but there is one brand that we are allowed show online with price but not allowed to actually sell.
Is there a function I can add for this particular brand to functions.php that will change the add to cart button in category or widget layout to "more info" and link to the product and then on the product page instead of the add to cart section just have a text message saying to call the store.
You can filter WooCommerce's is_purchasable method. Any item that returns false will not be able to be purchased.
function so_26378581_purchasable( $purchasable, $product ){
if ( has_term( 'your-brand', 'product_brand', $product->id ) ){
$purchasable = false;
}
return $purchasable;
}
add_action( 'woocommerce_is_purchasable', 'so_26378581_purchasable', 10, 2 );
This uses conditional logic to test whether the $product in question has the your-brand term in the product_brand taxonomy... via the has_term() function.
By the way, this is not the type of functionality that belongs in functions.php. Your theme should only be concerned with display/appearances. I would recommend that you make this its own plugin, or add to to a site specific snippets plugin.
to me it made no error message, but it did not work, as applied in my test subject and stand continues with two products of different brands.
I put this code:
function so_26378581_purchasable( $purchasable, $product ){
if ( has_term( 'product_brand', 'product_brand', $product->id ) ){
$purchasable = false;
}
return $purchasable;
}
add_action( 'woocommerce_is_purchasable', 'so_26378581_purchasable', 10, 2 );

Resources