PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 9, 2022

[FIXED] How to display the variation name in Woocommerce Items

 May 09, 2022     methods, php, product, woocommerce, wordpress     No comments   

Issue

I am trying to make a Ajax popup cart where product will be add dynamically. Everything is working fine except the product variation. when a variable product added to cart its not showing the variation name:

<?php 
$items = WC()->cart->get_cart();
    foreach($items as $item => $values) {
        $_product       =  wc_get_product( $values['data']->get_id() );
        $product_link   = get_permalink( $values['data']->get_id() );
        $title          = $_product->get_title();
        $variations     = wc_get_formatted_cart_item_data($values,true);
        echo '<a href="'.$product_link.'">'. $title.'</a>';
        echo $variations;
    }
?>

Solution

First, you just need to use WC_Product method get_name() (see in the template cart/minicart.php on line 36) replacing in your code the line:

$title          = $_product->get_title();

with:

$title          = $_product->get_name();

Important Note: In some cases you will need to add the following lines (depending on what you want to display and where):

// Force displaying variation attributes in the product name (in cart/minicart/checkout)
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_true' );
// (Optional) Force displaying product variation attributes as separated formatted metadata (in cart/minicart/checkout)
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );

Code goes in functions.php file of the active child theme (or active theme).

To test it, once added this code to your theme's functions.php file, empty the cart first, as cart fragments are cached in mini cart (Ajax).

This time it will show the variation name.



Answered By - LoicTheAztec
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing