gpt4 book ai didi

php - Woocommerce 中特定产品的购物车批量折扣

转载 作者:行者123 更新时间:2023-12-04 13:02:30 34 4
gpt4 key购买 nike

在 woocommerce 中,我试图仅针对特定产品 ID 进行自定义购物车项目数量折扣。这就是我想要做的:
如果客户将特定产品添加到购物车,我希望保留第一件商品的原始价格,并为每个额外的商品数量设置折扣自定义价格。

例如:

  • 产品有效价格为 40(我们为第 1 个数量的项目保留此价格)
  • 数量不超过 2(附加项目数量)的价格为 12。

  • 因此,如果客户添加数量为 5 件的产品,则价格将为:
    40 + 12 + 12 + 12 + 12 = 88

    所以我试着稍微改变这个答案代码:
    Cart item quantity progressive percentage discount in Woocommerce 3

    但无法使其正常工作。

    任何帮助表示赞赏。

    最佳答案

    对于您的情况,请改用以下内容:

    add_filter('woocommerce_add_cart_item_data', 'add_items_default_price_as_custom_data', 20, 3 );
    function add_items_default_price_as_custom_data( $cart_item_data, $product_id, $variation_id ){

    ## Your settings her below ##
    $product_ids = array(37); // <=== Your specific product(s) ID(s) in the array
    $discounted_price = 12; // <=== The specific product discounted price

    // The WC_Product Object
    $product = wc_get_product($variation_id > 0 ? $variation_id : $product_id);

    if( array_intersect( $product_ids, array($product_id, $variation_id) ) && ! $product->is_on_sale() ){
    // We set the Product discounted price
    $cart_item_data['pricing']['discounted'] = $discounted_price;

    // Set the Product default base price as custom cart item data
    $cart_item_data['pricing']['active'] = $product->get_price();
    }

    return $cart_item_data;
    }

    // Display the product original price
    add_filter('woocommerce_cart_item_price', 'display_cart_items_default_price', 20, 3 );
    function display_cart_items_default_price( $product_price, $cart_item, $cart_item_key ){
    if( isset($cart_item['pricing']['active']) && $cart_item['quantity'] > 1 ) {
    $product_price = wc_price( wc_get_price_to_display( $cart_item['data'], array( 'price' => $cart_item['pricing']['active'] ) ) );
    }
    return $product_price;
    }

    // Display the product name with the a custom "discount" label
    add_filter( 'woocommerce_cart_item_name', 'append_custom_label_to_item_name', 20, 3 );
    function append_custom_label_to_item_name( $product_name, $cart_item, $cart_item_key ){
    if( isset($cart_item['pricing']['discounted']) && $cart_item['data']->get_price() != $cart_item['pricing']['discounted'] ) {
    $discounted_price = (float) $cart_item['pricing']['discounted'];
    $default_price = (float) $cart_item['pricing']['active'];
    $quantity = (int) $cart_item['quantity'];

    // Calculate new product price
    $new_price = ($default_price + ($discounted_price * ($quantity - 1))) / $quantity;

    // Get the discount percentage (if needed)
    $percent = 100 - ($new_price / $default_price * 100);

    $product_name .= ' <em>(' . __('quantity discounted', 'woocommerce') . ')</em>';
    }
    return $product_name;
    }

    // Set the new discounted price
    add_action( 'woocommerce_before_calculate_totals', 'set_custom_discount_cart_item_price', 25, 1 );
    function set_custom_discount_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

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

    foreach( $cart->get_cart() as $cart_item ){

    // For items non on sale set a discount based on quantity
    if( isset($cart_item['pricing']['discounted']) && $cart_item['quantity'] > 1 ) {
    $discounted_price = (float) $cart_item['pricing']['discounted'];
    $default_price = (float) $cart_item['pricing']['active'];
    $quantity = (int) $cart_item['quantity'];

    // Calculate new product price
    $new_price = ($default_price + ($discounted_price * ($quantity - 1))) / $quantity;

    // Set cart item calculated price
    $cart_item['data']->set_price($new_price);
    }
    }
    }
    代码位于事件子主题(或事件主题)的 function.php 文件中。测试和工作。

    关于php - Woocommerce 中特定产品的购物车批量折扣,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52484559/

    34 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com