我试图使一个条件,其中一个函数显示在WooCommerce结账页面,如果任何产品从类别ilutulestik
是存在于购物车。
然而,目前它似乎无法获取购物车信息。我假设,因为如果我对代码使用if($cat\u in\u cart)
条件,我使用它的函数不会显示,即使购物车中存在ilutulestik
类别的产品。
我尝试了许多不同的方法来获取购物车信息,但似乎没有一种有效。我将在下面介绍我的两种尝试方式:
试试1
add_action('woocommerce_before_cart', 'kontrollime_ilutulestiku_olemasolu');
function kontrollime_ilutulestiku_olemasolu()
{
global $woocommerce;
$cat_in_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item_key => $values )
{
$item = $values['data'];
$item_id = $item->id;
if ( has_term( 'ilutulestik-2', 'product_cat', $item_id ) )
{
$cat_in_cart = true;
break;
}
}
}
试试2
add_action('woocommerce_before_cart', 'kontrollime_ilutulestiku_olemasolu');
function kontrollime_ilutulestiku_olemasolu($package)
{
global $woocommerce;
$cat_in_cart = false;
foreach ($package['contents'] as $product)
{
// get product categories
$product_cats = wp_get_post_terms(
$product['product_id'], 'product_cat', array('fields' => 'names') );
// if it has category_name unset flat rate
if( in_array('ilutulestik-2', $product_cats) )
{
$cat_in_cart = true;
break;
}
}
}
我希望这段代码检查购物车中是否有属于类别Ilutulestik
(或段塞ilutulestik-2
)的产品,如果有,将$cat_in_cart
值更改为true
,因此稍后,我可以使用激活任何代码,如果($cat_in_cart)
。
购物车项目与产品类别一起使用的正确方式是:
add_action('woocommerce_before_cart', 'action_before_cart');
function action_before_cart() {
$categories = array('ilutulestik-2');
$has_category = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product categories
if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$has_category = true;
break;
}
}
// Testing output (display a notice)
if ( $has_category ) {
wc_print_notice( sprintf( 'Product category "%s" is in cart!', reset($categories)), 'notice' );
}
}
代码进入函数。活动子主题(或活动主题)的php文件。测试和工作。
如果您还需要检查产品类别的父术语,您将改用:
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
add_action('woocommerce_before_cart', 'action_before_cart');
function action_before_cart() {
$categories = array('ilutulestik-2');
$has_category = false;
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
// Check for product categories
if ( has_product_categories( $cart_item['product_id'], $categories ) ) {
$has_category = true;
break;
}
}
// Testing output (display a notice)
if ( $has_category ) {
wc_print_notice( sprintf( 'Product category "%s" is in cart!', reset($categories)), 'notice' );
}
}
代码进入函数。活动子主题(或活动主题)的php文件。测试和工作。