2017-12-01 8 views
0

인터넷을 통해 수색했으나 아무리 많은 사람들이 질문을해도 불구하고 아무 대답도없는 것 같습니다.WooCommerce의 모든 쿠폰에서 제품을 제외하십시오.

모든 쿠폰에서 특정 제품을 제외 할 수있는 방법이 있습니까?

난 당신이 쿠폰 수준에서 그것을 할 수 있다는 것을 이해하지만이 ... 여러 사람이 등 쿠폰을 생성, TLDR

많은 사람들이 자동으로 쿠폰이 특히 오히려 지저분 : 제품을 만들 수있는 방법 모든 쿠폰에서 제외 상품 수준

+0

는이 솔루션을 시도해 봤어 작동? https://www.clowcreative.com/post/exclude-products-from-coupon-codes-automatically-at-checkout –

답변

2

다음은이 프로세스를 자동화하는 좋은 방법입니다.

1) 제품 일반 설정 메타 옥스에 사용자 정의 체크 박스를 추가하여 현재 제품의 쿠폰 기능을 비활성화합니다. 그래서 당신은 백엔드 편집 제품 페이지에이를 얻을 것이다 : custom checkbox in product general settings metabox

선택한 모든 제품

배열에 저장하고 다음에 사용됩니다 ...

2)이 선택한 제품에서 제외됩니다 제품 수준에서 쿠폰 할인 및 제품 할인 금액은 0으로 설정됩니다.


코드 :

// Create and display the custom field in product general setting tab 
add_action('woocommerce_product_options_general_product_data', 'add_custom_field_general_product_fields'); 
function add_custom_field_general_product_fields(){ 
    global $post; 

    echo '<div class="product_custom_field">'; 

    // Custom Product Checkbox Field 
    woocommerce_wp_checkbox(array(
     'id'  => '_disabled_for_coupons', 
     'desc'  => __('Disable this products from coupon discounts', 'woocommerce'), 
     'label'  => __('Disabled for coupons', 'woocommerce'), 
     'desc_tip' => 'true', 
    )); 

    echo '</div>';; 
} 

// Save the custom field and update all excluded product Ids in option WP settings 
add_action('woocommerce_process_product_meta', 'save_custom_field_general_product_fields', 10, 1); 
function save_custom_field_general_product_fields($post_id){ 

    $current_disabled = isset($_POST['_disabled_for_coupons']) ? 'yes' : 'no'; 

    $disabled_products = get_option('_products_disabled_for_coupons'); 
    if(empty($disabled_products)) { 
     if($current_disabled == 'yes') 
      $disabled_products = array($post_id); 
    } else { 
     if($current_disabled == 'yes') { 
      $disabled_products[] = $post_id; 
      $disabled_products = array_unique($disabled_products); 
     } else { 
      if (($key = array_search($post_id, $disabled_products)) !== false) 
       unset($disabled_products[$key]); 
     } 
    } 

    update_post_meta($post_id, '_disabled_for_coupons', $current_disabled); 
    update_option('_products_disabled_for_coupons', $disabled_products); 
} 

// Make coupons invalid at product level 
add_filter('woocommerce_coupon_is_valid_for_product', 'set_coupon_validity_for_excluded_products', 12, 4); 
function set_coupon_validity_for_excluded_products($valid, $product, $coupon, $values){ 
    if(! count(get_option('_products_disabled_for_coupons')) > 0) return $valid; 

    $disabled_products = get_option('_products_disabled_for_coupons'); 
    if(in_array($product->get_id(), $disabled_products)) 
     $valid = false; 

    return $valid; 
} 

// Set the product discount amount to zero 
add_filter('woocommerce_coupon_get_discount_amount', 'zero_discount_for_excluded_products', 12, 5); 
function zero_discount_for_excluded_products($discount, $discounting_amount, $cart_item, $single, $coupon){ 
    if(! count(get_option('_products_disabled_for_coupons')) > 0) return $discount; 

    $disabled_products = get_option('_products_disabled_for_coupons'); 
    if(in_array($cart_item['product_id'], $disabled_products)) 
     $discount = 0; 

    return $discount; 
} 

코드는 function.php의 활성 자식 테마의 파일 (또는 활성 테마)에 또는 플러그인 파일에 간다.

는 테스트를 완벽하게