WooCommerce 结帐中的本地取货自定义百分比折扣

在 Woocommerce 应答代码中使用本地取货运输选项自定义百分比折扣, 以使 Woocommerce 上的本地取货折扣为 2%。因此,选择自取订单的人可以获得总金额的折扣。这段代码很棒,但我想将其隐藏在购物车页面上,并且仅显示在结帐页面上。现在它显示在两个页面上。

我尝试更改$cart$checkout删除了规则 15 和 16,但不幸的是,这不起作用。有人指出我正确的方向吗?


跃然一笑
浏览 77回答 1
1回答

小唯快跑啊

要使其仅在结帐页面上激活,请使用以下命令:add_action( 'woocommerce_cart_calculate_fees', 'custom_discount_for_pickup_shipping_method', 10, 1 );function custom_discount_for_pickup_shipping_method( $cart ) {&nbsp; &nbsp; if ( is_admin() && ! defined( 'DOING_AJAX' ) )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; // Only on checkout page&nbsp; &nbsp; if ( is_checkout() ) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $percentage = 2; // <=== Discount percentage&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $chosen_shipping_method_id = WC()->session->get( 'chosen_shipping_methods' )[0];&nbsp; &nbsp; &nbsp; &nbsp; $chosen_shipping_method&nbsp; &nbsp; = explode(':', $chosen_shipping_method_id)[0];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Only for Local pickup chosen shipping method&nbsp; &nbsp; &nbsp; &nbsp; if ( strpos( $chosen_shipping_method_id, 'local_pickup' ) !== false ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Calculate the discount&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $discount = $cart->get_subtotal() * $percentage / 100;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Add the discount&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cart->add_fee( __('Pickup discount') . ' (' . $percentage . '%)', -$discount );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}代码位于活动子主题(活动主题)的 function.php 文件中。经过测试并有效。
打开App,查看更多内容
随时随地看视频慕课网APP