Skip to content

Instantly share code, notes, and snippets.

@hakikz
Created March 3, 2022 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakikz/2fc00988cff0f1a271193fa6fbb00f8b to your computer and use it in GitHub Desktop.
Save hakikz/2fc00988cff0f1a271193fa6fbb00f8b to your computer and use it in GitHub Desktop.
PVT Support for Second Image using Additional Variation Images Gallery
<?php
require_once( plugin_dir_path( __DIR__ ) . 'product-variant-table-for-woocommerce.php');
class PVTFW_COMMON{
/**
*
* On install selected columns
*
**/
function get_default_columns(){
$columns = array(
'image_link' => "on",
'image_link_additional' => "on",
'sku' => "on",
'variation_description' => "on",
'attributes' => "off",
'dimensions_html' => "off",
'weight_html' => "off",
'availability_html' => "on",
'price_html' => "on",
'quantity' => "on",
'action' => "on",
);
return apply_filters('pvtfw_columns_array', $columns);
}
/**
*
* Label name initialized
*
**/
function get_columns_labels(){
$default_column_lables = array(
'image_link' => __('Thumbnail', 'product-variant-table-for-woocommerce'),
'image_link_additional' => __('Second Thumbnail', 'product-variant-table-for-woocommerce'),
'sku' => __('SKU', 'product-variant-table-for-woocommerce'),
'variation_description' => __('Description', 'product-variant-table-for-woocommerce'),
'attributes' => __('Attributes', 'product-variant-table-for-woocommerce'),
'dimensions_html' => __('Dimensions', 'product-variant-table-for-woocommerce'),
'weight_html' => __('Weight', 'product-variant-table-for-woocommerce'),
'availability_html' => __('Stock', 'product-variant-table-for-woocommerce'),
'price_html' => __('Price', 'product-variant-table-for-woocommerce'),
'quantity' => __('Quantity', 'product-variant-table-for-woocommerce'),
'action' => __('Action', 'product-variant-table-for-woocommerce'),
);
$columns_labels_filter = apply_filters( 'pvtfw_columns_labels', $default_column_lables );
return $columns_labels_filter;
}
/**
*
* Table container class
*
**/
public static function container( $data ){
if( empty($data) ){
return;
}
else{
$classes = implode(' ', $data);
return $classes;
}
}
}
$pvtfw_common = new PVTFW_COMMON();
<?php
require_once(dirname(plugin_dir_path(__FILE__)) . '/class_pvtfw_common.php');
/**
* Initialization Start
*/
global $product, $pvtfw_common;
$product_id = $product->get_id();
$handle = new WC_Product_Variable($product->get_id());
$variations = $handle->get_children();
$default_columns = $pvtfw_common->get_default_columns();
$columns_labels = $pvtfw_common->get_columns_labels();
$columns = get_option('pvtfw_variant_table_columns', $default_columns);
/**
* Initialization End
*/
$options = apply_filters( 'pvtfw_process_data_option', array() );
$cart_url = home_url();
$product_url = get_permalink( $product->get_id() );
$product_id = $product->get_id();
$qty_layout = get_option('pvtfw_variant_table_qty_layout', 'plus/minus');
//Getting Cart Text
$btn_text = get_option('pvtfw_variant_table_cart_btn_text');
if(!$btn_text){
$text = __('Add To Cart', 'product-variant-table-for-woocommerce');
}
else{
$text = $btn_text;
}
foreach ($variations as $value) {
$single_variation = new WC_Product_Variation($value);
if( $single_variation->variation_is_visible() ):
$variant_id = $single_variation->get_id();
$stock_info = __('Out of Stock', 'product-variant-table-for-woocommerce');
// Checking Prodcut Stock
if(! $single_variation->is_in_stock()){
$btn_format = apply_filters( 'pvtfw_row_cart_btn_oos', sprintf('<button class="pvtfw_variant_table_cart_btn button alt" disabled>
<span class="pvtfw-btn-text">%s</span>
<div class="spinner-wrap"><span class="flaticon-spinner-of-dots"></span></div>
</button>', sprintf( _x( '%s', 'Button Text: Out of Stock', 'product-variant-table-for-woocommerce' ), $text )));
}
else{
$btn_format = apply_filters( 'pvtfw_row_cart_btn_is', sprintf('<button data-product-id="%s" data-url="%s" data-product="%s" data-variant="%s" class="pvtfw_variant_table_cart_btn button alt">
<span class="pvtfw-btn-text">%s</span>
<div class="spinner-wrap"><span class="flaticon-spinner-of-dots"></span></div>
</button>', $product_id, $cart_url, $product_url, $variant_id, sprintf( _x( '%s', 'Button Text: Add To Cart', 'product-variant-table-for-woocommerce' ), $text )
));
}
$qtyargs = array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $single_variation->get_min_purchase_quantity(), $single_variation ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $single_variation->get_max_purchase_quantity(), $single_variation ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $single_variation->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
'input_id' => 'pvtfw_variant_table_qty'.$variant_id,
);
$options['image_link'][] = "<figure class='item'><img class='pvtfw_variant_table_img_size' src='".wp_get_attachment_url( $single_variation->get_image_id() )."' /></figure>";
$options['image_link_additional'][] = wp_get_attachment_image( get_post_meta( $variant_id, 'woo_variation_gallery_images', true )[0], array('100', '100'), "", array( "class" => "pvtfw_variant_table_img_size" ) );
$options['sku'][] = $single_variation->get_sku();
$options['variation_description'][] = $single_variation->get_description();
$options['attributes'][] = $single_variation->get_variation_attributes(false) ;
$options['dimensions_html'][] = wc_format_dimensions($single_variation->get_dimensions(false));
$options['weight_html'][] = wc_format_weight($single_variation->get_weight(false));
$options['availability_html'][] = $single_variation->get_stock_quantity();
if($single_variation->is_on_sale()):
$options['price_html'][] = wc_price($single_variation->get_sale_price())." <del>". wc_price($single_variation->get_regular_price())."</del>"."<input type='hidden' name='hidden_price' class='hidden_price' value='".$single_variation->get_sale_price()."'>";
else:
$options['price_html'][] = wc_price($single_variation->get_regular_price())."<input type='hidden' name='hidden_price' class='hidden_price' value='".$single_variation->get_regular_price()."'>";
endif;
if("basic" == $qty_layout){
/**
* If stock quantity is empty then it displayed -2 for max value. Below condition is checking
* it is -1 value or not if -1 value max=""
* else the stock quantity will be displayed
*/
if($single_variation->get_max_purchase_quantity() != -1){
$options['quantity'][] = '<div class="pvtfw-quantity"><input id="pvtfw_variant_table_qty'.$variant_id.'" type="number" class="input-text qty text" step="1" min="'.$single_variation->get_min_purchase_quantity().'" max="'.$single_variation->get_max_purchase_quantity().'" name="quantity" value="1" title="Qty" size="2" placeholder="" inputmode="numeric"></div>';
}
else{
$options['quantity'][] = '<div class="pvtfw-quantity"><input id="pvtfw_variant_table_qty'.$variant_id.'" type="number" class="input-text qty text" step="1" min="'.$single_variation->get_min_purchase_quantity().'" max="" name="quantity" value="1" title="Qty" size="2" placeholder="" inputmode="numeric"></div>';
}
}
else{
// $options['quantity'][] = apply_filters( 'woocommerce_quantity_input_args', wp_parse_args( $args, $qtyargs ), $single_variation );
$options['quantity'][] = apply_filters( 'woocommerce_quantity_input_args', wp_parse_args( $qtyargs ), $single_variation );
}
$options['action'][] = $btn_format;
// $options['sub_total'][] = get_woocommerce_currency_symbol()."<span class='pvtfw_subtotal'></span>";
endif;
}
// Removing values if value is off in column array
foreach($columns as $key=>$value)
{
if(is_null($value) || $value == 'off')
unset($columns[$key]);
}
// Creating new column to show only on columns by user OR predefined as on
$latest = array();
foreach ($columns as $key => $value){
if(!is_null($value) || $value != 'off'){
$latest[$key] = $options[$key];
}
}
$mapped = [];
foreach ($latest as $key1 => $each_key_data ) {
foreach ($each_key_data as $key2 => $value2 ) {
switch ($key1) {
case 'image_link':
$key1_title = __('Thumbnail', 'product-variant-table-for-woocommerce');
$key1 = apply_filters( 'pvtfw_image_link_title', $key1_title );
break;
case 'image_link_additional':
$key1_title = __('Second Thumbnail', 'product-variant-table-for-woocommerce');
$key1 = apply_filters( 'pvtfw_image_link_additional_title', $key1_title );
break;
case 'sku':
$key1_title = __('SKU', 'product-variant-table-for-woocommerce');
$key1 = apply_filters( 'pvtfw_sku_title', $key1_title );
break;
case 'price_html':
$key1_title = __('Price', 'product-variant-table-for-woocommerce');
$key1 = apply_filters( 'pvtfw_price_html_title', $key1_title );
break;
case 'variation_description':
$key1_title = __('Description', 'product-variant-table-for-woocommerce');
$key1 = apply_filters( 'pvtfw_variation_description_title', $key1_title );
break;
case 'dimensions_html':
$key1_title = __('Dimensions', 'product-variant-table-for-woocommerce');
$key1 = apply_filters( 'pvtfw_dimensions_html_title', $key1_title );
break;
case 'weight_html':
$key1_title = __('Weight', 'product-variant-table-for-woocommerce');
$key1 = apply_filters( 'pvtfw_weight_html_title', $key1_title );
break;
case 'availability_html':
$key1_title = __('Stock', 'product-variant-table-for-woocommerce');
$key1 = apply_filters( 'pvtfw_availability_html_title', $key1_title );
break;
case 'quantity':
$key1_title = __('quantity', 'product-variant-table-for-woocommerce');
$key1 = apply_filters( 'pvtfw_quantity_title', $key1_title );
break;
case 'action':
$key1_title = __('Action', 'product-variant-table-for-woocommerce');
$key1 = apply_filters( 'pvtfw_action_title', $key1_title );
break;
default:
$key1 = $key1;
break;
}
$mapped[$key2][$key1] = $value2;
}
}
foreach ($mapped as $key1 => $values) {
echo "<tr>";
foreach ($values as $key2 => $value) {
if($key2 == "attributes"){
// print_r($value);
foreach($value as $key3 => $val){
$taxonomy_name = wc_attribute_label($key3);
// Getting attribute name (with full arrray) using get_term by passing slug
$term = get_term_by('slug', $val, $key3);
// If term is not empty then print attribute label else product page inputted vairation name
if(!empty($term)){
echo "<td data-title='{$taxonomy_name}'>{$term->name}</td>";
}
else{
echo "<td data-title='{$taxonomy_name}'>{$val}</td>";
}
}
}
elseif($key2 == __("quantity", "product-variant-table-for-woocommerce") && "plus/minus" == $qty_layout){
echo "<td data-title='{$key2}'>";
woocommerce_quantity_input($value);
echo "</td>";
}
else{
echo "<td data-title='{$key2}'>{$value}</td>";
}
}
/**
* ================================
* Subtotal code here
* ================================
*/
$showSubTotal = get_option('pvtfw_variant_table_sub_total', '');
if($showSubTotal != ''):
echo "<td>".get_woocommerce_currency_symbol()."<span class='pvtfw_subtotal'></span></td>";
endif;
echo "</tr>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment