복붙노트

[WORDPRESS] 고객이 제품 가격을 설정하고 WooCommerce에서 특정 유효성 검사가있는 장바구니에 추가 할 수있게하십시오.

WORDPRESS

고객이 제품 가격을 설정하고 WooCommerce에서 특정 유효성 검사가있는 장바구니에 추가 할 수있게하십시오.

해결법


  1. 1.

    function giftcard_price_field() {
        global $product;
        
        // Instanceof
        if ( $product instanceof WC_Product ) {
            
            // Set category(ies)
            $cats = array ( 'giftcard' );
    
            // True
            if ( has_term( $cats, 'product_cat', $product->get_id() ) ) {
                // add a new input field for the price, allowing the customer to set the price
                echo '<div class="giftcard-product-price">
                <label for="giftcard-product-price">Giftcard value: </label>
                <input type="text" id="giftcard_product_price" name="giftcard_product_price" placeholder="Giftcard value" maxlength="1000">
                </div>';
            }
        }
    }
    add_action( 'woocommerce_before_add_to_cart_button', 'giftcard_price_field', 10, 0 );
    
    // Remove price
    function action_woocommerce_single_product_summary() {
        global $product;
        
        // Instanceof
        if ( $product instanceof WC_Product ) {
            
            // Set category(ies)
            $cats = array ( 'giftcard' );
    
            // True
            if ( has_term( $cats, 'product_cat', $product->get_id() ) ) {   
                remove_action('woocommerce_single_product_summary','woocommerce_template_single_price', 10 );
            }
        }
    }
    add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 5 );
    
    // Validate
    function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
        // Isset
        if ( isset ( $_POST['giftcard_product_price'] ) ) {
            $giftcard_product_price = $_POST['giftcard_product_price'];
    
            // Error = empty, not numeric or less than 100
            if ( empty ( $giftcard_product_price ) ) {
                wc_add_notice( __( 'Field is empty', 'woocommerce' ), 'error' );
                $passed = false;
            } elseif ( ! is_numeric ( $giftcard_product_price ) ) {
                wc_add_notice( __( 'NOT a number or a numeric string', 'woocommerce' ), 'error' );
                $passed = false;                
            } elseif ( $giftcard_product_price < 100 ) {
                wc_add_notice( __( 'Less than 100', 'woocommerce' ), 'error' );
                $passed = false;                
            }   
        }
        
        return $passed;
    }
    add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
    
    function filter_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
        if ( isset ( $_POST['giftcard_product_price'] ) ) {
            $cart_item_data['giftcard_product_price'] = sanitize_text_field( $_POST['giftcard_product_price'] );
        }
        
        return $cart_item_data; 
    }
    add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data', 10, 3 );
    
    // Set price
    function action_woocommerce_before_calculate_totals( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            if ( isset ( $cart_item['giftcard_product_price'] ) ) {
                $cart_item['data']->set_price( $cart_item['giftcard_product_price'] );
            }
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
    
  2. from https://stackoverflow.com/questions/63544045/allow-customer-to-set-the-product-price-and-add-to-cart-with-certain-validations by cc-by-sa and MIT license