Product image is not aligned with thumbnails, custom WooComerce Template - wordpress

I'm trying to fix an issue on this website but I can't seem to find the exact cause of it. The theme was made custom as well as the WooCommerce templates. However, when visiting a product page, the main image is not aligned with the thumbnails and I don't know how to fix it.
I managed to find the portion of template that seems to be responsible of it which is this
`<?php
global $product;
$images = array();
if ($product->get_type() == 'variable') {
$variations = $product->get_available_variations();
foreach ($variations as $key => $var) {
$variation = new WC_Product_Variation($var['variation_id']);
$main_image_id = array($variation->get_image_id());
$gallery_ids = get_post_meta( $variation->get_id(), '_wc_additional_variation_images', true );
$gallery_ids = str_replace(" ", "", $gallery_ids);
$gallery_ids = explode(",", $gallery_ids);
$gallery_image_ids = array();
foreach ($gallery_ids as $gallery_id) {
if ($gallery_id) {
$gallery_image_ids[] = $gallery_id;
}
}
$gallery_image_ids = array_merge($main_image_id, $gallery_image_ids);
$images[] = array(
'title' => $var['variation_description'],
'variation_id' => $var['variation_id'],
'gallery_ids' => $gallery_image_ids
);
}
}
$gallery_ids = $product->get_gallery_image_ids();
foreach ($gallery_ids as $index => $gallery_id) {
$images[] = array(
'title' => $product->get_title(),
'variation_id' => 'other-' . $index,
'gallery_ids' => $gallery_ids
);
}
?>`
Do you have any tips or suggestions on how to fix it?
https://staging-hiretail-staging.kinsta.cloud/product/armadio-zero-bifacciale
Thanks!
I treid to combine the official Woo template with the custom one and it seems like it works but no image appears.

Related

WordPress listing all/selected pages in A-Z index

I want to list all pages as well as selected pages in A-Z listing in WordPress. I know there are a number of plugins available, but I want this without a plugin.
Update
sorry if question is not clear, i want A-Z listing like attached image
i have solved this issue,
just need to put if condition, here is the code
$arr[0] = array(2=>2983);
$arr[1] = array(2=>2981);
$arr[2] = array('A'=>20);
$arr[3] = array('A'=>25);
print "<pre>";
print_r($arr);
$newArry = array();
foreach ($arr as $a) {
foreach ($a as $key => $value) {
if (array_key_exists($key, $newArry)) {
//$newArry[$key] = array($value);
array_push($newArry[$key], $value);
} else {
$newArry[$key] = array($value);
}
echo "<br/> Key ".$key ." => Value ".$value;
//print_r($b);
}
}
print_r($newArry);
You can use a WP_Query, as page is simply a post type.
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$pages = $query->posts;
See WP_Query documentation.

How to Populate a Drop-down field in WP

I have a gravity form form on my WP site and I recently changed a free text field into a drop down field.
The website is a store which hold several categories of goods and I want my drop-down to show the user all the possible categories he can choose from.
Please assist in how to "pull" the categories into the drop-down list.
Thanks in advance.
You can do using some filters of gravity form, code is following
// Here 1 is form id
add_filter( 'gform_pre_render_1', 'populate_category' );
add_filter( 'gform_pre_validation_1', 'populate_category' );
add_filter( 'gform_pre_submission_filter_1', 'populate_category' );
add_filter( 'gform_admin_pre_render_1', 'populate_category' );
function populate_category( $form ) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-category' ) === false ) {
continue;
}
// Get category list
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC'
) );
$choices = array();
foreach( $categories as $category ) {
$choices[] = array( 'text' => $category->name, 'value' => $category->name );
}
$field->placeholder = 'Select a Category';
$field->choices = $choices;
}
return $form;
}
This is working perfectly its tested code.

Insert some posts in a loop - wordpress plugin

I wrote a plugin, which reads csv files and create new products. The plugin works when I create only one product but when I add while in Insert() the plugin doesn't work. I want to create all products first off. Maybe it's something related to the add_action... Please help.
define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
function CreateProduct($line) {
$data = explode('";"', $line);
define(POST_NAME, $data[2]);
define(cena_netto_pw, $data[5]);
$post = get_page_by_title( POST_NAME, 'OBJECT', 'product' );
$product_ID = $post->ID;
$post_data = get_post($product_ID);
function hbt_create_post() {
$my_post = array(
'post_title' => POST_NAME,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' =>'product'
);
$product_ID = wp_insert_post( $my_post );
}
if(!isset($post))
hbt_create_post();
return $error_obj;
}
function Import() {
$file = PLUGIN_DIR.'test.csv';
$open = fopen($file, 'r');
while (!feof($open)) {
$line = fgets($open);
CreateProduct($line);
}
fclose($open);
}
add_action('admin_init', 'Import' );
?>
While loop code
while (!feof($open)) { $line = fgets($open); CreateProduct($line); }
This code doesn't work. It works when there is only
$line = fgets($open); CreateProduct($line);
try
fgetcsv($open)
instead
fgets($open)

Gettin atrribute thumbnail from node from a wordpress rss feed

I've been trying to get this seemingly easy peace of code to work.
I'm loading rss from a wordpress site and it all works fine except for the thumbnails. Since in the XML they are set as an attribute instead of a nodeValue i can't seem to get import them. (i've really tried a lot)
$rss = new DOMDocument();
$rss->load('http://goalprogramme.wordpress.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
// in XML it looks like <media:thumbnail url="http://goalprogramme.files.wordpress.com/2014/01/dsc_0227.jpg?w=150"/>
//echo $node->getElementsByTagName('media:thumbnail')->item(0)->getAttribute('url');
//push items
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'thumbnail' => $node->getElementsByTagName('media:thumbnail')->item(0)->getAttribute('url') // this line doesn't work !!!
);
array_push($feed, $item);
}
Any help would be greatly appreciated.
Thanks so much in advance!
Hours later i've created another piece of code that does work. If anyone needs it it, here it is:
$feed_array = array();
$feed = simplexml_load_file('http://goalprogramme.wordpress.com/feed/');
foreach ($feed->channel->item as $item) {
$title = (string) $item->title;
$description = (string) $item->description;
$link = (string) $item->link;
$date = (string) $item->date;
if ($media = $item->children('media', TRUE)) {
if ($media->thumbnail) {
$attributes = $media->thumbnail->attributes();
$thumbnail = (string)$attributes['url'];
}
}
$item = array (
'title' => $title ,
'desc' => $description,
'link' => $link,
'date' => $date,
'thumbnail' => $thumbnail
);
array_push($feed_array, $item);
}

Import multiple attributes with variations to woocommerce variable products

I am working with a script to import multiple attributes to a variable products.
I am getting attributes in the following format.
the code script is as follow
function products_options_values($a) {
$result = array(array());
foreach ($a as $k => $list) {
$_tmp = array();
foreach ($result as $result_item) {
foreach ($list as $list_item) {
$_tmp[] = array_merge($result_item, array($k => $list_item));
}
}
$result = $_tmp;
}
return $result;
} if($attributes = get_result("select * from products_attributes where products_id = X ")){
wp_set_object_terms($product_id, 'variable', 'product_type');
$attrib_array = array();
$attrib_combo = array();
$max_price = $product['products_price'];
$min_price = $product['products_price'];
foreach ($attributes as $attribute) {
$slug = sanitize_title($attribute['products_options_name']);
$attrib_array[$slug] = array(
'name' => $attribute['products_options_name'],
'value' => ltrim($attrib_array[$slug]['value'] . ' | ' . $attribute['products_options_values_name'], ' | '),
'position' => 0,
'is_visible' => 1,
'is_variation' => 1,
'is_taxonomy' => 0);
if($attribute['price_prefix'] == '-')$attributeprefix = '-';
else if($attribute['price_prefix'] == '+')$attributeprefix = '+';
else $attributeprefix = '';
$attrib_combo[$slug][] = array($attribute['products_options_values_name'], $attributeprefix . $attribute['options_values_price']);
}
$combos = products_options_values($attrib_combo);
foreach ($combos as $combo) {
$variation_id = wp_insert_post(array(
'post_title' => 'Product ' . $product_id . ' Variation',
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'product_variation',
'post_author' => 1,
'post_parent' => $product_id
));
$opt_price = $product['products_price'] .$attributeprefix. $attribute['options_values_price'];
$special_price = $special['specials_new_products_price'];
foreach ($combo as $k => $v) {
update_post_meta($variation_id, 'attribute_' . $k, $v[0]);
$opt_price += $v[1];
$special_price += $v[1];
}
update_post_meta($variation_id, '_regular_price', $opt_price);
update_post_meta($variation_id, '_price', $opt_price);
update_post_meta($variation_id, '_thumbnail_id', 0);
update_post_meta($variation_id, '_stock', $product['products_quantity']);
if ($opt_price > $max_price) {
$max_price = $opt_price;
}
if ($opt_price < $min_price) {
$min_price = $opt_price;
}
}
update_post_meta($product_id, '_product_attributes', $attrib_array);
update_post_meta($product_id, '_max_variation_regular_price', $max_price);
update_post_meta($product_id, '_min_variation_regular_price', $min_price);
update_post_meta($product_id, '_max_variation_price', $max_price);
update_post_meta($product_id, '_min_variation_price', $min_price);}
with this code i can create attributes and variations successfully
but when i see in the website and select a variable product and click add to cart button error display
Invalid value posted for
I am not understanding where I have error or missing some meta field to cover.
help me seniors if i am missing some meta fields or somethin code issues.
Just for giggles and kicks - in your config file - is your live site set to your home URL, or is it blank? you should make sure it will be empty. I have had some really weird things happen with SEF when that is not configured properly.
You also have a lot of script going on - especially on that front page. If the site is working without the SEF installed, and broken when it is turned on - more than likely a confict.
Just a side note - Joomla's core SEF works just fine. Don't bother with third party stuff - you really do not need it.

Resources