jquery 根据WooCommerce结账中的付款方式设置产品价格

kt06eoxx  于 2023-08-04  发布在  jQuery
关注(0)|答案(2)|浏览(119)

我想在选择特定付款方式时,在结账时将单个产品的价格设置为0。我的代码将这些产品的价格设置为0,并且正确计算了小计。然而,税收和总额是错误的。Woocommerce假设价格没有变化。
有人可以检查这个代码并找到错误吗?

function update_product_prices_and_totals($cart_object) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    if (is_checkout() && isset($_POST['payment_method']) && $_POST['payment_method'] === 'invoice') {
        // Set subtotal, tax, and total to 0
        $subtotal = 0;
        $total = 0;
        $included_products = array(338991, 353754); // Array of product IDs to include

        // Loop through the products in the cart
        foreach ($cart_object->get_cart() as $cart_item_key => $cart_item) {
            $product_id = $cart_item['product_id'];

            // Check the product IDs
            if (in_array($product_id, $included_products)) {
                // Set the price to 0
                $cart_item['data']->set_price(0);

                // Calculate taxes for this product and subtract from the subtotal
                $taxes = array_sum($cart_item['line_tax_data']['subtotal']);
                $subtotal -= $taxes;

                continue; // Skip the iteration for included products
            }

            // Update the subtotal
            $subtotal += $cart_item['line_subtotal'];
        }

        // Update the total
        $total = $subtotal + $cart_object->get_cart_contents_tax();

        // Set the updated subtotal and total
        $cart_object->subtotal = $subtotal;
        $cart_object->total = $total;
    }
}
add_action('woocommerce_cart_calculate_fees', 'update_product_prices_and_totals', 10, 1);

字符串
我试图改变总和税收从Woocommerce方法。一个更好的方法是将这些产品的税收直接设置为0,这也不起作用。

ux6nzvsh

ux6nzvsh1#

你没有使用正确的勾拳,把事情弄得比实际情况复杂得多。请尝试以下操作:

add_action('woocommerce_before_calculate_totals', 'update_product_prices_and_totals' );
function update_product_prices_and_totals( $cart ) {
    if ( is_admin() && ! defined('DOING_AJAX') )
        return;

    if (  is_checkout() && ! is_wc_endpoint_url() && WC()->session->get('chosen_payment_method') === 'invoice') {
        // Define here the array of targeted product IDs
        $targeted_ids = array(338991, 353754);

        // Loop through cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            // Check for the targeted product (IDs) to be changed
            if ( in_array($cart_item['product_id'], $targeted_ids) || in_array($cart_item['variation_id'], $targeted_ids) ) {
                $cart_item['data']->set_price(0); // Set the price to 0
            }
        }
    }
}

// Refresh checkout on payment method change.
add_action('woocommerce_checkout_init', 'payment_methods_trigger_update_checkout');
function payment_methods_trigger_update_checkout() {
    wc_enqueue_js("$('form.checkout').on('change', 'input[name=payment_method]', function() {
        $(document.body).trigger('update_checkout');
    });");
}

字符串
这段代码放在活动子主题(或活动主题)的functions.php文件中。
测试和工程,而无需更新任何税收或总额。

aoyhnmkz

aoyhnmkz2#

我曾在许多电子商务应用程序,所以我有一个想法,但我不太确定它是否会工作。
你必须在继续之前减去if条件中的税;

$total -= $taxes;

字符串
然后,您必须在合计计算中包括每个产品的line_subtotal

$subtotal += $cart_item['line_subtotal'];
$total += $cart_item['line_subtotal'];


所以你的代码应该看起来像这样。

function update_product_prices_and_totals($cart_object) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    if (is_checkout() && isset($_POST['payment_method']) && $_POST['payment_method'] === 'invoice') {
        // Set subtotal, tax, and total to 0
        $subtotal = 0;
        $total = 0;
        $included_products = array(338991, 353754); // Array of product IDs to include

        // Loop through the products in the cart
        foreach ($cart_object->get_cart() as $cart_item_key => $cart_item) {
            $product_id = $cart_item['product_id'];

            // Check the product IDs
            if (in_array($product_id, $included_products)) {
                // Set the price to 0
                $cart_item['data']->set_price(0);

                // Calculate taxes for this product and subtract from the subtotal
                $taxes = array_sum($cart_item['line_tax_data']['subtotal']);
                $subtotal -= $taxes;
                $total -= $taxes;

                continue; // Skip the iteration for included products
            }

            // Update the subtotal
            $subtotal += $cart_item['line_subtotal'];
            $total += $cart_item['line_subtotal'];
        }

        // Set the updated subtotal and total
        $cart_object->subtotal = $subtotal;
        $cart_object->total = $total;
    }
}
add_action('woocommerce_cart_calculate_fees', 'update_product_prices_and_totals', 10, 1);


注意:我不知道这个**$cart_object->get_cart_contents_tax()**(我没有足够的声誉来评论,所以如果你不介意告诉我)检查这个解决方案,并给予我你的反馈,如果它的工作或没有

相关问题