代码之家  ›  专栏  ›  技术社区  ›  Michael

在WooCommerce中动态设置产品变化自定义计算价格

  •  1
  • Michael  · 技术社区  · 8 年前

    当用户根据在产品页面中输入的内容结账时,我试图动态添加到产品价格中。价格的设定仅适用于无变化的产品。

    我还需要能够根据产品的变化来确定价格。

    我使用的代码:

    function add_cart_item_data( $cart_item_meta, $product_id, $variation_id ) {
        $product = wc_get_product( $product_id );
        $price = $product->get_price();
    
        $letterCount = strlen($_POST['custom_name']);
        $numberCount = strlen($_POST['custom_number']);
    
        if($letterCount != '0') {
    
            $letterPricing = 20 * $letterCount;
            $numberPricing = 10 * $numberCount;
    
            $additionalPrice = $letterPricing + $numberPricing;
    
            $cart_item_meta['custom_price'] = $price + $additionalPrice;
        }
        return $cart_item_meta;
    }
    
    function calculate_cart_total( $cart_object ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
           return;
        }
    
        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value['custom_price'] ) ) {
                $price = $value['custom_price'];
                $value['data']->set_price( ( $price ) );
            }
        }
    }
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   LoicTheAztec    7 年前

    我已经彻底重温了您的代码:

    // Set custom data as custom cart data in the cart item
    add_filter( 'woocommerce_add_cart_item_data', 'add_custom_data_to_cart_object', 30, 3 );
    function add_custom_data_to_cart_object( $cart_item_data, $product_id, $variation_id ) {
        if( ! isset($_POST['custom_name']) || ! isset($_POST['custom_number']) )
            return $cart_item_data; // Exit
    
        if( $variation_id > 0)
            $product = wc_get_product( $variation_id );
        else
            $product = wc_get_product( $product_id );
    
        $price = $product->get_price();
    
        // Get the data from the POST request and calculate new custom price
        $custom_name = sanitize_text_field( $_POST['custom_name'] );
        if( strlen( $custom_name ) > 0 )
            $price += 20 * strlen( $custom_name );
        $custom_number = sanitize_text_field( $_POST['custom_number'] );
        if( strlen( $custom_number ) > 0 )
            $price += 10 * strlen( $custom_number );
    
        // Set new calculated price as custom cart item data
        $cart_item_data['custom_data']['price'] = $price;
    
        return $cart_item_data;
    }
    
    // Set the new calculated price of the cart item
    add_action( 'woocommerce_before_calculate_totals', 'set_new_cart_item_price', 50, 1 );
    function set_new_cart_item_price( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        foreach ( $cart->get_cart() as $cart_item ) {
            if( isset( $cart_item['custom_data']['price'] ) ) {
                // Get the new calculated price
                $new_price = (float) $cart_item['custom_data']['price'];
    
                // Set the new calculated price
                $cart_item['data']->set_price( $new_price );
            }
        }
    }
    

    此代码继续运行。活动子主题(或主题)的php文件。

    经过测试,可与产品变体配合使用。