WooCommerce [订阅]:用于升级和降级的分组产品的单选按钮

WooCoomerce [Subscription]: Radio buttons at Grouped Products for Upgradation & Downgradation

WooCommerce 订阅提供了升级或降级到其他订阅产品的能力。

  1. 分组产品
  2. 变量订阅。

在单个产品页面使用分组订阅时,使用复选框显示所有订阅的价格。这变得非常混乱,因为您只能 select 一种产品。我正在尝试将它们转换为单选按钮,但我做不到。

You can see the request for the idea raised at : https://ideas.woocommerce.com/forums/133476-woocommerce/suggestions/38542126-radio-buttons-instead-of-checkboxes-for-grouped-pr

我试过的

@foreach ( $grouped_products as $grouped_product_child )
    <input type="radio" id="@php echo $grouped_product_child->get_id() @endphp" name="grouped-product" value="@php echo $grouped_product_child->get_id() @endphp">
@endforeach

<input type="hidden" name="add-to-cart" value="@php echo esc_attr( $product->get_id() ); @endphp" />
 
@if($quantites_required)
    <button type="submit">Subscribe Now</button>
@endif

我特意留下了检查输入选项是否可见的逻辑。

每次单击“立即订阅”菜单时,都会收到一条错误消息,要求我 Please choose a product to add to your cart…

我不得不编辑 single-product/add-to-cart 中的 grouped.php 文件。我复制了文件并进行了以下更改。

@php global $product, $post; @endphp

@php
  $quantites_required      = false;
  $previous_post           = $post;
@endphp

<div class="purchase">

  <div class="prices">

<form class="cart grouped_form"
      action="@php esc_url( apply_filters( 'woocommerce_add_to_cart_form_action', $product->get_permalink() ) ); @endphp"
      method="post"
      enctype='multipart/form-data'
      x-data="{product: '2370'}" >
  <div>
    @foreach ( $grouped_products as $key => $grouped_product_child )
      @php
        $post_object        = get_post( $grouped_product_child->get_id() );
        $quantites_required = $quantites_required || ( $grouped_product_child->is_purchasable() && ! $grouped_product_child->has_options() );
        $post               = $post_object;
        setup_postdata( $post );
      @endphp

      <div class="form-groups grouped-product-item" data-product="@php echo $grouped_product_child->get_id(); @endphp">
        @if (! $grouped_product_child->is_purchasable() || $grouped_product_child->has_options() || ! $grouped_product_child->is_in_stock())
          @php woocommerce_template_loop_add_to_cart(); @endphp
        @else ( $grouped_product_child->is_sold_individually() )
          <input type="checkbox" id="@php echo $grouped_product_child->get_id() @endphp" name="quantity[@php echo $grouped_product_child->get_id() @endphp]" value="1"
                 on:click="product = @php echo $grouped_product_child->get_id() @endphp" @if ($key == 0) checked @endif>
        @endif
        <label for="@php echo $grouped_product_child->get_id() @endphp"
               @click="$dispatch('productchange', {product: '@php echo $grouped_product_child->get_id() @endphp'})"
        >
          <span class="detail">
            <div class="cost">
              <h3><span>₹</span>
              @php echo \WC_Subscriptions_Product::get_price_string($grouped_product_child); @endphp
              </h3>
            </div>

          <div class="extra">
            @if(App\Product::inclusiveOfAllClasses($grouped_product_child->get_id()))
              <div class="class">
              <span>Inclusive of All Classes</span>
            </div>
            @endif
            @if( App\Product::savePercentage($grouped_product_child->get_id()) )
              <div class="save">
              Save {{ App\Product::savePercentage($grouped_product_child->get_id()) }}%
            </div>
            @endif
          </div>
          </span>
        </label>
      </div>

    @endforeach
  </div>

<input type="hidden" name="add-to-cart" value="@php echo esc_attr( $product->get_id() ); @endphp" />

  <div x-data="{product: '1'}"
       @productchange.window="product = $event.detail.product"
       x-init=" product = $store.application.product"
       x-subscribe>
  @foreach ( $grouped_products as $key => $grouped_product_child )
    <div x-show.transition.in="product == {{ $grouped_product_child->get_id() }}">
      @if($quantites_required)
        @if(\App\CartValidation::isCurrentProductInCart(intval($grouped_product_child->get_id())))
          <div class="pricing-information">
            <a href="javascript:void(0)" class="crush-button crush-button-gradient cart-open">
              View Cart
            </a>
            <div class="help-text">
              <img src="@asset('images/caution.svg')" alt="Caution">
              <h6>Product already added to the cart.</h6>
            </div>
          </div>
        @else
          <button type="submit" class="hide-on-mobile actual-submit">Subscribe Now</button>
        @endif
      @endif
    </div>
  @endforeach
  </div>

</form>
  </div>
</div>

请注意这是一个 blade 文件。如果您使用的是 .php,则必须将其转换为 vanilla php.