0

我将 WooCommerce 会员资格用于一种产品,其余的 WooCommerce 产品是通用产品。

但我希望访问者应该能够注册 WooCommerce 会员资格,但不能注册其他产品。对于其他产品,我想使用访客结帐。

4

1 回答 1

2

GitHub 上已经有一个可以使用的解决方案。

// Code goes in theme functions.php or a custom plugin
add_filter( 'pre_option_woocommerce_enable_guest_checkout', 'conditional_guest_checkout_based_on_product' );
function conditional_guest_checkout_based_on_product( $value ) {
  $restrict_ids = array( 1, 2, 3 ); // Replace with product ids which cannot use guest checkout

  if ( WC()->cart ) {
    $cart = WC()->cart->get_cart();
    foreach ( $cart as $item ) {
      if ( in_array( $item['product_id'], $restrict_ids ) ) {
        $value = "no";
        break;
      }
    }
  }

  return $value;
}
于 2018-03-02T02:45:59.110 回答