doxqszx09742 2017-12-05 05:41
浏览 135
已采纳

根据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 .

I created this form in

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

Before modification the form is

<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>

After modification form is

<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>

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 .

  • 写回答

1条回答 默认 最新

  • dpgf42422 2017-12-05 07:22
    关注

    You need to use 2 different hooks:

    • The first one just as yours without trying to change the price in it.
    • The second one where you will change your cart item price

    The code:

    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' );
        }
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存