Created
July 12, 2021 15:53
-
-
Save jorpdesigns/02b42904a6b698cf76237823658eed1f to your computer and use it in GitHub Desktop.
Snippet to loop through WooCommerce cart items and conduct checks or perform actions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { | |
$product = $cart_item['data']; | |
// GET ITEM LINK | |
$link = $product->get_permalink( $cart_item ); | |
// GET ITEM CART PRICE | |
$price = WC()->cart->get_product_price( $product ); | |
// GET ITEM REGULAR PRICE | |
$regularPrice = $product->get_regular_price(); | |
// GET ITEM SALE PRICE | |
$regularPrice = $product->get_sale_price(); | |
// SET ITEM PRICE TO 25 | |
$product->set_price( 25 ); | |
// GET ITEM QUANTITY | |
$quantity = $cart_item['quantity']; | |
// GET SUBTOTAL | |
$subtotal = WC()->cart->get_product_subtotal( $product, $cart_item['quantity'] ); | |
// CHECK IF PRODUCT ID IS 12345 | |
if ( $cart_item['product_id'] == 12345 ) { | |
// Do something | |
} | |
// CHECK IF PRODUCT BELONGS TO CATEGORY ID 25 | |
if ( in_array( 25, $cart_item['data']->get_category_ids() ) ) { | |
// Do something | |
} | |
// CHECK IF ITEM HAS CUSTOM FIELD 'custom_field' | |
if ( isset( $cart_item['custom_field'] ) ) { | |
// Do something | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment