根据Woocommerce中的自定义购物车数据更改购物车价格 [英] Change cart item prices based on custom cart item data in Woocommerce

查看:66
本文介绍了根据Woocommerce中的自定义购物车数据更改购物车价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我在产品单页中创建一个表单,以便客户可以输入产品的高度和宽度.因此,产品价格会根据输入的高度和宽度而有所不同.

currently i create a form in product single page so that customer can enter height and width of the product . So the product price vary based on the entered height and width .

我在

woocommerce->单个产品->添加到购物车-> simple.php

woocommerce->single-product->add-to-cart->simple.php

修改前,表单为

<form class="cart" method="post" enctype='multipart/form-data'>
    <?php
        /**
         * @since 2.1.0.
         */
        do_action( 'woocommerce_before_add_to_cart_button' );

        /**
         * @since 3.0.0.
         */
        do_action( 'woocommerce_before_add_to_cart_quantity' );

        woocommerce_quantity_input( array(
            'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
            'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
            'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
        ) );

        /**
         * @since 3.0.0.
         */
        do_action( 'woocommerce_after_add_to_cart_quantity' );
    ?>

    <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

    <?php
        /**
         * @since 2.1.0.
         */
        do_action( 'woocommerce_after_add_to_cart_button' );
    ?>
</form>

修改后的表单是

<form class="cart" method="post" enctype='multipart/form-data'>
    <p>width  <input type="text" name="new-width" class="new-width"></p>
    <p>Height <input type="text" name="new-height" class="new-height"></p>
        <?php
            /**
             * @since 2.1.0.
             */
            do_action( 'woocommerce_before_add_to_cart_button' );

            /**
             * @since 3.0.0.
             */
            do_action( 'woocommerce_before_add_to_cart_quantity' );

            woocommerce_quantity_input( array(
                'min_value'   => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
                'max_value'   => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
                'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $product->get_min_purchase_quantity(),
            ) );

            /**
             * @since 3.0.0.
             */
            do_action( 'woocommerce_after_add_to_cart_quantity' );
        ?>

        <button type="submit" name="add-to-cart" value="<?php echo esc_attr( $product->get_id() ); ?>" class="single_add_to_cart_button button alt"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>

        <?php
            /**
             * @since 2.1.0.
             */
            do_action( 'woocommerce_after_add_to_cart_button' );
        ?>
    </form>

在此之后,我使用以下代码,并且为了测试目的,将value设置为30.但这是行不通的.我错过了什么?

After this i used the following code and for testing pupose i set value to 30 . But it is not working . what i missed ?

add_filter( 'woocommerce_add_cart_item' , 'set_woo_prices');

function set_woo_prices( $woo_data ) {

  if(isset( $_POST['new-width']))  { $woo_data['new-width'] =$_POST['new-width']; }
  if(isset( $_POST['new-height'])) { $woo_data['new-height'] =$_POST['new-height']; }
  if( $_POST['new-width'] !=="" && $_POST['new-height']!=="" ){

     $woo_data['data']->set_price( "30" ); 
    }
     return $woo_data;

}

请咨询.我的目的是根据用户输入的宽度和高度来更改产品价格.

Please advice . My aim is to change product price based on user entered width and height .

推荐答案

您需要使用2个不同的钩子:

You need to use 2 different hooks:

  • 第一个和您一样,而没有尝试更改其中的价格.
  • 第二个更改购物车价格的位置

代码:

add_filter( 'woocommerce_add_cart_item', 'add_custom_cart_item_data', 10, 2 );
function add_custom_cart_item_data( $cart_item_data, $cart_item_key ) {

    if( isset( $_POST['new-width'] ) )
        $cart_item_data['new-width'] = $_POST['new-width'];
    if(isset( $_POST['new-height'] ) )
       $cart_item_data['new-height'] = $_POST['new-height'];

    return $cart_item_data;
}


add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_price', 20, 1 );
function set_custom_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // First loop to check if product 11 is in cart
    foreach ( $cart->get_cart() as $cart_item ){
        if( isset($cart_item['new-width']) && isset($cart_item['new-height']) 
        && ! empty($cart_item['new-width']) && ! empty($cart_item['new-height']) )
            $cart_item['data']->set_price( '30' );
    }
}

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

经测试可正常工作

这篇关于根据Woocommerce中的自定义购物车数据更改购物车价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆