从 WooCommerce 中的购物车项目中获取产品类别条款
Get the product category terms from cart items in WooCommerce
我只知道一些产品的产品类别,有些我不知道
function savings_33_55_cart() {
foreach ( WC()->cart->get_cart() as $key => $cart_item ) {
for ($i=0; $i < $cart_item['quantity'] ; $i++) {
$productId = $cart_item['data']->get_id();
echo "PROD ID: " . $productId . "<br>";
$terms = get_the_terms( $productId, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
echo "PRODUCT CATEGORY: " . $product_cat . "<br>";
}
}
}
}
add_action( 'woocommerce_cart_totals_before_order_total', 'savings_33_55_cart' );
我希望获得每个产品的产品类别,但我只获得部分产品的产品类别
首先你的$product_cat
变量没有定义。
要获取购物车商品的产品类别,您需要获取产品变体的父变量产品 ID,因为它们不将自定义分类作为产品类别或产品标签处理.
To get the correct product ID for any custom taxonomy terms on cart items always use:
$product_id = $cart_item['product_id'];
Instead of:
$product_id = $cart_item['data']->get_id();
现在如果您需要获取产品类别术语名称,而不是使用get_the_terms()
函数,您可以使用wp_get_post_terms()
和implode()
功能如:
$term_names = wp_get_post_terms( $product_id, 'product_cat', ['fields' => 'names'] );
// Displaying term names in a coma separated string
if( count( $term_names ) > 0 )
echo __("Product categories") . ": " . implode( ", ", $term_names ) . '<br>':
// OR displaying term names as a vertical list
if( count( $term_names ) > 0 )
echo __("Product categories") . ": " . implode( "<br>", $term_names ) . '<br>';
因此在每个循环的购物车项目中:
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$quantity = $cart_item['quantity'];
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
echo __("PRODUCT ID: ") . $product_id . "<br>";
$term_names = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'names') );
if ( count($term_names) > 0 ) {
echo __("PRODUCT CATEGORY: ") . implode("<br>", $term_names) . "<br>";
}
}
我只知道一些产品的产品类别,有些我不知道
function savings_33_55_cart() {
foreach ( WC()->cart->get_cart() as $key => $cart_item ) {
for ($i=0; $i < $cart_item['quantity'] ; $i++) {
$productId = $cart_item['data']->get_id();
echo "PROD ID: " . $productId . "<br>";
$terms = get_the_terms( $productId, 'product_cat' );
foreach ($terms as $term) {
$product_cat = $term->name;
echo "PRODUCT CATEGORY: " . $product_cat . "<br>";
}
}
}
}
add_action( 'woocommerce_cart_totals_before_order_total', 'savings_33_55_cart' );
我希望获得每个产品的产品类别,但我只获得部分产品的产品类别
首先你的$product_cat
变量没有定义。
要获取购物车商品的产品类别,您需要获取产品变体的父变量产品 ID,因为它们不将自定义分类作为产品类别或产品标签处理.
To get the correct product ID for any custom taxonomy terms on cart items always use:
$product_id = $cart_item['product_id'];
Instead of:
$product_id = $cart_item['data']->get_id();
现在如果您需要获取产品类别术语名称,而不是使用get_the_terms()
函数,您可以使用wp_get_post_terms()
和implode()
功能如:
$term_names = wp_get_post_terms( $product_id, 'product_cat', ['fields' => 'names'] );
// Displaying term names in a coma separated string
if( count( $term_names ) > 0 )
echo __("Product categories") . ": " . implode( ", ", $term_names ) . '<br>':
// OR displaying term names as a vertical list
if( count( $term_names ) > 0 )
echo __("Product categories") . ": " . implode( "<br>", $term_names ) . '<br>';
因此在每个循环的购物车项目中:
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$quantity = $cart_item['quantity'];
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
echo __("PRODUCT ID: ") . $product_id . "<br>";
$term_names = wp_get_post_terms( $product_id, 'product_cat', array('fields' => 'names') );
if ( count($term_names) > 0 ) {
echo __("PRODUCT CATEGORY: ") . implode("<br>", $term_names) . "<br>";
}
}