Skip to content

Instantly share code, notes, and snippets.

@younes-dro
Last active October 9, 2019 15:41
Show Gist options
  • Save younes-dro/97bb6a0a4f8f8f225e129f6b8448870b to your computer and use it in GitHub Desktop.
Save younes-dro/97bb6a0a4f8f8f225e129f6b8448870b to your computer and use it in GitHub Desktop.
Display vairation products as a table
add_action('plugins_loaded', function(){
function woocommerce_variable_add_to_cart() {
global $product, $post;
// print_r($product->product_type);
$variations = $product->get_available_variations();
// ob_start(); // Start buffering
?>
<form class="cart" action="<?php echo esc_url($product->add_to_cart_url()); ?>" method="post" enctype="multipart/form-data">
<table class="dro-variable-product" id="dro-variable-product">
<tbody>
<?php
foreach ($variations as $key => $value) {
?>
<tr>
<td width="65%">
<h2 class="product_title_variation"><?php echo implode('/', $value['attributes']); ?></h2>
<div class="description_variation"><?php echo $value['variation_description']; ?></div>
<div class="custom-field-container">
<?php
$product = wc_get_product($value['variation_id']);
// Check for the custom field value
$entier = $product->get_meta('_entier');
$sans_tete = $product->get_meta('_sans_tete');
$sans_ecaille = $product->get_meta('_sans_ecaille');
$deux = $product->get_meta('_deux');
$sans_queue = $product->get_meta('_sans_queue');
$sans_peau = $product->get_meta('_sans_peau');
$avec_peau = $product->get_meta('_avec_peau');
$coupe_deux = $product->get_meta('_coupe_deux');
$cuisson = $product->get_meta('_cuisson');
if ($entier) {
// Only display our field if we've got a value for the field title
echo '<div class="cfwc-custom-field-wrapper">'
. '<label for="cfwc-title-field"></label><input value="yes" type="checkbox" id="_entier" name="_entier[' . $value['variation_id'] . ']">'
. 'Entier'
. '<i id="entier" class="infos entier fa fa-info-circle"></i>'
. '</div>';
}
if ($sans_tete) {
// Only display our field if we've got a value for the field title
echo '<div class="cfwc-custom-field-wrapper">'
. '<label for="cfwc-title-field"></label><input value="yes" type="checkbox" id="_sans_tete" name="_sans_tete[' . $value['variation_id'] . ']">'
. 'Sans tête'
. '<i id="sans-tete" class="infos sans-tete fa fa-info-circle"></i>'
. '</div>';
}
if ($sans_ecaille) {
// Only display our field if we've got a value for the field title
echo '<div class="cfwc-custom-field-wrapper">'
. '<label for="cfwc-title-field"></label><input value="yes" type="checkbox" id="_sans_ecaille" name="_sans_ecaille[' . $value['variation_id'] . ']">'
. 'Sans écaille'
. '<i id="sans-ecaille" class="infos sans-ecaille fa fa-info-circle"></i>'
. '</div>';
}
if ($deux) {
// Only display our field if we've got a value for the field title
echo '<div class="cfwc-custom-field-wrapper">'
. '<label for="cfwc-title-field"></label><input value="yes" type="checkbox" id="_deux" name="_deux[' . $value['variation_id'] . ']">'
. 'Les deux'
. '<i id="deux" class="infos deux fa fa-info-circle"></i>'
. '</div>';
}
if ($sans_queue) {
// Only display our field if we've got a value for the field title
echo '<div class="cfwc-custom-field-wrapper">'
. '<label for="cfwc-title-field"></label><input value="yes" type="checkbox" id="_sans_queue" name="_sans_queue[' . $value['variation_id'] . ']">'
. 'Sans queue'
. '<i id="sans-queue" class="infos deux fa fa-info-circle"></i>'
. '</div>';
}
if ($sans_peau) {
// Only display our field if we've got a value for the field title
echo '<div class="cfwc-custom-field-wrapper">'
. '<label for="cfwc-title-field"></label><input value="yes" type="checkbox" id="_sans_peau" name="_sans_peau[' . $value['variation_id'] . ']">'
. 'Sans peau'
. '<i id="sans-peau" class="infos deux fa fa-info-circle"></i>'
. '</div>';
}
if ($avec_peau) {
// Only display our field if we've got a value for the field title
echo '<div class="cfwc-custom-field-wrapper">'
. '<label for="cfwc-title-field"></label><input value="yes" type="checkbox" id="_avec_peau" name="_avec_peau[' . $value['variation_id'] . ']">'
. 'Avec peau'
. '<i id="avec-peau" class="infos deux fa fa-info-circle"></i>'
. '</div>';
}
if ($coupe_deux) {
// Only display our field if we've got a value for the field title
echo '<div class="cfwc-custom-field-wrapper">'
. '<label for="cfwc-title-field"></label><input value="yes" type="checkbox" id="_coupe_deux" name="_coupe_deux[' . $value['variation_id'] . ']">'
. 'Coupé en deux'
. '<i id="coupe-deux" class="infos deux fa fa-info-circle"></i>'
. '</div>';
}
if ($cuisson) {
// Only display our field if we've got a value for the field title
echo '<div class="cfwc-custom-field-wrapper">'
. '<label for="cfwc-title-field"></label><input value="yes" type="checkbox" id="_cuisson" name="_cuisson[' . $value['variation_id'] . ']">'
. 'Pour cuisson en croûte de sel'
. '<i id="cuisson" class="infos deux fa fa-info-circle"></i>'
. '</div>';
}
?>
</div>
</td>
<td align="center">
<?php echo $value['price_html']; ?>
<?php
$black_color = ($value['display_regular_price'] === $value['display_price']) ? "black" : "";
?>
<?php // if(($product->get_meta('_prix_kilo')) || $product->get_meta('_prix_kilo')){?>
<span class="price-variation-kilo <?php echo $black_color ?>">
<?php
if ($product->get_meta('_prix_kilo')) {
echo $product->get_meta('_prix_kilo') . ' €/kg';
}
?>
</span>
<input type="hidden" class="display_regular_price" value="<?php print_r($value['display_regular_price']) ?>" />
<input type="hidden" class="display_price" value="<?php print_r($value['display_price']) ?>" />
<input type="hidden" name="variation_id[]" value="<?php echo $value['variation_id'] ?>" />
<input type="hidden" name="product_id[]" value="<?php echo esc_attr($post->ID); ?>" />
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr($post->ID); ?>" />
<?php
if (!empty($value['attributes'])) {
foreach ($value['attributes'] as $attr_key => $attr_value) {
?>
<input type="hidden" name="<?php echo $attr_key ?>" value="<?php echo $attr_value ?>">
<?php
}
}
woocommerce_quantity_input([ 'input_name' => 'quantity[]', 'min_value' => 0, 'input_value' => '0', 'max_value' => $product->backorders_allowed() ? '' : $product->get_stock_quantity()]);
?>
<?php // do_action( 'woocommerce_before_add_to_cart_button' ); ?>
</td>
</tr>
<?php
}
?>
<tr><td class="total-container" colspan="3">
<input type="hidden" id="input-total-variable-products" value="0" />
<span id="total-variable-products"></span><?php //echo get_woocommerce_currency_symbol() ?>
<?php
$_main_prix_kilo = get_post_meta($post->ID, '_main_prix_kilo', '_main_prix_kilo', true);
// var_dump($_main_prix_kilo);
$_promo_prix_kilo = get_post_meta($post->ID, '_promo_prix_kilo', '_promo_prix_kilo', true);
// var_dump($_promo_prix_kilo);
?>
<?php if (($_main_prix_kilo) || ($_promo_prix_kilo)): ?>
<?php $black_color = ($_promo_prix_kilo) ? "" :"black"?>
<span class="price">
<?php if ($_main_prix_kilo): ?>
<ins>
<span class="woocommerce-Price-amount amount<?php echo $black_color?>"><?php echo $_main_prix_kilo; ?> €/kg</span>
</ins>
<?php endif; ?>
<?php if ($_promo_prix_kilo): ?>
<del>
<span class="woocommerce-Price-amount amount"><?php echo $_promo_prix_kilo; ?> €/kg</span>
</del>
<?php endif; ?>
</span>
<?php endif; ?>
</td></tr>
<tr><td class="add-cart-" colspan="3"><button type="submit" class="dro-single_add_to_cart_button button alt"><?php echo apply_filters('single_add_to_cart_text', __('Add to cart', 'woocommerce'), $product->product_type); ?></button></td></tr>
<tr><td style="border: none" class="contacter-nous" colspan="3"><?php echo '<a href="'. get_bloginfo('url').'/index.php/contactez-nous/" >Connectez-vous pour connaître le 1er jour de livraison disponible</a>';?></td></tr>
</tbody>
</table>
<input type="hidden" name="action" value="woocommerce_ajax_add_to_cart" />
</form>
<div class="popup-infos">
<div class="popup-content">
<div class="content">
<h1 class="header-popup">INFORMATIONS SUR LES MODES DE DÉCOUPE <span class="close-popup"><i class="fa fa-close"></i></span></h1>
<div class="popup_content">
</div>
</div>
</div>
</div>
<?php
//return;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment