Woocommerce variable product on custom single product template - wordpress

I've developed custom single product template for Woocommerce for products which works correctly and as intended. The problem is that now the customer wants some different variations of products that are being sold and I can't work out how to add this functionality.
Currently on the product page I have used functions such as the_post_thumbail() and get_post_meta() to display various information about the product such as price and excerpt entered in the product post type. I then use do_action('woocommerce_simple_add_to_cart') along with a few other buttons.
This all works fine but the problem is now with the variation feature. I found the following code which echos out the product variations ID and Variation Type but I'm not sure how to implement this into a working system. Any help or guidance would be appreciated.
global $product, $post;
$variations = $product->get_available_variations();
foreach ($variations as $key => $value) {
echo 'variation ID'.$value['variation_id'];
foreach ($value['attributes'] as $attr_key => $attr_value) {
echo $attr_key.': '.$attr_value;
}
}

I found the answer so I'm just posting this here in case anyone else has the same question. It was as simple as changing do_action('woocommerce_simple_add_to_cart') to do_action('woocommerce_variable_add_to_cart'). This displays the product variations in a list box and has the add to cart button.

Related

Wordpress create lists of favorites for users

I have to create lists of favorites products in woocommerce.
First I've created a new custom post type to manage my products list.
Now I need to add my products list to my custom type but I don't know how to do this.
The final result will be a page listing all my lists for a user contains all added products by user.
Can anyone help ?
I found a pretty comprehensive (paid) plugin by WooCommerce that does exactly this: WooCommerce Wishlists. I haven't tried it.
WooCommerce Wishlists allows guests and customers to create and add products to an unlimited number of Wishlists. From birthdays to weddings and everything in between, WooCommerce Wishlists is a welcome addition to any WooCommerce store.
Plugin link: https://woocommerce.com/products/woocommerce-wishlists/
Thanks for your answer but i've needed to make my own plugin.
WC Wishlist allow you to make only one list ;)
After some works i've found a solution with meta_user_data to manage my lists.
public function action_create_favorite_list(){
if( ! is_user_logged_in() ) :
return;
endif;
$list = new stdClass();
$list->name = $_POST["list_name"];
$list->items = array();
$fav = get_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', true);
if(empty($fav))
{
add_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', array());
$fav = get_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', true);
}
array_push($fav,$list);
update_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', $fav);
wp_send_json($fav);
wp_die();
}

How To Hide/Remove Additional Checkout Fields for Specific Products On Checkout Page?

I have installed WooCommerce Checkout & Account Field Editor plugin on my WordPress website for adding additional fields to my checkout page. The purpose of installing this plugin is to add three additional fields like how_you_heard_about_our_store, user_membership_level and user_refferer_name to the checkout page for getting additional details from my users.
This is working fine and the user can provide the required information as needed during checkout. However, there is one product Gift Certificate that I need to exclude these additional fields on it when this product is on the checkout page.
The purpose is to hide these fields on this product only. I have 4 different variations of this product and I need these fields to be hidden for each of its variations.
I have tried my following techniques but this is actually for default WooCommerce fields.
https://www.liquidweb.com/kb/way-conditionally-show-hide-checkout-fields-specific-products-product-categories-store/
Also, I have tried the following as well with no luck:
function custom_override_checkout_fields( $fields ) {
unset($fields['order']["how_heard"]);
unset($fields['order']["member_level"]);
return $fields;
}
Is there any specific action or filter for removing additional fields from my checkout page? Any help would highly be appreciated.
add_filter( 'woocommerce_checkout_fields' , 'hide_checkout_fields' );
function hide_checkout_fields( $fields ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$id = $product->get_id();
$products = array(2516, 584, 2454); // Product Ids
if (in_array($id, $products))
{
unset($fields['order']['how_heard']);
unset($fields['order']['member_level']);
}
}
return $fields;
}

Woocommerce - Display product category above product title

I am trying to display specific categories above each of my product titles on the main shop page, the product category page, the single product page and the cart.
Using Shopfront as an example in the image below, I want to display "Nikon" above the titles of these flashes. The products are in multiple categories (Nikon, flashes, camera equipment etc.) but I only want to display the brand (Nikon) above the title. There are multiple brands within the shop (Canon, Nikon, Sony etc.) so the correct brand needs to be shown above the title depending on the maker of the product.
How can I programmatically add the "brand" category above the titles? I was thinking I could call for the categories list associated with each product within the product loop, but somehow whitelist or manually specify within the code which categories are allowed to be output on the front-end.
I have Googled pretty deep on this but can't find anything that gets me heading in the right direction.
Thanks in advance.
This code should solve your problem.You can add this code in functions.php.I tested and confirmed its working for me.
function category_single_product(){
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats && ! is_wp_error ( $product_cats ) ){
$single_cat = array_shift( $product_cats ); ?>
<h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>
<?php }
}
add_action( 'woocommerce_before_shop_loop_item_title', 'category_single_product', 25 );

Woocommerce customize the product name in the cart and order pages

Actually, I already got hook to customize the price in the cart page and all other page.
This is the hook to customize the product price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
Actually, I am using woocommerce plugin. I want a hook to customize the product name displayed in the cart page and all other pages next to cart page.
I want to customize the Product name , i want to add the some static attributes to product name displayed in the cart page, order page ,order details page and all the pages next to cart page.
Thanks in advance
I wanted to share this as I managed to figure out something for my needs. (I only ever have a single item in the order as I've customised WooCommerce for holiday bookings.)
<?php
add_action( 'woocommerce_product_title', 'add_custom_name' );
function add_custom_name( $title ) {
return 'test name';
}
?>
Hopefully someone can elaborate on this further or take what has been done with the custom price code an properly re-purpose it for product names.

Get Category list related to current page in wordpress

i need to display the category list related to the current page (based on selected category in admin.) in wordpress site. i tried with get_the_category($post_id). But it's not working. Thanks.
You can try get_the_terms() instead. It should return an array of cats. Wordpress considers categories a taxonomy, so specifying 'category' as the taxonomy type should do the trick.Hope this helps.
$array_of_cats = get_the_terms( $post->ID, 'category' );
foreach($array_of_cats as $key=> $value){
//output as a list...
}
Function reference here.

Resources