반응형
카트 항목 이름, 수량 모든 세부 정보 가져오기
woocommerce 카트 아이템을 서드파티 배송 툴로 보내려고 합니다.서드파티에 보낼 상품명, 수량, 개별가격이 필요합니다.어떻게 하면 좋을까요?
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo $_product->post_title;
}
품목명, 수량, 가격은 어떻게 알 수 있나요?
다음을 시도해 보십시오.
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = wc_get_product( $values['data']->get_id());
echo "<b>".$_product->get_title().'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
}
?>
제품 이미지 및 일반 및 판매 가격을 얻으려면:
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = wc_get_product( $values['data']->get_id() );
//product image
$getProductDetail = wc_get_product( $values['product_id'] );
echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
echo "<b>".$_product->get_title() .'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
/*Regular Price and Sale Price*/
echo "Regular Price: ".get_post_meta($values['product_id'] , '_regular_price', true)."<br>";
echo "Sale Price: ".get_post_meta($values['product_id'] , '_sale_price', true)."<br>";
}
?>
WooCommerce 2.1 (2014) 이후 글로벌이 아닌 WC 기능을 사용해야 합니다.보다 적절한 함수를 호출할 수도 있습니다.
foreach ( WC()->cart->get_cart() as $cart_item ) {
$item_name = $cart_item['data']->get_title();
$quantity = $cart_item['quantity'];
$price = $cart_item['data']->get_price();
...
이것은 깨끗한 코드일 뿐만 아니라 필요에 따라 필터를 적용하므로 post_meta에 직접 액세스하는 것보다 더 좋습니다.
제품 가격에 대한 주의사항
카트 품목의 가격은 제품의 가격과 다를 수 있습니다(데이터베이스에 포스트 메타로 저장됨).
일부 플러그인 또는 사용자 지정 기능(함수에 추가됨)php of active tema)는 카트 아이템의 가격을 변경할 수 있습니다.
카트에 추가된 제품 가격을 확실히 받으려면 다음과 같이 받아야 합니다.
foreach ( WC()->cart->get_cart() as $cart_item ) {
// gets the cart item quantity
$quantity = $cart_item['quantity'];
// gets the cart item subtotal
$line_subtotal = $cart_item['line_subtotal'];
$line_subtotal_tax = $cart_item['line_subtotal_tax'];
// gets the cart item total
$line_total = $cart_item['line_total'];
$line_tax = $cart_item['line_tax'];
// unit price of the product
$item_price = $line_subtotal / $quantity;
$item_tax = $line_subtotal_tax / $quantity;
}
대신:
foreach ( WC()->cart->get_cart() as $cart_item ) {
// gets the product object
$product = $cart_item['data'];
// gets the product prices
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$price = $product->get_price();
}
기타 입수 가능한 데이터:
foreach ( WC()->cart->get_cart() as $cart_item ) {
// get the data of the cart item
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
// gets the cart item quantity
$quantity = $cart_item['quantity'];
// gets the cart item subtotal
$line_subtotal = $cart_item['line_subtotal'];
$line_subtotal_tax = $cart_item['line_subtotal_tax'];
// gets the cart item total
$line_total = $cart_item['line_total'];
$line_tax = $cart_item['line_tax'];
// unit price of the product
$item_price = $line_subtotal / $quantity;
$item_tax = $line_subtotal_tax / $quantity;
// gets the product object
$product = $cart_item['data'];
// get the data of the product
$sku = $product->get_sku();
$name = $product->get_name();
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$price = $product->get_price();
$stock_qty = $product->get_stock_quantity();
// attributes
$attributes = $product->get_attributes();
$attribute = $product->get_attribute( 'pa_attribute-name' ); // // specific attribute eg. "pa_color"
// custom meta
$custom_meta = $product->get_meta( '_custom_meta_key', true );
// product categories
$categories = wc_get_product_category_list( $product->get_id() ); // returns a string with all product categories separated by a comma
}
카트 항목 수만 표시됩니다.
global $woocommerce;
echo $woocommerce->cart->cart_contents_count;
당신은 이렇게 제품 이름을 얻을 수 있다.
foreach ( $cart_object->cart_contents as $value ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $value['data'] );
if ( ! $_product->is_visible() ) {
echo $_product->get_title();
} else {
echo $_product->get_title();
}
}
대부분의 경우 다른 로직(백엔드의 설정 예)과 비교할 수 있도록 카트에 있는 제품의 ID를 가져옵니다.
이 경우 @Rohil_에서 답변을 연장할 수 있습니다.다음과 같이 배열 내의 ID를 반환하고 PHPBeginner를 실행합니다.
<?php
function njengah_get_ids_of_products_in_cart(){
global $woocommerce;
$productsInCart = array();
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = wc_get_product( $values['data']->get_id());
/* Display Cart Items Content */
echo "<b>".$_product->get_title().'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
/**Get IDs and in put them in an Array**/
$productsInCart_Ids[] = $_product->get_id();
}
/** To Display **/
print_r($productsInCart_Ids);
/**To Return for Comparision with some Other Logic**/
return $productsInCart_Ids;
}
언급URL : https://stackoverflow.com/questions/28576667/get-cart-item-name-quantity-all-details-woocommerce
반응형
'codememo' 카테고리의 다른 글
| 플라스크 앱에서 다른 사이트로 GET 요청을 보내려면 어떻게 해야 하나요? (0) | 2023.02.12 |
|---|---|
| Springfox Swagger-UI가 추가되었지만 작동하지 않습니다. 제가 놓치고 있는 것은 무엇입니까? (0) | 2023.02.12 |
| mongodb python 연결을 닫는 방법 (0) | 2023.02.12 |
| 스프링 부트에서 Postgre를 사용하여 Data Source를 로드할 수 없음SQL 드라이버 (0) | 2023.02.12 |
| outside/esc 클릭 시 Angularjs 부트스트랩모달 종료 콜 (0) | 2023.02.08 |