如何在购物车woocommerce WordPress中显示产品父类别名称?

时间:2019-11-30 14:45:39

标签: php wordpress woocommerce

嗨,我想在woocommerce WordPress购物车中显示父类别名称。 我有此代码段筛选器来显示购物车中的类别,但我也希望拥有父类别。

    /**
 * @snippet       Display Categories Under Product Name @ WooCommerce Cart
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=72844
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.2.1
 */

add_filter( 'woocommerce_cart_item_name', 'bbloomer_cart_item_category', 99, 3);

function bbloomer_cart_item_category( $name, $cart_item, $cart_item_key ) {

$product_item = $cart_item['data'];

// make sure to get parent product if variation
if ( $product_item->is_type( 'variation' ) ) {
$product_item = wc_get_product( $product_item->get_parent_id() );
} 

$cat_ids = $product_item->get_category_ids();

// if product has categories, concatenate cart item name with them
if ( $cat_ids ) $name .= '</br>' . wc_get_product_category_list( $product_item->get_id(), ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', count( $cat_ids ), 'woocommerce' ) . ' ', '</span>' );

return $name;

}

0 个答案:

没有答案