作为单选按钮的WooCommerce变奏曲

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了作为单选按钮的WooCommerce变奏曲相关的知识,希望对你有一定的参考价值。

是否有可能在WooCommerce中将变体下拉列表更改为单选按钮而无需使用插件?我想在变体部分进行以下设置:

  • 1升(10€)
  • 2升(20€)
  • 3升(25€)

选择选项时,应自动更改底部的价格。

谢谢

答案

我不知道如何在没有插件的情况下做到这一点,但我建议你放弃这个要求,并使用Woocommerce Radio Buttons插件。这完全符合您的要求:

引用:

这是一个简单轻巧的插件,一旦安装和激活,将把您的Woocommerce变体从下拉菜单转换为单选按钮。让您的消费者无需访问下拉菜单即可查看所有变体。 (可选)确保您使用的是产品的变体,并在编辑产品页面中设置默认变体,以自动显示“添加到购物车”按钮。

另一答案

我设法使其工作的最佳方法是在选择下拉列表后直接添加单选按钮标记,然后使用CSS隐藏选择下拉列表。您还需要一些自定义JS来触发隐藏的选择值更改,以便您的价格根据您选择的单选按钮而改变。

这是我添加标记的方式:

function variation_radio_buttons($html, $args) {
  $args = wp_parse_args(apply_filters('woocommerce_dropdown_variation_attribute_options_args', $args), array(
    'options'          => false,
    'attribute'        => false,
    'product'          => false,
    'selected'         => false,
    'name'             => '',
    'id'               => '',
    'class'            => '',
    'show_option_none' => __('Choose an option', 'woocommerce'),
 ));

  if(false === $args['selected'] && $args['attribute'] && $args['product'] instanceof WC_Product) {
    $selected_key     = 'attribute_'.sanitize_title($args['attribute']);
    $args['selected'] = isset($_REQUEST[$selected_key]) ? wc_clean(wp_unslash($_REQUEST[$selected_key])) : $args['product']->get_variation_default_attribute($args['attribute']);
  }

  $options               = $args['options'];
  $product               = $args['product'];
  $attribute             = $args['attribute'];
  $name                  = $args['name'] ? $args['name'] : 'attribute_'.sanitize_title($attribute);
  $id                    = $args['id'] ? $args['id'] : sanitize_title($attribute);
  $class                 = $args['class'];
  $show_option_none      = (bool)$args['show_option_none'];
  $show_option_none_text = $args['show_option_none'] ? $args['show_option_none'] : __('Choose an option', 'woocommerce');

  if(empty($options) && !empty($product) && !empty($attribute)) {
    $attributes = $product->get_variation_attributes();
    $options    = $attributes[$attribute];
  }

  $radios = '<div class="variation-radios">';

  if(!empty($options)) {
    if($product && taxonomy_exists($attribute)) {
      $terms = wc_get_product_terms($product->get_id(), $attribute, array(
        'fields' => 'all',
      ));

      foreach($terms as $term) {
        if(in_array($term->slug, $options, true)) {
          $radios .= '<input type="radio" name="'.esc_attr($name).'" value="'.esc_attr($term->slug).'" '.checked(sanitize_title($args['selected']), $term->slug, false).'><label for="'.esc_attr($term->slug).'">'.esc_html(apply_filters('woocommerce_variation_option_name', $term->name)).'</label>';
        }
      }
    } else {
      foreach($options as $option) {
        $checked    = sanitize_title($args['selected']) === $args['selected'] ? checked($args['selected'], sanitize_title($option), false) : checked($args['selected'], $option, false);
        $radios    .= '<input type="radio" name="'.esc_attr($name).'" value="'.esc_attr($option).'" id="'.sanitize_title($option).'" '.$checked.'><label for="'.sanitize_title($option).'">'.esc_html(apply_filters('woocommerce_variation_option_name', $option)).'</label>';
      }
    }
  }

  $radios .= '</div>';

  return $html.$radios;
}
add_filter('woocommerce_dropdown_variation_attribute_options_html', 'variation_radio_buttons', 20, 2);

这是我使用的JS:

$(document).on('change', '.variation-radios input', function() {
  $('select[name="'+$(this).attr('name')+'"]').val($(this).val()).trigger('change');
});

以上是关于作为单选按钮的WooCommerce变奏曲的主要内容,如果未能解决你的问题,请参考以下文章

根据 WooCommerce 中的自定义结帐单选按钮和文本字段设置动态费用

WooCommerce 结帐单选按钮,可根据特定项目小计设置百分比费用

Wordpress - 单选按钮结帐 woocommerce 显示/隐藏必填字段

根据 Woocommerce 结帐中的单选按钮动态更新费用

单选按钮以片段形式传递数据

在产品页面上显示运费 - WooCommerce