WooCommerce促销折扣:买10送1

时间:2020-09-13 17:19:35

标签: php wordpress woocommerce cart discount

我正在尝试为三种可变产品(464、465和466)设置特定的折扣。如果客户购买十个产品,他们将免费获得一个。

基于WooCommerce discount: buy one get one 50% off的答案代码,我提出了以下代码:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_11th_at_100', 10, 1 );
function add_custom_discount_11th_at_100( $wc_cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    $discount = 0;
    $items_prices = array();

    // Set HERE your targeted variable product ID
    $targeted_product_id = 464;

    foreach ( $wc_cart->get_cart() as $key => $cart_item ) {
        if( $cart_item['product_id'] == $targeted_product_id ){
            $qty = intval( $cart_item['quantity'] );
            for( $i = 0; $i < $qty; $i++ )
                $items_prices[] = floatval( $cart_item['data']->get_price());
        }
    }
    $count_items_prices = count($items_prices);
    if( $count_items_prices > 10 ) foreach( $items_prices as $key => $price )
        if( $key % 11 == 1 ) $discount -= number_format($price / 1, 11 );

    if( $discount != 0 ){
        // Displaying a custom notice (optional)
        wc_clear_notices();
        wc_add_notice( __("Buy 10 Get 1 Free"), 'notice');

        // The discount
        $wc_cart->add_fee( 'Buy 10 Get 1 Free', $discount, true  );
        # Note: Last argument in add_fee() method is related to applying the tax or not to the discount (true or false)
    }
}

但是它仅适用于一个产品ID。如何将其扩展为适用于三个产品ID?

1 个答案:

答案 0 :(得分:2)

要使其适用于多种产品,您可以使用 init() { // Title text color let navBarAppearance = UINavigationBar.appearance() navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white] navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white] navBarAppearance.backgroundColor = .clear // Clear list background color UITableView.appearance().separatorStyle = .none UITableViewCell.appearance().backgroundColor = .clear UITableView.appearance().backgroundColor = .clear UITableViewCell.appearance().selectionStyle = .none } var body: some View { NavigationView { ZStack { LinearGradient(gradient: Gradient(colors: [Color("DarkShade"), Color("PrimaryDark")]), startPoint: .top, endPoint: .bottom) .edgesIgnoringSafeArea(.all) List(self.viewModel.rooms.enumerated().map({ $0 }), id: \.element.id) { (index, room) in ZStack { RoomsItemView(room: room) .onAppear(perform: { let count = self.viewModel.rooms.count if index == count - 1 { self.viewModel.loadRooms(currentListSize: count) } }) NavigationLink(destination: GuestRoomView(room: room)) { EmptyView() } } } } .pullToRefresh(isShowing: self.$isRefreshing, onRefresh: self.onRefresh) .navigationBarTitle("rooms_title") .navigationBarBackButtonHidden(true) .navigationBarItems( leading: Button(action: { self.shouldShowSpotifyAuth = false }) { VStack { Spacer() HStack { Image(systemName: "chevron.left") .resizable() .scaledToFit() .frame(height: 20.0) Spacer() } Spacer() } .frame(width: 40.0, height: 30.0) }, trailing: Button(action: { print("create room") }) { VStack { Spacer() HStack { Spacer() Image(systemName: "plus") .resizable() .scaledToFit() .frame(height: 20.0) } Spacer() } .frame(width: 40.0, height: 30.0) } ) } .onAppear(perform: { self.onRefresh() }) .onReceive(viewModel.onRoomsUpdate) { self.isRefreshing = false } .gesture(DragGesture().updating($dragOffset, body: { (value, state, transaction) in if(value.startLocation.x < 20 && value.translation.width > 100) { self.shouldShowSpotifyAuth = false } }) ) } php函数,如下所示:

in_array()

代码进入您的活动子主题(或活动主题)的function.php文件中,已经过测试并且可以正常工作。

相关问题