반응형
WooCommerce:아이템이 이미 카트에 있는지 확인합니다.
나는 이 웹사이트에서 이 멋진 조각을 발견했다.
카트에 특정 제품이 있는지 확인하는 기능은 다음과 같습니다.
function woo_in_cart($product_id) {
global $woocommerce;
foreach($woocommerce->cart->get_cart() as $key => $val ) {
$_product = $val['data'];
if($product_id == $_product->id ) {
return true;
}
}
return false;
}
필요한 장소에서 사용할 수 있습니다.
if(woo_in_cart(123)) {
// Product is already in cart
}
문제는 다음과 같은 여러 제품을 확인하기 위해 이 제품을 사용하는 방법입니다.
if(woo_in_cart(123,124,125,126...)) {
// Product is already in cart
}
감사해요.
global $woocommerce
시대에 뒤떨어져서 간단하게 대체되고 있습니다.
다음은 하나의 정수 제품 ID 또는 제품 ID 배열을 받아들여 카트 내에 있는 일치하는 ID 수를 반환하는 인수를 가진 커스텀 함수입니다.
코드는 다양한 제품 및 제품 종류를 포함하여 모든 제품 유형을 처리합니다.
function matched_cart_items( $search_products ) {
$count = 0; // Initializing
if ( ! WC()->cart->is_empty() ) {
// Loop though cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
// Handling also variable products and their products variations
$cart_item_ids = array($cart_item['product_id'], $cart_item['variation_id']);
// Handle a simple product Id (int or string) or an array of product Ids
if( ( is_array($search_products) && array_intersect($search_products, cart_item_ids) )
|| ( !is_array($search_products) && in_array($search_products, $cart_item_ids)
$count++; // incrementing items count
}
}
return $count; // returning matched items count
}
이 코드는 기능합니다.php 파일(액티브 테마 또는 플러그인 파일)을 지정합니다.
코드가 테스트되어 동작한다.
용도:
1) 하나의 제품 ID(정수)의 경우:
$product_id = 102;
// Usage as a condition in an if statement
if( 0 < matched_cart_items($product_id) ){
echo '<p>There is "'. matched_cart_items($product_id) .'"matched items in cart</p><br>';
} else {
echo '<p>NO matched items in cart</p><br>';
}
2) 제품 ID 배열의 경우:
$product_ids = array(102,107,118);
// Usage as a condition in an if statement
if( 0 < matched_cart_items($product_ids) ){
echo '<p>There is "'. matched_cart_items($product_ids) .'"matched items in cart</p><br>';
} else {
echo '<p>NO matched items in cart</p><br>';
}
3) 3개 이상의 일치하는 카트 항목에 대한 제품 ID 배열의 경우, 예를 들어 다음과 같습니다.
$product_ids = array(102, 107, 118, 124, 137);
// Usage as a condition in an if statement (for 3 matched items or more)
if( 3 <= matched_cart_items($product_ids) ){
echo '<p>There is "'. matched_cart_items($product_ids) .'"matched items in cart</p><br>';
} else {
echo '<p>NO matched items in cart</p><br>';
}
좀 더 간단한 방법으로 제품 ID를 장바구니에 담습니다.
$product_ids = array_merge(
wp_list_pluck(WC()->cart->get_cart_contents(), 'variation_id'),
wp_list_pluck(WC()->cart->get_cart_contents(), 'product_id')
);
하나의 제품을 체크하려면 in_array 기능을 사용하면 됩니다.
in_array(123, $product_ids);
및 여러 제품의 경우:
array_intersect([123, 345, 567], $product_ids);
케이스 1 : Array를 인수로 전달합니다.
function woo_in_cart($arr_product_id) {
global $woocommerce;
$cartarray=array();
foreach($woocommerce->cart->get_cart() as $key => $val ) {
$_product = $val['data'];
array_push($cartarray,$_product->id);
}
$result = !empty(array_intersect($cartarray,$arr_product_id));
return $result;
}
함수를 호출하는 방법
$is_incart=array(2,4,8,11);
print_r(woo_in_cart($is_incart));
케이스 2 : 실행하는 코드를 사용합니다.
$is_in_product_cart=array(123,124,125,126,..);
foreach($is_in_product_cart as $is_in_cart )
if(woo_in_cart($is_in_cart))
{
// Product is already in cart
}
}
woo_in_cart 함수에 오류가 있었습니다.다음은 정답입니다.
function woo_in_cart($arr_product_id) {
global $woocommerce;
$cartarray=array();
foreach($woocommerce->cart->get_cart() as $key => $val ) {
$_product = $val['product_id'];
array_push($cartarray,$_product);
}
if (!empty($cartarray)) {
$result = array_intersect($cartarray,$arr_product_id);
}
if (!empty($result)) {
return true;
} else {
return false;
};
}
다음은 사용 예를 제시하겠습니다.
//Set IDs Array variable
$my_products_ids_array = array(22,23,465);
if (woo_in_cart($my_products_ids_array)) {
echo 'ohh yeah there some of that products in!';
}else {
echo 'no matching products :(';
}
언급URL : https://stackoverflow.com/questions/41262426/woocommerce-check-if-items-are-already-in-cart
반응형
'itsource' 카테고리의 다른 글
InputStream을 JSONObject로 변환 (0) | 2023.03.13 |
---|---|
내 워드프레스 게시물을 인스타그램에 공유하는 방법 (0) | 2023.03.13 |
추적: 노드 유형 SpreadProperty의 이름이 Object.isSpreadProperty에서 SpreadElement로 변경되었습니다. (0) | 2023.03.08 |
ES6 구문을 사용하여 온클릭 이벤트를 통해 전달 매개 변수 반응 (0) | 2023.03.08 |
스프링 부트가 logging.path 변수를 수신하지 않는 이유는 무엇입니까? (0) | 2023.03.08 |