wocommerce优惠券不接受Woocommerce产品定制价格

时间:2018-03-22 05:00:29

标签: wordpress woocommerce hook-woocommerce woothemes woocommerce-theming

在我们的woocommerce商店,客户可以根据这些细节输入产品的自定义宽度和高度以及产品价格。

例如,如果产品的初始价格为50 。并且客户添加 width = 2,height = 3 ,然后此产品的价格将 50 * 2 * 3 = 300

为此我们使用以下代码

// Save custom field value in cart item as custom data
add_filter( 'woocommerce_add_cart_item', 'calculate_custom_cart_item_prices', 30, 3 );
function calculate_custom_cart_item_prices( $cart_item_data, $product_id, $variation_id ) {
    if ( isset($_POST['width']) && isset($_POST['height']) ) {
        // Get the correct Id to be used (compatible with product variations)
        $the_id = $variation_id > 0 ? $variation_id : $product_id;

        $product = wc_get_product( $the_id ); // Get the WC_Product object
        $product_price = (float) $product->get_price(); // Get the product price

        // Get the posted data
        $width  = (float) sanitize_text_field( $_POST['width'] );
        $height = (float) sanitize_text_field( $_POST['height'] );

        $new_price = $width * $height * $product_price; // Calculated price

        $cart_item_data['calculated-price'] = $new_price; // Save this price as custom data
    }
    return $cart_item_data;
}

// Set custom calculated price in cart item price
add_action( 'woocommerce_before_calculate_totals', 'set_calculated_cart_item_price', 20, 1 );
function set_calculated_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ){
        if( ! empty( $cart_item['calculated-price'] ) ){
            // Set the calculated item price (if there is one)
            $cart_item['data']->set_price( $cart_item['calculated-price'] );
        }
    }

它是虫蛀,但问题是

  

当客户为此产品申请50%的优惠券代码时   折扣是25,因为它的计算基于50 *(50/100)= 25;

     

但实际上产品新价格是300,所以折扣应该是   300 *(50/100)= 150;

请帮助解决这个问题。

1 个答案:

答案 0 :(得分:1)

尝试更新您的' calculate_custom_cart_item_prices'对这样的事情起作用,看看是否有帮助。

add_filter( 'woocommerce_add_cart_item', 'calculate_custom_cart_item_prices', 30, 2 );
function calculate_custom_cart_item_prices( $cart_item_data, $cart_item_key ) {
    if ( isset($_POST['width']) && isset($_POST['height']) ) {
        // Get the correct Id to be used (compatible with product variations)
        $the_id = $cart_item_data['variation_id'] > 0 ? $cart_item_data['variation_id'] : $cart_item_data['product_id'];

        $product = wc_get_product( $the_id ); // Get the WC_Product object
        $product_price = (float) $product->get_price(); // Get the product price

        // Get the posted data
        $width  = (float) sanitize_text_field( $_POST['width'] );
        $height = (float) sanitize_text_field( $_POST['height'] );

        $new_price = $width * $height * $product_price; // Calculated price

        $cart_item_data['calculated-price'] = $new_price; // Save this price as custom data
    }
    return $cart_item_data;
}

我对正在发生的事情的猜测是Woocommerce的变化改变了&woocommerce_add_cart_item' woocommerce_add_cart_item'过滤器有效,因此您需要更新此功能。

相关问题