Hide variation info from cart item title in WooCommerce 3+
This filter should work returning a false
value for $should_include_attributes
first argument in woocommerce_product_variation_title_include_attributes
filter hook this way:
add_filter( 'woocommerce_product_variation_title_include_attributes', 'custom_product_variation_title', 10, 2 );
function custom_product_variation_title($should_include_attributes, $product){
$should_include_attributes = false;
return $should_include_attributes;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
It should just work as you expect.
Update: The shorter way is:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
just works too.
Hide specific variation attributes displayed in WooCommerce cart items
You were not that far! You were just missing a simple conditional statement inside that foreach
loop. Something like if ( $data['key'] !== 'Kcalperdag' ):
should do the trick.
I've tested it with a bogus array
and it seems perfect.
We use the $data['key']
as a condition to chose if we want to display or not our content.
<dl class="variation">
<?php
foreach ( $item_data as $data ):
if ( $data['key'] !== 'Kcalperdag' ): ?>
<dt class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>">
<?php echo wp_kses_post( $data['key'] ); ?>:
</dt>
<dd class="<?php echo sanitize_html_class( 'variation-' . $data['key'] ); ?>">
<?php echo wp_kses_post( wpautop( $data['display'] ) ); ?>
</dd>
<?php endif;
endforeach; ?>
</dl>
Remove specific product variation from cart in WooCommerce
You can also use a foreach loop as follow to target and remove a specific variation id from cart:
$remove_variation_id = 41; // The variation id to remove
// loop through cart items
foreach ( WC()->cart->get_cart() as $item_key => $item ) {
// If the targeted variation id is in cart
if ( $item['variation_id'] == $remove_variation_id ) {
WC()->cart->remove_cart_item( $item_key ); // we remove it
break; // stop the loop
}
}
Tested and works.
Replace a certain character in WooCommerce cart item variations title by a pipe
So you should use a custom function hooked in woocommerce_cart_item_name
filter hook, where you will replace the coma by a pipe using str_replace()
php function, this way:
// Tested on WooCommerce version 3+
add_filter( 'woocommerce_cart_item_name', 'custom_item_name', 10, 3 );
function custom_item_name( $item_name, $cart_item, $cart_item_key ){
$new_item_name = str_replace(',', ' |', $item_name);
return $new_item_name;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works. It will replace the coma by a pipe in cart and checkout items title.
Related answer: WooCommerce 3.0 - hide variation info in product title
Display variations attributes values as separate rows in Woocommerce cart / checkout
Try the following, that will display your product variations attributes as separated rows, below the cart item title:
add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
add_filter( 'woocommerce_is_attribute_in_product_name', '__return_false' );
Code goes in function.php file of your active child theme (or active theme). I hope it will work.
Related:
- Remove attribute values from product variation title and show them on separate rows
- Hide variation info from cart item title in WooCommerce 3+
Only keep in cart the last variation from WooCommerce variable products
The following will only keep "silently" in cart the last variation from a variable product:
add_action('woocommerce_before_calculate_totals', 'keep_last_variation_from_variable_products', 10, 1 );
function keep_last_variation_from_variable_products( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$parent_ids = array();
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Only for product variations
if ( $cart_item['variation_id'] > 0 ) {
// When a variable product exist already in cart
if( isset($parent_ids[$cart_item['product_id']]) && ! empty($parent_ids[$cart_item['product_id']]) ) {
// Remove it
$cart->remove_cart_item($parent_ids[$cart_item['product_id']]);
}
// Set in the array the cart item key for variable product id key
$parent_ids[$cart_item['product_id']] = $cart_item_key;
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Related Topics
How to Get Page Number on Dompdf PDF When Using "View"
Decrypting Strings in Python That Were Encrypted with Mcrypt_Rijndael_256 in PHP
Fix Incorrectly Displayed Encoding on an HTML Document with PHP
Php: Check for Duplicate Values in a Multidimensional Array
Using Fetch_Assoc on Prepared Statements
Pdo Prepared Statement Fetch() Returning Double Results
How Would I Implement a Simple Site Search with PHP and MySQL
Login Only If User Is Active Using Laravel
How Come Checkbox State Is Not Always Passed Along to PHP Script
Remove Double Quote in JSON_Encode()
What Does the MySQLi_Error() Expects Parameter 1 to Be MySQLi, Null Given Mean
Session Lost After Page Redirect in PHP