Maison  >  Questions et réponses  >  le corps du texte

Obtenez l'ID de clé d'article du panier d'achat d'un article supprimé spécifique : WooCommerce Shopping Cart

Dans WooCommerce, j'ai un modèle cart.php personnalisé dans lequel je dois vérifier si un produit (article) supprimé est unique, puis coder davantage en fonction de ces informations.

Existe-t-il un moyen de trouver cet identifiant ONE Key d'un élément supprimé sans utiliser de hooks, c'est-à-dire une notification '“Item X”已删除的那个。撤消?' ?

Je ne trouve aucune solution nulle part.

P粉384244473P粉384244473171 Il y a quelques jours275

répondre à tous(1)je répondrai

  • P粉545956597

    P粉5459565972024-04-05 10:12:26

    Vous pouvez supprimer les articles du panier de deux manières :

    • Utilisé à partir de l'objet WC_Cart :

      $removed_items = WC()->cart->get_removed_cart_contents();
    • Utilisé depuis l'objet WC_Session :

      $removed_items = WC()->session->get('removed_cart_contents');

    Pour localiser un produit supprimé spécifique et obtenir son identifiant de clé (et en révoquer ou supprimer une URL) , utilisez :

    $targeted_product_id = 25; // Set the product ID to target
    $targeted_item_key   = ''; // Initializing
    
    // Get removed cart items
    $removed_items = WC()->cart->get_removed_cart_contents();
    
    // Loop through removed cart items
    foreach( $removed_items as $item_key => $item ) {
        $product_id   = $item['product_id'];
        $variation_id = $item['variation_id'];
    
        if( in_array($targeted_product_id, [$product_id, $variation_id]) ) {
            $targeted_item_key = $item_key; 
            break;
        }
    }
    // get the Undo URL
    $undo_url = WC()->cart->get_undo_url( $targeted_item_key );
    
    // Test output Undo URL
    echo ''. __("Undo Url") . '';
    
    // get the remove URL
    $remove_url = WC()->cart->get_remove_url( $targeted_item_key );
    
    // Test output remove URL
    echo ''. __("Remove Url") . '';
    

    répondre
    0
  • Annulerrépondre