简体   繁体   English

根据 Woocommerce 中的自定义购物车项目数据更改购物车项目价格

[英]Change cart item prices based on custom cart item data in 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 woocommerce->单一产品->加入购物车->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 .在此之后,我使用了以下代码,为了测试目的,我将值设置为 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 .我的目标是根据用户输入的 width 和 height 更改产品价格。

You need to use 2 different hooks:您需要使用 2 个不同的钩子:

  • 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 ) {

    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.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

Tested and works测试和工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Woocommerce 3 中更改购物车商品价格 - Change cart item prices in Woocommerce 3 根据 Woocommerce 中的自定义购物车项目数据自定义购物车项目小计 - Customize cart item subtotal based on custom cart item data in Woocommerce 根据Woocommerce中的特定产品属性值更改购物车商品价格 - Change cart item prices based on specific product attribute value in Woocommerce WooCommerce中的自定义条件购物车项目价格 - Custom conditional cart item prices in WooCommerce 根据自定义字段和数量阈值更改 WooCommerce 购物车商品价格 - Change WooCommerce cart item price based on a custom field and quantity threshold 根据Woocommerce中不同的基本价格计算购物车商品价格 - Cart item price calculation, based on different basic prices in Woocommerce 根据 Woocommerce 中的购物车商品价格有条件地设置不同的税率 - Set different Tax rates conditionally based on cart item prices in Woocommerce 根据 WooCommerce 中的付款方式提高购物车商品价格 - Increase cart item prices based on payment method in WooCommerce 更改Woocommerce中特定产品类别的购物车项目价格 - Change cart item prices for specific product categories in Woocommerce 将自定义购物车商品值添加到WooCommerce订单商品元数据 - Add a custom cart item value to WooCommerce order item meta data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM