WooCommerce 카트 유효성 검사에 추가: 카트에 추가하지 않음
며칠 동안 고치려고 하는 우커머스에 문제가 있습니다.
어떤 남자를 위한 웹사이트를 만들고 있는데, 상품 페이지에 맞춤 입력을 추가해달라고 해서 제가 직접 할 수가 없어서 온라인으로 프리랜서를 이용했습니다.
상품 페이지에 카트 추가 버튼, 수량 입력, 날짜 입력이 있습니다.
"날짜 입력"은 프리랜서가 한 것입니다.
문제는 $_REQUEST['날짜']가 비어있을 때 사용자 정의 오류 팝업이 뜨지만 제품이 카트에 추가된다는 것입니다.
코드:(기능.php)
function add_the_date_validation() {
if ( empty( $_REQUEST['thedate'] )) {
wc_add_notice( __( 'Please enter a date.', 'woocommerce' ), 'error' );
return false;
}
return true;
}
add_action( 'woocommerce_add_to_cart_validation', 'add_the_date_validation', 10, 5 );
보기: https://i.stack.imgur.com/a75P6.png
- 어떻게 하면 제품이 카트에 잔류물이 추가되는 것을 방지할 수 있습니까?
프리랜서가 수행한 기타 코드:
function save_add_the_date_field( $cart_item_key, $product_id = null, $quantity= null, $variation_id= null, $variation= null ) {
if( isset( $_REQUEST['thedate'] ) ) {
WC()->session->set( $cart_item_key.'_the_date', $_REQUEST['thedate'] );
}
}
add_action( 'woocommerce_add_to_cart', 'save_add_the_date_field', 1, 5 );
function render_meta_on_checkout_order_review_item( $quantity = null, $cart_item = null, $cart_item_key = null ) {
if( $cart_item_key && WC()->session->__isset( $cart_item_key.'_the_date' ) ) {
echo $quantity. '<dl class="">
<dt class="">Date: </dt>
<dd class=""><p>'. WC()->session->get( $cart_item_key.'_the_date') .'</p></dd>
</dl>';
}
}
add_filter( 'woocommerce_checkout_cart_item_quantity', 'render_meta_on_checkout_order_review_item', 1, 3 );
function the_date_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( WC()->session->__isset( $cart_item_key.'_the_date' ) ) {
wc_add_order_item_meta( $item_id, "the_date", WC()->session->get( $cart_item_key.'_the_date') );
}
}
add_action( 'woocommerce_add_order_item_meta', 'the_date_order_meta_handler', 1, 3 );
function the_date_force_individual_cart_items($cart_item_data, $product_id) {
$unique_cart_item_key = md5( microtime().rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data','the_date_force_individual_cart_items', 10, 2 );
제품 추가 기능을 사용해야 한다고 했는데 날짜 선택기가 없는 것 같습니다.어쨌든 저는 당신의 검증 기능을 다음과 같이 수정하려고 합니다.
function add_the_date_validation( $passed ) {
if ( empty( $_REQUEST['thedate'] )) {
wc_add_notice( __( 'Please enter a date.', 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'add_the_date_validation', 10, 5 );
돌아오는 대신TRUE
당신은 현재 상태를 반환하길 원합니다.$passed
변수.테스트에 필요한 나머지 코드를 설정하지는 않을 것이기 때문에 잘 될 것이라고 약속할 수는 없지만, 이것은 제가 여러 번 해본 것과 비슷합니다.
다른 참고로 매장의 모든 제품에 이 유효성 검사를 적용하려는 경우가 아니라면 추가 조건부 논리를 사용하여 이 기능을 제한해야 합니다.
저는 이 문제가 있었고 시행착오 끝에 수정을 통해 다음의 $priority 매개변수(10은 기본값)를 조정했습니다.
add_filter( 'woocommerce_add_to_cart_validation', 'add_the_date_validation', 10, 5 );
평소처럼 다른 건 다 해봤어요이것을 15로 바꿨고 빙고..!우선 순위가 낮은 코드보다 먼저 실행되고 있었고, 그 다음에 아이템을 카트에 넣었던 것 같습니다.
// Allow only pre-defined quantity from specific per category in the cart
add_filter( 'woocommerce_add_to_cart_validation', 'allowed_quantity_per_category_in_the_cart', 10, 2 );
function allowed_quantity_per_category_in_the_cart( $passed, $product_id) {
$running_qty = 0;
$restricted_product_cats = $restricted_product_cat_slugs = array();
//Specify the category/categories by category slug and the quantity limit
$restricted_product_cats[] = array('cat_slug'=> 'skip-bins', 'max_num_products' => 1); // change here
//$restricted_product_cats[] = array('cat_slug'=> 'caliper-paint-kit', 'max_num_products' => 4); //change here
foreach($restricted_product_cats as $restricted_prod_cat) $restricted_product_cat_slugs[]= $restricted_prod_cat['cat_slug'];
// Getting the current product category slugs in an array
$product_cats_object = get_the_terms( $product_id, 'product_cat' );
foreach($product_cats_object as $obj_prod_cat) $current_product_cats[]=$obj_prod_cat->slug;
// Iterating through each cart item
foreach (WC()->cart->get_cart() as $cart_item_key=>$cart_item ){
// Check if restricted category/categories found
if( array_intersect($restricted_product_cat_slugs, $current_product_cats) ) {
foreach($restricted_product_cats as $restricted_product_cat){
if( in_array($restricted_product_cat['cat_slug'], $current_product_cats) && has_term( $current_product_cats, 'product_cat', $cart_item['product_id'] )) {
// count(selected category) quantity
$running_qty += (int) $cart_item['quantity'];
// Check if More than allowed products in the cart
if( $running_qty >= $restricted_product_cat['max_num_products'] ) {
//limit to maximum allowed
//WC()->cart->set_quantity( $cart_item_key, $restricted_product_cat['max_num_products'] ); // Change quantity
// Get the category information
$catinfo = get_term_by('slug', $restricted_product_cat['cat_slug'], 'product_cat');
wc_add_notice( sprintf( 'Maximum of %s '.($restricted_product_cat['max_num_products']>1?'skip bin ('.$catinfo->name.') are':'skip bin').' allowed in the cart. Please remove the existing Skip Bin from the cart to add the new Skip Bin', $restricted_product_cat['max_num_products'] ), 'error' );
///WC()->cart->empty_cart();
$passed = false; // don't add the new product to the cart
// We stop the loop
break;
}
}
}
}
}
return $passed;
}
네, 이 포럼에 처음 와서 죄송합니다.그러나 해결책을 찾았고 900번 라인에서 class-wc-cart.php를 직접 편집하고 if: 전에 함수를 복사했습니다.
if($passed!=false)
{
do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );
}
이제 그것은 저에게 효과가 있습니다. 그것이 가장 정확한 해결책이 아니라는 것을 압니다. 그러나 그것이 효과가 있는 순간 저는 제가 언제 우커머스를 업데이트할지 주의해야 합니다.
언급URL : https://stackoverflow.com/questions/30736863/woocommerce-add-to-cart-validation-prevent-add-to-cart
'programing' 카테고리의 다른 글
constant *p vs. int constant *p - 유형 다음에 const를 허용합니까? (0) | 2023.09.19 |
---|---|
zip 파일 다운로드 방법 (0) | 2023.09.19 |
아직 첨부되지 않은 경우 Larvel 관계가 첨부 (0) | 2023.09.19 |
선행 또는 후행 공백을 포함하는 MySQL 선택 필드 (0) | 2023.09.19 |
SecurityContext - Spring 3.2.2에서 인증 개체를 찾을 수 없습니다. (0) | 2023.09.14 |