Commit aa21f946 authored by Nahla Shiri's avatar Nahla Shiri

variation radio

parent 41b5ccf2
This diff is collapsed.
=== WC Variations Radio Buttons ===
Contributors: mantish
Donate link: mailto:paypal@8manos.com
Tags: woocommerce, variations, woocommerce variations, radio buttons, variations radio, variations radio buttons
Requires at least: 4.4
Tested up to: 4.7.4
Stable tag: 2.0.0
WC requires at least: 3.0
WC tested up to: 3.0.3
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Variations Radio Buttons for WooCommerce. Let your customers choose product variations using radio buttons instead of dropdowns.
== Description ==
Radio buttons are more friendly than dropdown selects. This plugin will help you have a better UX in your WooCommerce site, as your customers will see all your variations without having to click on a dropdown.
The radio buttons will be toggled dinamically so your customers won't be able to choose non existent variations.
### Theme Compatibility
This plugin may not work out of the box with some themes. This is due to a theme having its own template for variation selection (single-product/add-to-cart/variable.php).
To make this plugin work with a non compatible theme, a [child theme](http://themify.me/docs/child-themes) has to be created.
Then copy plugins/wc-variations-radio-buttons/templates/single-product/add-to-cart/variable.php to themes/your-child-theme/woocommerce/single-product/add-to-cart/variable.php
### Github
Source code and contributions at [github](https://github.com/8manos/wc-variations-radio-buttons)
== Changelog ==
= 2.0.0 =
* Updated to match changes in WooCommerce 3.0.
* This version is not compatible with WooCommerce 2.x.
* Fix issues with the variation image not changing.
= 1.1.5 =
* Fix error when using product_page WooCommerce shortcode.
* Show alert when clicking disabled add to cart button.
= 1.1.4 =
* Avoid overwriting of custom add-to-cart button.
= 1.1.3 =
* Fixes clear button that got screwed on last version.
= 1.1.2 =
* Updated to match changes in WooCommerce 2.5.
* Image disappearing in certain themes fixed.
= 1.1.1 =
* Support for responsive images as in WordPress 4.4 and WooCommerce 2.4.11.
* Checked compatibility with WooCommerce 2.5.0-RC1.
= 1.1.0 =
* Full compatibility with WooCommerce 2.4.
* Variations are called via AJAX when there are many variations.
* Changed version requirements: At least WordPress 4.1 and WooCommerce 2.4.
= 1.0.3 =
* Now works with woocommerce_variation_is_active filter.
* Fix bug that prevented the plugin to work with custom attributes.
* Hopefuly no more "The plugin does not have a valid header." message.
= 1.0.2 =
* Now works better with some themes.
= 1.0.1 =
* WooCommerce 2.4 compatibility.
= 1.0 =
* First release.
\ No newline at end of file
<?php
/**
* Variable product add to cart
*
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.5.0
*
* Modified to use radio buttons instead of dropdowns
* @author 8manos
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'print_attribute_radio' ) ) {
function print_attribute_radio( $checked_value, $value, $label, $name ) {
// This handles < 2.4.0 bw compatibility where text attributes were not sanitized.
$checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );
$input_name = 'attribute_' . esc_attr( $name ) ;
$esc_value = esc_attr( $value );
$id = esc_attr( $name . '_v_' . $value );
$filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );
printf( '<div><input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s><label for="%3$s">%5$s</label></div>', $input_name, $esc_value, $id, $checked, $filtered_label );
}
}
global $product;
$attribute_keys = array_keys( $attributes );
do_action( 'woocommerce_before_add_to_cart_form' ); ?>
<form class="variations_form cart" method="post" enctype='multipart/form-data' data-product_id="<?php echo absint( $product->get_id() ); ?>" data-product_variations="<?php echo htmlspecialchars( wp_json_encode( $available_variations ) ) ?>">
<?php do_action( 'woocommerce_before_variations_form' ); ?>
<?php if ( empty( $available_variations ) && false !== $available_variations ) : ?>
<p class="stock out-of-stock"><?php _e( 'This product is currently out of stock and unavailable.', 'woocommerce' ); ?></p>
<?php else : ?>
<table class="variations" cellspacing="0">
<tbody>
<?php foreach ( $attributes as $name => $options ) : ?>
<tr>
<td class="label"><label for="<?php echo sanitize_title( $name ); ?>"><?php echo wc_attribute_label( $name ); ?></label></td>
<?php
$sanitized_name = sanitize_title( $name );
if ( isset( $_REQUEST[ 'attribute_' . $sanitized_name ] ) ) {
$checked_value = $_REQUEST[ 'attribute_' . $sanitized_name ];
} elseif ( isset( $selected_attributes[ $sanitized_name ] ) ) {
$checked_value = $selected_attributes[ $sanitized_name ];
} else {
$checked_value = '';
}
?>
<td class="value">
<?php
if ( ! empty( $options ) ) {
if ( taxonomy_exists( $name ) ) {
// Get terms if this is a taxonomy - ordered. We need the names too.
$terms = wc_get_product_terms( $product->get_id(), $name, array( 'fields' => 'all' ) );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) {
continue;
}
print_attribute_radio( $checked_value, $term->slug, $term->name, $sanitized_name );
}
} else {
foreach ( $options as $option ) {
print_attribute_radio( $checked_value, $option, $option, $sanitized_name );
}
}
}
echo end( $attribute_keys ) === $name ? apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . __( 'Clear', 'woocommerce' ) . '</a>' ) : '';
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<div class="single_variation_wrap">
<?php
do_action( 'woocommerce_before_single_variation' );
do_action( 'woocommerce_single_variation' );
do_action( 'woocommerce_after_single_variation' );
?>
</div>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
<?php endif; ?>
<?php do_action( 'woocommerce_after_variations_form' ); ?>
</form>
<?php do_action( 'woocommerce_after_add_to_cart_form' ); ?>
<?php
/**
* Plugin Name: WC Variations Radio Buttons
* Plugin URI: https://wordpress.org/plugins/wc-variations-radio-buttons/
* Description: Variations Radio Buttons for WooCommerce. Let your customers choose product variations using radio buttons instead of dropdowns.
* Version: 2.0.0
* Author: 8manos
* Author URI: http://8manos.com
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// Check if WooCommerce is active
if ( is_plugin_active( 'woocommerce/woocommerce.php') ) {
class WC_Radio_Buttons {
// plugin version
const VERSION = '2.0.0';
private $plugin_path;
private $plugin_url;
public function __construct() {
add_filter( 'woocommerce_locate_template', array( $this, 'locate_template' ), 10, 3 );
//js scripts
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ), 999 );
}
public function get_plugin_path() {
if ( $this->plugin_path ) {
return $this->plugin_path;
}
return $this->plugin_path = plugin_dir_path( __FILE__ );
}
public function get_plugin_url() {
if ( $this->plugin_url ) {
return $this->plugin_url;
}
return $this->plugin_url = plugin_dir_url( __FILE__ );
}
public function locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) {
$template_path = $woocommerce->template_url;
}
$plugin_path = $this->get_plugin_path() . 'templates/';
// Look within passed path within the theme - this is priority
$template = locate_template( array(
$template_path . $template_name,
$template_name
) );
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) ) {
$template = $plugin_path . $template_name;
}
// Use default template
if ( ! $template ) {
$template = $_template;
}
return $template;
}
function load_scripts() {
wp_deregister_script( 'wc-add-to-cart-variation' );
wp_register_script( 'wc-add-to-cart-variation', $this->get_plugin_url() . 'assets/js/frontend/add-to-cart-variation.js', array( 'jquery', 'wp-util' ), self::VERSION );
}
}
new WC_Radio_Buttons();
}
\ No newline at end of file
...@@ -4,9 +4,17 @@ jQuery( document ).ready( function() { ...@@ -4,9 +4,17 @@ jQuery( document ).ready( function() {
var download_link = jQuery( '.download_file_link' ).attr('href'); var download_link = jQuery( '.download_file_link' ).attr('href');
var variation_id = jQuery( 'input[name="variation_id"]' ).val(); var variation_id = jQuery( 'input[name="variation_id"]' ).val();
var link = download_link+'?variation_id='+variation_id; var link = download_link+'?variation_id='+variation_id;
alert(link);
window.location.href= link; window.location.href= link;
return false; return false;
}); });
jQuery('.color-list').each(function (index, value) {
var color = jQuery(this).find('input').attr('data-color');
jQuery(this).find('input[type=radio]:after').css('background-color',color);
});
}); });
\ No newline at end of file
...@@ -335,6 +335,28 @@ function nap_variation_color($variationID) { ...@@ -335,6 +335,28 @@ function nap_variation_color($variationID) {
return $color; return $color;
} }
function nap_color_attribute($name){
$term = get_term_by('slug',$name, 'pa_couleur');
$termID = $term->term_id;
$color = get_field('couleurs', 'pa_couleur_' . $termID);
return $color;
}
function nap_icon_format($name){
$icons = array();
$term = get_term_by('slug',$name, 'pa_format');
$termID = $term->term_id;
$active = get_field('format_icone_active', 'pa_format_' . $termID);
$inactive = get_field('format_icone_inactive', 'pa_format_' . $termID);
$icons['active']=$active;
$icons['inactive']=$inactive;
return $icons;
}
function nap_distinct_color_variations($variations) { function nap_distinct_color_variations($variations) {
$var_colors = array(); $var_colors = array();
foreach ($variations as $variation) : foreach ($variations as $variation) :
...@@ -347,6 +369,30 @@ function nap_distinct_color_variations($variations) { ...@@ -347,6 +369,30 @@ function nap_distinct_color_variations($variations) {
return $var_colors; return $var_colors;
} }
function nap_variation_format($variationID) {
$variable_product = wc_get_product($variationID);
return $variable_product->get_attribute('format');
}
function nap_distinct_format_variations($variations) {
$var_format = array();
foreach ($variations as $variation) :
$variation_ID = $variation['variation_id'];
$format_name = nap_variation_format($variation_ID);
$term = get_term_by('name', $format_name, 'pa_format');
$termID = $term->term_id;
if (!in_array($termID, $var_format)):
$var_format[$termID] = $format_name;
endif;
endforeach;
// print_r($var_format);
return $var_format;
}
function nap_get_item($post_ID, $args = null, $current_page = '', $options = null) { function nap_get_item($post_ID, $args = null, $current_page = '', $options = null) {
$link = $image = $size = ''; $link = $image = $size = '';
if (isset($options)) { if (isset($options)) {
...@@ -391,3 +437,21 @@ function nap_get_item($post_ID, $args = null, $current_page = '', $options = nul ...@@ -391,3 +437,21 @@ function nap_get_item($post_ID, $args = null, $current_page = '', $options = nul
} }
remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20); remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
function nap_upload_image($img){
return trim(strtolower($img));
}
function nap_print_attribute_radio( $checked_value, $value, $label, $name ) {
$checked = sanitize_title( $checked_value ) === $checked_value ? checked( $checked_value, sanitize_title( $value ), false ) : checked( $checked_value, $value, false );
$input_name = 'attribute_' . esc_attr( $name ) ;
$esc_value = esc_attr( $value );
$id = esc_attr( $name . '_v_' . $value );
$filtered_label = apply_filters( 'woocommerce_variation_option_name', $label );
printf( '<div><input type="radio" name="%1$s" value="%2$s" id="%3$s" %4$s><label for="%3$s">%5$s</label></div>', $input_name, $esc_value, $id, $checked, $filtered_label );
}
\ No newline at end of file
...@@ -22,7 +22,7 @@ if ( ! defined( 'ABSPATH' ) ) { ...@@ -22,7 +22,7 @@ if ( ! defined( 'ABSPATH' ) ) {
global $product; global $product;
?> ?>
<div class="product_meta"> <!--<div class="product_meta">
<?php do_action( 'woocommerce_product_meta_start' ); ?> <?php do_action( 'woocommerce_product_meta_start' ); ?>
...@@ -38,4 +38,4 @@ global $product; ...@@ -38,4 +38,4 @@ global $product;
<?php do_action( 'woocommerce_product_meta_end' ); ?> <?php do_action( 'woocommerce_product_meta_end' ); ?>
</div> </div>-->
...@@ -30,7 +30,7 @@ $tabs = apply_filters( 'woocommerce_product_tabs', array() ); ...@@ -30,7 +30,7 @@ $tabs = apply_filters( 'woocommerce_product_tabs', array() );
if ( ! empty( $tabs ) ) : ?> if ( ! empty( $tabs ) ) : ?>
<div class="woocommerce-tabs wc-tabs-wrapper"> <!--<div class="woocommerce-tabs wc-tabs-wrapper">
<ul class="tabs wc-tabs" role="tablist"> <ul class="tabs wc-tabs" role="tablist">
<?php foreach ( $tabs as $key => $tab ) : ?> <?php foreach ( $tabs as $key => $tab ) : ?>
<li class="<?php echo esc_attr( $key ); ?>_tab" id="tab-title-<?php echo esc_attr( $key ); ?>" role="tab" aria-controls="tab-<?php echo esc_attr( $key ); ?>"> <li class="<?php echo esc_attr( $key ); ?>_tab" id="tab-title-<?php echo esc_attr( $key ); ?>" role="tab" aria-controls="tab-<?php echo esc_attr( $key ); ?>">
...@@ -43,6 +43,6 @@ if ( ! empty( $tabs ) ) : ?> ...@@ -43,6 +43,6 @@ if ( ! empty( $tabs ) ) : ?>
<?php call_user_func( $tab['callback'], $key, $tab ); ?> <?php call_user_func( $tab['callback'], $key, $tab ); ?>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>
</div> </div>-->
<?php endif; ?> <?php endif; ?>
...@@ -20,4 +20,4 @@ if ( ! defined( 'ABSPATH' ) ) { ...@@ -20,4 +20,4 @@ if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly. exit; // Exit if accessed directly.
} }
the_title( '<h1 class="product_title entry-title">', '</h1>' ); //the_title( '<h1 class="product_title entry-title">', '</h1>' );
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment