brand-above-title-in-cart.php(file created)
| @@ -0,0 +1,27 @@ | |||
| 1 | + | /* START Display Brand above title in cart */ | |
| 2 | + | // Function to display child category above product title on cart page | |
| 3 | + | function display_child_category_above_product_title_on_cart( $item_name, $cart_item, $cart_item_key ) { | |
| 4 | + | // We attempt to check if $cart_item contains product details | |
| 5 | + | if ( isset( $cart_item['product_id'] ) ) { | |
| 6 | + | $product_id = $cart_item['product_id']; | |
| 7 | + | ||
| 8 | + | // Get child categories of the product | |
| 9 | + | $product_categories = get_the_terms( $product_id, 'product_cat' ); | |
| 10 | + | ||
| 11 | + | if ( $product_categories && ! is_wp_error( $product_categories ) ) { | |
| 12 | + | // Loop through each category | |
| 13 | + | foreach ( $product_categories as $product_category ) { | |
| 14 | + | // Check if the category is a child category | |
| 15 | + | if ( $product_category->parent !== 0 ) { | |
| 16 | + | // Display the child category name | |
| 17 | + | $item_name = '<div class="child-category">' . $product_category->name . '</div>' . $item_name; | |
| 18 | + | } | |
| 19 | + | } | |
| 20 | + | } | |
| 21 | + | } | |
| 22 | + | ||
| 23 | + | // Return the modified or original item name | |
| 24 | + | return $item_name; | |
| 25 | + | } | |
| 26 | + | add_filter( 'woocommerce_cart_item_name', 'display_child_category_above_product_title_on_cart', 10, 3 ); | |
| 27 | + | /* END Display Brand above title in cart */ | |
更新
更早