简体   繁体   English

在 Woocommerce 中动态更改特定产品的购物车项目价格和名称

[英]Change cart item price and name for a specific product dynamically in Woocommerce

In WooCommerce I have a specific product which price is set to 0 (the product id is 2938 ) .在 WooCommerce 我有一个价格设置为 0 的特定产品(产品 ID 为2938

When adding that product to cart via href="http://yourdomain.com/?add-to-cart=2938" , I'm trying to set the price dynamically based on the current user's custom post (it's a somewhat complicated calculation based on what posts the user has created, otherwise i'd just use groups/bundles.)通过href="http://yourdomain.com/?add-to-cart=2938"将该产品添加到购物车时,我试图根据当前用户的自定义帖子动态设置价格(这是一个有点复杂的计算基于用户创建的帖子,否则我只会使用组/捆绑包。)

Based on " Change cart item prices in Woocommerce 3 " answer code, here's what I've got:基于更改 Woocommerce 3 中的购物车商品价格答案代码,这是我得到的:

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price', 20, 1);
function add_custom_price( $cart ) {

    // ID of the product I've set up
    $product_id_to_change = 2938;

    $user_id = get_current_user_id();

    $studio_args = array(
        'post_type' => 'studio',
        'post_status' => 'published',
        'posts_per_page' => 1,
        'author' => $user_id
    );

    $studio_query = new WP_Query( $studio_args );

    foreach( $studio_query as $studio ) {
        $studio_listing_name = get_the_title();
        $studio_listing_option = get_post_meta( $studio->ID, 'wpcf-listing-option', true );
    }

    if ( $studio_listing_option = array( 1, 2 ) ) {
        $new_price = 180;
    } elseif ( $studio_listing_option = array( 3, 4 ) ) {
        $new_price = 345;
    } elseif ( $studio_listing_option = array( 5, 6, 7 ) ) {
        $new_price = 690;
    }

    $new_name = 'Payment for 2020 Listing: ' . $user_id . ' - ' . $studio_listing_name . ' - ' . 'Listing Option ' . $studio_listing_option;

    // This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition (when using price calculations for example)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Loop through cart items
    foreach ( $cart->get_cart() as $item ) {
        $item['data']->set_price( $new_price );
        $item['data']->set_name( $new_name );
    }

}

Important to note that each user can only have 1 studio.需要注意的是,每个用户只能拥有 1 个工作室。 I think my foreach and if ( $studio_listing_option = array( 1, 2 ) ) check is wrong/can probably be done better.我认为我的 foreach 和if ( $studio_listing_option = array( 1, 2 ) )检查是错误的/可能可以做得更好。 Any ideas on how I can get it working better?关于如何让它更好地工作的任何想法?

How can I restrict the calculation to just product ID 2938 ?如何将计算限制为仅产品 ID 2938

Also, $studio_listing_name returns blank, and $studio_listing_option returns an array.此外, $studio_listing_name返回空白, $studio_listing_option返回一个数组。 Can I improve on that to get it working properly?我可以改进它以使其正常工作吗?

First, it's difficult to help as we don't know anything about your "studio" custom post type and how the data is set on it for the customers.首先,很难提供帮助,因为我们对您的“工作室”自定义帖子类型以及如何为客户设置数据一无所知。

There are some mistakes and missing things in your code like in your IF statements, where you should need to use in_array() conditional function.你的代码中有一些错误和遗漏的东西,比如在你的IF语句中,你应该需要使用in_array()条件 function。

I have tried to guess how the data is set in wpcf-listing-option meta data for your studio custom post type and I suppose that is a number between 1 (one) and 7 (seven).我试图猜测数据是如何在wpcf-listing-option元数据中为您的studio自定义帖子类型设置的,我想这是一个介于1 (一)和7 (七)之间的数字。

Also in your code, when you loop through $studio_query , for $studio_listing_name and $studio_listing_option you will always get the values from the last item from the loop… so there's something wrong in your logic.同样在您的代码中,当您遍历$studio_query时,对于$studio_listing_name$studio_listing_option您将始终从循环中的最后一项中获取值......所以您的逻辑有问题。

In the following code I am targeting your specified product Id only (without any guaranty, it should help even if it doesn't work completely) :在下面的代码中,我只针对您指定的产品 ID (没有任何保证,即使它不能完全工作,它也应该有所帮助)

add_action( 'woocommerce_before_calculate_totals', 'customize_cart_item_details', 20, 1);
function customize_cart_item_details( $cart ) {

    // This is necessary for WC 3.0+
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding hook repetition (when using price calculations for example)
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 || ! is_user_logged_in() )
        return;

    // ID of the product I've set up
    $defined_product_id = 2938;

    $customer_id = get_current_user_id();

    // Get "studio" custom post objects
    $studio_query = get_posts( array(
        'post_type' => 'studio',
        'post_status' => 'published',
        'posts_per_page' => 1,
        'author' => $customer_id
    ) );

    // Get the first "studio" post for the current user
    $studio = reset( $studio_query );

    $studio_name   = $studio->post_title;
    $studio_option = get_post_meta( $studio->ID, 'wpcf-listing-option', true );

    if ( in_array( $studio_option, array( 1, 2 ) ) ) {
        $new_price = 180;
    } elseif ( in_array( $studio_option, array( 3, 4 ) ) ) {
        $new_price = 345;
    } elseif ( in_array( $studio_option, array( 5, 6, 7 ) ) ) {
        $new_price = 690;
    }

    $new_name = sprintf( 
        __( "Payment for 2020 Listing: %s - %s - Listing Option %s", "woocommerce" ),
        $customer_id, $studio_name, $studio_option 
    );

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Only for the defined product ID (or variation ID)
        if ( in_array( $defined_product_id, [ $cart_item['product_id'], $cart_item['variation_id'] ] ) ) {
            $cart_item['data']->set_price( $new_price );
            $cart_item['data']->set_name( $new_name );   
        }
    }
}

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

相关问题 Woocommerce中特定产品类别的空购物车项目价格 - Null cart item price for a specific product category in Woocommerce 在WooCommerce中动态更改产品价格 - Change product price dynamically in WooCommerce 在 Woocommerce 购物车结账和订单中禁用特定产品的项目名称链接 - Disable item name link for specific product in Woocommerce cart checkout and orders 一旦特定变体发生变化,Woocommerce 会动态更改可变产品的价格 - Woocommerce dynamically change price of a variable product once a specific variation is changes 在购物车中更改WooCommerce产品差异的原始价格 - Change original price in cart for WooCommerce product variation 通过自定义字段更改购物车中的产品价格,Woocommerce - Change product price in Cart by custom field, Woocommerce 更改 WooCommerce 购物车和结帐中的产品价格 - Change price of product in WooCommerce cart and checkout 更改Woocommerce中特定产品类别的购物车项目价格 - Change cart item prices for specific product categories in Woocommerce 根据Woocommerce中的特定产品属性值更改购物车商品价格 - Change cart item prices based on specific product attribute value in Woocommerce 设置购物车商品仅针对Woocommerce中的特定商品生成的销售价格 - Set cart item product generated sale price only for specific products in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM