I am trying to pass the active product variation to a wordpress widget.
The widget code for displaying the image is currently:
$product = wc_get_product(get_the_ID());
$artwork_url = wp_get_attachment_url($product->get_image_id());
$unit = get_option('woocommerce_dimension_unit');
$height2 = $product->get_height();
$width2 = $product->get_width();
$height = $height2;
$title = get_the_title();
$price = $product->get_price();
$measurements_display = $widget->dimensions == 1 ? 'WxH' : 'HxW';
The $artwork_url is what I am having problems with. When I visit the site, the variation options successfully display the appropriate variation image. I would like to pass that value onto the widget with the $artwork_url expression. I've tried pulling thumbnails, feature images, wo-post-images, and am not getting anywhere.
If anyone could offer a suggestion to this, i'd greatly appreciate any advice.
Thanks
Cameron
I've tried defining $variations and $post-image, $variable_product, but can't seem to get the language right.
To get the active variation image URL in the widget, you can try the following code:
$product = wc_get_product(get_the_ID());
$variation_id = isset($_REQUEST['variation_id']) ? $_REQUEST['variation_id'] : '';
if ($variation_id) {
$variation = new WC_Product_Variation($variation_id);
$artwork_url = wp_get_attachment_url($variation->get_image_id());
} else {
$artwork_url = wp_get_attachment_url($product->get_image_id());
}
$unit = get_option('woocommerce_dimension_unit');
$height2 = $product->get_height();
$width2 = $product->get_width();
$height = $height2;
$title = get_the_title();
$price = $product->get_price();
$measurements_display = $widget->dimensions == 1 ? 'WxH' : 'HxW';
This code first checks if a variation_id is present in the request parameters. If it is, it creates a new WC_Product_Variation object with that ID and gets the variation's image URL. If not, it gets the parent product's image URL as before.
Related
i have a loop which retrieves correctly post data, (title, etc) and works fine with ACF fields ONLY if the viewer is the post author, it is not working IF the viewer is someone else... :(
public static function getFields() {
$postID = get_the_ID();
$fields = [];
$array_anagrafica = [];
if( have_rows('anagrafica', $postID) ) { // <<- it's not working even with no $postID
while (have_rows('anagrafica', $postID)) {
the_row();
$array_anagrafica['cf'] = get_sub_field('codice_fiscale');
$array_anagrafica['piva'] = get_sub_field('partita_iva');
any hint?
it is a matter of acf_form -> using acf_form() toghether with acf_form_head() replicates backend form and can then be properly initated.
I'm trying to load product images on list page. Here is my code:
$_productCollection = $block->getLoadedProductCollection();
foreach ($_productCollection as $_product){
$images = $product->getMediaGalleryImages();
The $_product variable is fine. The variable $images is always null.
I was able to solve this with following code:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_productCollection = $block->getLoadedProductCollection();
foreach ($_productCollection as $_product){
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($id);
$images = $product->getMediaGalleryImages();
}
You need to use the addMediaGalleryData method.
See this answer on magento.stackexchange.com for more details.
I am trying to create a custom Wordpress plugin which utilises shortcodes to output what I want. In this text code I am attempting to read an rss file and spit out just a list of the top 5 feeds.
The $showno is one of the shortcode variables so I can use the following [player show=foo snowno=5]. In the example code $show isn't used.
The code below only shows the most recent item in the feed list. If I change the return to echo then it works as expected except it show at the top of the post instead of where I've entered the shortcode. I searched for an answer to this and the solution offered was simply "use return" which I've done...
Appreciate advice. Thanks
include_once(ABSPATH . WPINC . '/rss.php');
$num_items = $showno;
$feedurl = 'http://feeds.bbci.co.uk/news/rss.xml';
$feed = fetch_rss($feedurl);
$items = array_slice ($feed->items, 0, $num_items);
foreach ($items as $item ) {
$title = $item[title];
$mp3link = $item[link];
$description = $item[description];
return "<li>$title - $description</li>";
}
Shortcodes should be returned not echoed.
In your code, you are breaking the execution of the foreach and returning the first value.
You should build a string variable and after the foreach loop return it, so all your loop will be included, e.g.:
$final_html = '';
foreach( $items as $item )
{
$final_html .= "<li>$title - $description</li>";
}
return $final_html;
i am currently using contact form 7 for wordpress. i would like a unique id for every form filled in and sent.
i have had a look around the internet but cant find anything, although i did find the following:
http://contactform7.com/special-mail-tags/
would there be any easy way to make my own function to do something similar to the above tags? i would need it to be a function to go into my themes function file, so that plugin updates wont affect it.
Cheers Dan
[_serial_number] field is an auto-incremented form submission ID. You just have to have Flamingo plugin installed as well. See http://contactform7.com/special-mail-tags/
(It's the same link as in the question, I guess the field wasn't there when the question was asked).
To generate ID for the Contactform7 you need to hook the 'wpcf7_posted_data'.
However, to generate incremental ID you need to save the forms in database so you can retrieve which ID should be next on next Form submit. For this you will need CFDB plugin (https://cfdbplugin.com/).
If you dont want to put the code inside theme functions.php file, you can use this plugin instead: https://wordpress.org/plugins/add-actions-and-filters/
Example code:
function pk_generate_ID($formName, $fieldName) {
//Retrieve highest ID from all records for given form stored by CFDB, increment by 1 and return.
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
$start = '001';
$exp = new CFDBFormIterator();
$atts = array();
$atts['show'] = $fieldName;
$atts['filter'] = "$fieldName>=$start&&$fieldName<999999999999"; //is_numeric() is not permitted by default for CFDB filter
$atts['limit'] = '1';
$atts['orderby'] = "$fieldName DESC";
$atts['unbuffered'] = 'true';
$exp->export($formName, $atts);
$found = $start;
while ($row = $exp->nextRow()) {
$row2 = $row[$fieldName];
$row2 += 1;
$found = max($found,$row2);
}
return $found;
}
function pk_modify_data( $posted_data){
$formName = 'Form1'; // change this to your form's name
$fieldName = 'ID-1'; // change this to your field ID name
//Get title of the form from private property.
$cf_sub = (array) WPCF7_Submission::get_instance();
$cf = (array) $cf_sub["\0WPCF7_Submission\0contact_form"];
$title = (string) $cf["\0WPCF7_ContactForm\0title"];
if ( $posted_data && $title == $formName) ) { //formName match
$posted_data[$fieldName] = pk_generate_ID($formName, $fieldName);
}
return $posted_data;
}
add_filter('wpcf7_posted_data', 'pk_modify_data');
I just want to create plugin that will when visitor(user,visitor,...) visit some post,remember what post,and to increment counter of that post,I wrote this code,but sometimes,counter is incremented,even post isn't viewed,or post with other Id is added to a table.Can someone help me with this,please.I know that there are plugins for this that I'm trying to do,but still want to write this plugin.
function IncrementPostCount($the_content) {
global $post;
global $wpdb;
if(($post->post_status == 'publish') && (int)$post->ID) {
if(is_single()) { // just for single post - not for page
$postID = (int)$post->ID;
$postTitle = urlencode($post->post_title);
$postLink = urlencode(get_permalink($post->ID));
$oneRow = $wpdb->get_row("SELECT * FROM wp_postovi WHERE postAjDi='$postID'");
if(empty ($oneRow)) {
$postCounter = 1;
$data_array = array(
'readnTimes' => $postCounter,
'linkPost'=>$postLink,
'TitlePost'=>$postTitle,
'postAjDi'=>$postID);
$wpdb->insert('wp_najcitaniji_postovi', $data_array);
}
else {
$postCounter = intval($oneRow->readnTimes) + 1;
$data_array = array('readnTimes' => $postCounter);
$where_array = array('postAjDi'=>intval($oneRow->postAjDi));
$wpdb->update('wp_postovi',$data_array,$where_array);
}
return $the_content;
}
return $the_content;
}
}
add_filter('the_content','IncrementPostCount');
Sorry on my bad english,tnx in advance.
Here's how to do that with the postmeta table.
function IncrementPostCount(){
if(is_single()){
global $wp_query;
$count = get_post_meta( $wp_query->post->ID, 'readnTimes', true );
$count = empty($count) ? 1 : $count + 1;
add_post_meta($wp_query->post->ID, 'readnTimes', $count, true) or update_post_meta($wp_query->post->ID, 'readnTimes', $count);
}
}
add_action( 'template_redirect', 'IncrementPostCount' );
Also, it's better to hook it in earlier. That way, the count is only incremented once per page load (the_content can be fired multiple times per page, even on a single page. template_redirect only fires once per request). Also, if you store the data at template_redirect, you can use the updated view count in the template, giving your visitors an even more accurate view count.
And you don't need to worry about database tables, custom SQL, or any of that.