web-dev-qa-db-fra.com

Obtention de l'identifiant de catégorie pour tous les produits dans le panier

Ce que j'essaie de faire est de prendre les catégories pour les produits qui sont dans le panier. Ensuite, vérifiez, via certaines conditions, l’identité de la catégorie et affichez un avis différent.

Jusqu'ici j'ai cette

/**
* Cart collaterals hook.
*
* @hooked woocommerce_cross_sell_display
* @hooked woocommerce_cart_totals - 10
*/
$categories = array( 39 );

foreach ( WC()->cart->get_cart() as $cart_item ) {
    if ( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
        $found = true; // Set to true
        break; // Stop the loop
    }
}        

if( $found ) {
    echo 'Category with ID = 39';
}
else if ($found != $cart_item['category_ids']) {
    echo "Category with ID = 39 and other ID('s)";
}
else {
    "Category with ID != 39";
}

On dirait que $cart_item['category_ids'] ne renvoie pas de catégories. $found fonctionne et affiche la catégorie avec l'ID = 39.

Lorsque je var_dump($cart_items), je vois différentes catégories comme celle-ci:

["category_ids"]=>
  array(1) {
    [0]=>
    int(39)
  }
   /// another stuff in the array

["category_ids"]=>
    array(2) {
      [0]=>
      int(32)
      [1]=>
      int(33)
    }

Ainsi, dans le panier, j'ai des produits de catégories avec l'identifiant 39, 32 et 33.

J'ai aussi essayé WC()->cart->get_cart()->category_ids mais this return NULL

MISE À JOUR: This

$cat_ids = array();
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $cat_ids = array_merge(
        $cat_ids, $cart_item['data']->get_category_ids()
    );
}

$cat_id = 39;
if ( in_array( $cat_id, $cat_ids ) ) {
    echo 'Only 39 ';
} elseif ( ! empty( $cat_ids ) ) {
    echo '39 + other';
} else {
    echo ' all other without 39';
}

Correspondant actuellement

Quand: catégorie 39 + autre -> Only 39

Quand: tous les autres sans 39 -> 39 + other

Quand: seulement 39 -> Only 39

CA devrait etre

Quand: catégorie 39 + autre -> 39 + other

Quand: tous les autres sans 39 -> all other without 39

Quand: seulement 39 -> Only 39

METTRE À JOUR

Quand: Catégorie 39 + produit de la autre catégorie (ID de catégorie = 32, 33, etc.)

var_dump(count( $cat_ids )); -> int(1)
var_dump($has_cat); -> bool(true)
var_dump(cat_ids); -> array(1) { [0]=> int(39) } <---- there are 3 products in the cart 2 of them are from other categories.

Quand: catégorie 39 seulement

var_dump(count( $cat_ids )); -> int(1)
var_dump($has_cat); -> bool(true)
var_dump(cat_ids); -> array(1) { [0]=> int(39) }

Quand: pas de catégorie 39

var_dump(count( $cat_ids )); -> int(2)
var_dump($has_cat); -> bool(false)
var_dump(cat_ids); -> array(2) { [0]=> int(32) [1]=> int(33) } <--- product is added in 2 categories

MISE À JOUR 2

Condition 1

1) cat 30; 
2) cat 39; 
$cond = 2 (because there are products in cart from 39 + other category)

Condition 2

1) cat 39; 
$cond = 1 (because in cart is/are product/s only from cat 39)

Condition 3

1) cat 40; 
2) cat 15;
$cond = last one (because there is no product/s from cat 39 in cart)
1
Ivanov

Utilisez $cart_item['data']->get_category_ids() pour récupérer les ID de catégorie:

$category_ids = $cart_item['data']->get_category_ids(); // array

Le category_ids que vous voyez n'est pas un élément direct dans le tableau $cart_item:

var_dump( $cart_item['category_ids'] ); // null and PHP throws a notice

C'est un élément de la propriété protected des données du produit ($cart_item['data']) qui est un objet ou une instance de classe - par exemple. la classe est WC_Product_Simple pour les produits simples.

METTRE À JOUR

Si vous souhaitez collecter tous les ID de catégorie de produit, vous pouvez le faire comme suit:

$cat_ids = array();
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $cat_ids = array_merge(
        $cat_ids, $cart_item['data']->get_category_ids()
    );
}
var_dump( $cat_ids );

MISE À JOUR # 2

Vous pouvez utiliser ceci pour vérifier si un produit dans le panier est dans certaines catégories:

$cat_ids = array( 39 );
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
    if ( has_term( $cat_ids, 'product_cat', $cart_item['data'] ) ) {
        echo 'Has category 39<br>';
    } else {
        echo 'No category 39<br>';
    }
}

MISE À JOUR # 3/# 4/# 5

Cela devrait fonctionner comme prévu:

// Collect the category IDs.
$cat_ids = array();
foreach ( wc()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $cat_ids = array_merge(
        $cat_ids, $cart_item['data']->get_category_ids()
    );
}

// And here we check the IDs.
$cat_id = 39;
$count = count( $cat_ids );
if ( 1 === $count && in_array( $cat_id, $cat_ids ) ) {
    echo 'Only in category 39';
} elseif ( $count && in_array( $cat_id, $cat_ids ) ) {
    echo 'In category 39 and other categories';
} else {
    echo 'No category 39';
}

Voici une autre version du bloc if ci-dessus:

if ( $count && in_array( $cat_id, $cat_ids ) ) {
    // Only one category.
    if ( $count < 2 ) {
        echo 'Only in category 39';
    // Multiple categories.
    } else {
        echo 'In category 39 and other categories';
    }
} else {
    echo 'No category 39';
}
1
Sally CJ