gpt4 book ai didi

php - WooCommerce 中特定产品标签 ID 的购物车批量折扣

转载 作者:行者123 更新时间:2023-12-04 07:34:32 26 4
gpt4 key购买 nike

我想创建一个函数来为 WooCommerce 创建有条件的批量动态定价,根据添加到购物车的数量不同的单价。
我有两个产品标签 ID(326、327),每个标签中有一堆产品。我想为两个标签和相同数量提供不同的折扣。

  • 标签 ID 326
  • 10 件以上 15% 折扣
  • 25 件以上 20% 折扣
  • 标签 ID 327
  • 10台以上优惠20%
  • 超过 25 件可享受 35% 的折扣。

  • 这是我正在使用的代码:
    add_action( 'woocommerce_before_calculate_totals', 'woocommerce_quantity_based_pricing', 9999 );
    function woocommerce_quantity_based_pricing( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;

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


    if( has_term( 326, 'product_tag' ) ) {
    $threshold1 = 10;
    $discount1 = 0.15;
    $threshold2 = 25;
    $discount2 = 0.3;
    } elseif( has_term( 327, 'product_tag' ) ) {
    $threshold1 = 10;
    $discount1 = 0.2;
    $threshold2 = 25;
    $discount2 = 0.35;
    }

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
    if ( $cart_item['quantity'] >= $threshold1 && $cart_item['quantity'] < $threshold2 ) {
    $price = round( $cart_item['data']->get_price() * ( 1 - $discount1 ), 2 );
    $cart_item['data']->set_price( $price );
    } elseif ( $cart_item['quantity'] >= $threshold2 ) {
    $price = round( $cart_item['data']->get_price() * ( 1 - $discount2 ), 2 );
    $cart_item['data']->set_price( $price );
    }
    }

    }
    不幸的是没有想要的结果。有什么建议吗?

    最佳答案

    要根据产品标签 ID 应用折扣,您可以使用 WC_Product::get_tag_ids() ,然后进行比较。
    我通过 woocommerce_cart_item_price 提供了一段额外的代码钩子(Hook),这将允许您删除原始价格并在其旁边显示新价格
    所以你得到:

    // Used to calculate totals
    function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

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

    // Set Tag IDs
    $tag_id_1 = 326;
    $tag_id_2 = 327;

    // Tag ID 1
    // 15% discount above 10 units
    $tag_id_1_threshold_1 = 10;
    $tag_id_1_discount_1 = 0.15;
    // 20% discount above 25 units
    $tag_id_1_threshold_2 = 25;
    $tag_id_1_discount_2 = 0.20;

    // Tag ID 2
    // 20% discount above 10 units
    $tag_id_2_threshold_1 = 10;
    $tag_id_2_discount_1 = 0.20;
    // 35% discount above 25 units
    $tag_id_2_threshold_2 = 25;
    $tag_id_2_discount_2 = 0.35;

    // Loop through cart items
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
    // Get price
    $price = $cart_item['data']->get_price();

    // Get quantity
    $quantity = $cart_item['quantity'];

    // Get product tag ID's
    $tag_ids = $cart_item['data']->get_tag_ids();

    // Check for tag ID
    if ( in_array ( $tag_id_1, $tag_ids ) ) {
    // Compare
    if ( $quantity >= $tag_id_1_threshold_1 && $quantity < $tag_id_1_threshold_2 ) {
    $discount = $tag_id_1_discount_1;
    } elseif ( $quantity >= $tag_id_1_threshold_2 ) {
    $discount = $tag_id_1_discount_2;
    }
    } elseif ( in_array ( $tag_id_2, $tag_ids ) ) {
    // Compare
    if ( $quantity >= $tag_id_2_threshold_1 && $quantity < $tag_id_2_threshold_2 ) {
    $discount = $tag_id_2_discount_1;
    } elseif ( $quantity >= $tag_id_2_threshold_2 ) {
    $discount = $tag_id_2_discount_2;
    }
    }

    // Isset & NOT empty
    if ( isset( $discount ) && ! empty( $discount ) ) {
    // Set new price
    $cart_item['data']->set_price( $price * ( 1 - $discount ) );

    // Reset
    $discount = '';
    }
    }
    }
    add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );

    // Strikethrough the original price
    function filter_woocommerce_cart_item_price( $price_html, $cart_item, $cart_item_key ) {
    // Get the product object
    $product = $cart_item['data'];

    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
    // Get reqular price
    $regular_price = $product->get_regular_price();

    // New price
    $new_price = $cart_item['data']->get_price();

    // NOT empty and NOT equal
    if ( ! empty ( $regular_price ) && $regular_price != $new_price ) {
    // Output
    $price_html = '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $new_price ) . '</ins>';
    }
    }

    return $price_html;
    }
    add_filter( 'woocommerce_cart_item_price', 'filter_woocommerce_cart_item_price', 10, 3 );

    enter image description here

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

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