Woocommerce get product Size in Email Template - wordpress

I am creating a email template and I am trying to get the size of the product. I have tried the following, but it retrieves all the sizes:
echo nl2br( $_product->get_attribute( 'size' ) );
The code above did work for color. Any ideas?

Your question is not clear about the problem. You should add few more
details, "are you trying to do outside of woocommerce loop?" and "are
you getting any error message?"
Anyway, you can get any custom attributes value by following code:
$product->get_attribute( 'your_attr' );
If your products custom attribute name is size, you can use as following:
global $product;
echo nl2br( $product->get_attribute( 'size' ) );
It should work. Thank you.

Related

WordPress shortcode to display message based on value used by plugin

I'm hoping someone can clue me in on how I can extract a plugin value and display it on my page using a short code. Specifically, when a user reaches the max allowable limit, I would like a notification message to appear on the page (in addition to its placement in a popup) Many tests and I have not been able to get this to work. MANY thanks in advance.
public function add_error_limit_message() {
if ( ! $this->limit_reached ) {
return;
}
$message = apply_filters( 'woocompare_limit_reached_message', __( 'You have reached the maximum number of products for compare table.', 'woocommerce-compare' ) );
echo '<div class="woocompare-error"><p>' . wp_kses_post( $message ) . '</p></div>';
}
add_shortcode( 'limit-reached-message', 'add_error_limit_message' );
Your shortcode function should return the text string you want to insert in place of the shortcode, not echo it.
To track down "critical errors" (php errors) turn on WP_DEBUG and WP_DEBUG_DISPLAY in your wp-config.php file. And try installing and activating the Query Monitor plugin. These will tell you enough about your php errors to track them down and fix them.

How to get a Product Variation Custom Field to work along other plugins?

I get this code (see below) to create a Product Variation Custom Field. It works just fine. Now I need to use the WP All Import Pro plugin to import Products from a csv file. This plugin allow to create the importation task by mapping the information in the csv to the fields of Product and Product Variation.
Problem: thing is that the new created Product Variation Custom Field is not visible for the WP All Import plugin. I mean, this new field is not listed when I do the mapping of the data be imported.
My assumption is that this code fails to create some data in the database to make this field available for other module.
How can I get this to work properly
I also attached a capture of the WP All Import page where the field should be visible.
/*******************************
add custom fields to product variations
*********************************/
// regular variable products
add_action( 'woocommerce_product_after_variable_attributes', 'add_to_variations_metabox', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'save_product_variation', 20, 2 );
/*
* Add new inputs to each variation
*
* #param string $loop
* #param array $variation_data
* #return print HTML
*/
function add_to_variations_metabox( $loop, $variation_data, $variation ){
$custom = get_post_meta( $variation->ID, '_custom', true ); ?>
<div class="variable_custom_field">
<p class="form-row form-row-first">
<label><?php echo __( 'MY CUSTOM FIELD:', 'plugin_textdomain' ); ?></label>
<input type="text" size="5" name="variation_custom_data[<?php echo $loop; ?>]" value="<?php echo esc_attr( $custom ); ?>" />
</p>
</div>
<?php
}
/*
* Save extra meta info for variable products
*
* #param int $variation_id
* #param int $i
* return void
*/
function save_product_variation( $variation_id, $i ){
// save custom data
if ( isset( $_POST['variation_custom_data'][$i] ) ) {
// sanitize data in way that makes sense for your data type
$custom_data = ( trim( $_POST['variation_custom_data'][$i] ) === '' ) ? '' : sanitize_title( $_POST['variation_custom_data'][$i] );
update_post_meta( $variation_id, '_custom', $custom_data );
}
}
WP ALL IMPORT has this section coded into their files. They have not provide any hook or filter to add fields to this section.
The most important thing here is that WP ALL IMPORT has thought is like this :
As all the Meta for a particular variation is added finally to the post meta table. Which will be retrieved as by get_post_meta. Just like the custom fields. So in order to attach meta to the variation, you can just another Custom Fields with the Name as the name which the actual meta is stored with and with the value you want the variation to be. Just check the image attached. Refer - http://www.wpallimport.com/documentation/custom-fields/theme-plugin-fields/
I know this question was asked almost a year ago but I have been trying to figure out this exact issue out for quite some time now. But in the end I managed to find a workaround that at least will allow you to get that data into the variation instead of the main parent product:
1) In the WpAllImport Wordpress Addon you will be able to see a "variations" tab as shown in your screenshot. Click on this.
2) On this page you will once again not be able to see any custom fields you have made for variations HOWEVER there is a product attributes section.
3) If you add your variation as an attribute and uncheck "Show in variations" and "Taxonomy" and "Is Visible" then you can store your data in an attribute.
4) After the import you should be able to see you data in the attribute, if you know some programming you can even write a script to then loop through all variations and copy this data over to a custom field.
I know it's not much, but for my purposes I was at least able to save the data in the variation which I then used late in a product export plugin.
Hope that helps!
Though this question was initially asked 2 years ago. I found a solution in 2020. I hope this will help anyone who's still looking for a solution.
Please go to All Products and at the top where it says Add New Product you should see two more buttons "Import" and "Export". Click on Export and you will see an option of checkbox "Yes, export all custom meta" Please check this potion and all your custom fields even in variations will be exported.
Thanks

Create custom layout product list woocommerce

I want to make a list of products woocommerce as warungssl..com shown are: the product name, normal price, discount prices, some attributes, link or button that leads to a product detail page, and a link or button cart.
In Which file should I change the script code and I do not understand how to display multiple attributes.
Note:
1. To (issuance, validation, and site seal) I will call the attribute
data. The obstacles I did not understand the script.
2. I make use of Wordpress themes porto
3. The following examples are meant attributes
Maybe it could let me know where the location of the files that I have to change.
please go to here : wp-content\plugins\woocommerce\templates\archive-product.php
or
please go to here : wp-content\themes\porto\woocommerce\archive-product.php
and put following code:
<?php
global $product;
$greenbar = $product->get_attribute( 'greenbar' );
$issuance = $product->get_attribute( 'issuance' );
$mobile_friendly = $product->get_attribute( 'mobile_friendly' );
$san_support = $product->get_attribute( 'san_support' );
$site_seal = $product->get_attribute( 'site_seal' );
$validation = $product->get_attribute( 'validation' );
$warranty = $product->get_attribute( 'warranty' );
?>

Visual Composer Grid with do_action shortcode is not working

I have visual composer which is packed with total theme. When I put the following grid short code in my page in the editor it works correctly.
[vc_basic_grid post_type="post_type" max_items="10" item="masonryGrid_SlideFromLeft" grid_id="vc_gid:1458178666639-80ebf3775500c87d35de078c3422fe96-10" taxonomies="555"]
However, when I call the exact same code using do_action it gives the following javascript error. I checked the html output and it is the same using do_action like putting the short code in editor.
Error: Syntax error, unrecognized expression: {'status':'Nothing found'}
s
Any help is greatly appreciated.
Well, you can't output contents directly in your templates by using core shortcodes of VC like that.
1. Problem:
For security, besides nonce, VC uses page_id and shortcode_id to check AJAX request/respond data.
The shortcode_id is automatically generated by VC, you can not harcode it.
For example, this is the shortcode you see on admin editor screen:
[vc_basic_grid post_type="post_type" max_items="10" item="masonryGrid_SlideFromLeft" grid_id="vc_gid:1458178666639-80ebf3775500c87d35de078c3422fe96-10" taxonomies="555"]
Let say the page ID is 4269, this is the generated HTML code on front-end:
<!-- vc_grid start -->
<div class="vc_grid-container-wrapper vc_clearfix">
<div class="vc_grid-container vc_clearfix wpb_content_element vc_masonry_grid" data-initial-loading-animation="zoomIn" data-vc-grid-settings="{"page_id":4269,"style":"all-masonry","action":"vc_get_vc_grid_data","shortcode_id":"1458178666639-80ebf3775500c87d35de078c3422fe96-10","tag":"vc_masonry_grid"}" data-vc-request="http://example.com/wp-admin/admin-ajax.php" data-vc-post-id="4269" data-vc-public-nonce="0641473b09">
</div>
</div>
<!-- vc_grid end -->
Now, if page_id and shortcode_id don't match each other, {'status':'Nothing found - $shorcode_id'} will be throw out and no contents will be displayed.
You can find out more inside vc_grid.min.js file.
2. Solution:
Generate a fake page with VC, then copy generated html code to your template file.
Create a template with VC directly.
Use Shorcode Mapper to create your own shorcode.
First you build a new page and add a grid post on it,
then we get
_vc_post_settings
post meta , and try to build a new one
then update post meta data
now we can by pass VC Ajax security check
in the following code "1513628284966-37b8c3ca-d8ec-1" is VC generated guid
you should change it to yours .
$meta = get_post_meta(1365,'_vc_post_settings');
$settings = array();
#$settings['vc_grid_id'] = $meta[0]['vc_grid_id'];
$key = random_int(1513628284966,9513628284966);
$settings['vc_grid_id']['shortcodes'][''.$key.'-37b8c3ca-d8ec-1'] = $meta[0]['vc_grid_id']['shortcodes']['1513628284966-37b8c3ca-d8ec-1'];
$settings['vc_grid_id']['shortcodes'][''.$key.'-37b8c3ca-d8ec-1']['atts']['custom_query'] = "tag=shop";
$settings['vc_grid_id']['shortcodes'][''.$key.'-37b8c3ca-d8ec-1']['atts']['grid_id'] = ''.$key.'-37b8c3ca-d8ec-1';
$n = add_post_meta(1365,'_vc_post_settings',$settings);
return do_shortcode("[vc_basic_grid post_type=\"custom\" show_filter=\"yes\" filter_style=\"dropdown\" item=\"5959\" grid_id=\"vc_gid:".$key."-37b8c3ca-d8ec-1\" filter_source=\"post_tag\" custom_query='tag=".$tag."']");
I've solved.
I had the same problems, with the Visual Composer Editor offered by WpBakery
https://wpbakery.com/
and after understanding the connection between the IDs of the block, and the ID of the Post, I put more attention to the settings of the Block.
There is infact one field called "Element ID", and here we have to put our ID of the Post we are editing.
In my case the Block was a block containing some Posts.
After saving, and viewing the page without the Editor, I was finally able to see the block, and not the message
{"status":"Nothing found"}
I found a solution to this problem.
I modified the woocommerce category template and linked to the woocommerce_archive_description hook to add additional descriptions from some pages, for this I got their id, and then display the content.
echo do_shortcode($post->post_content);
The gallery(media grid) didn't work because there was a mismatch between the page id and the shortcode id. Therefore, the logical solution was to redefine the global variable $post to the $post of the page from which I get the content.
global $post;
$post = get_post( $id );
And it turns out that the post id matches.
After that, don't forget to return the normal $post value;
wp_reset_postdata();
By the way - use this option to load custom styles for wpbakery elements.
echo '<style type="text/css" data-type="vc_shortcodes-custom-css">' . get_post_meta( $id, '_wpb_shortcodes_custom_css', true ) . '</style>';
The whole code
function extra_product_category_desc(){
if( is_product_category() ){
$id = get_term_meta (get_queried_object()->term_id, 'pageId', true);
if($id !== ''){
global $post;
$post = get_post( $id );
echo do_shortcode($post->post_content);
echo '<style type="text/css" data-type="vc_shortcodes-custom-css">' . get_post_meta( $id, '_wpb_shortcodes_custom_css', true ) . '</style>';
wp_reset_postdata();
}
}
}
add_action( 'woocommerce_archive_description', 'extra_product_category_desc', 11 );
You may also try with do_shortcode('');
Like
do_shortcode('[vc_basic_grid post_type="post_type" max_items="10" item="masonryGrid_SlideFromLeft" grid_id="vc_gid:1458178666639-80ebf3775500c87d35de078c3422fe96-10" taxonomies="555"]');
Best Regards,

Wordpress shortcode linked to another DB

First thing first, this is my first shortcode attempt.
It is worthed mention that I'm also using woocommerce on my website.
Let's start:
I know that to add shortcodes into wordpress, you need to write something similar to the code below into the functions.php file: (this is just an example)
function myshortcode() {
return "This is a shortcode example!";
}
add_shortcode( 'mycode', 'myshortcode' );
and if i add [mycode] into the wordpress page editor, the preview shows my text correctly.
But what if i need to use a variable (in my case woocommerce order number) in the shortcode?
Let's say i need to compare woocommerce_order_number with my_custom_uid (inserted into another database, outside wordpress).
I usually use a db request like the one below, and usually it works fine (as before, this is only an example):
select 'my_custom_uid' from 'my_custom_database' where 'woocommerce_order_number' = '1234'
The problem is that i don't know the woocommerce_order_number (it changes everytime!), because this shortcode needs to go inside an html email body i need to send out to customers after they placed the order.
How can i get the customer woocommerce order (variable that changes everytime) so that i will be able to use it into my shortcode to link it to my custom_uid?
If the question is not clear enough, please feel free to ask for clarification!
thanks a lot
I don't see a reason to use a shortcode. If you want to add something to an email, you should use one of the hooks in the email. For example:
function kia_display_email_order_meta( $order, $sent_to_admin, $plain_text ) {
$some_field = get_post_meta( $order->id, '_some_field', true ),
$another_field = get_post_meta( $order->id, '_another_field', true ),
if( $plain_text ){
echo 'The value for some field is ' . $some_field . ' while the value of another field is ' . $another_field;
} else {
echo '<p>The value for <strong>some field</strong> is ' . $some_field. ' while the value of <strong>another field</strong> is ' . $another_field . '</p>';
}
}
add_action('woocommerce_email_customer_details', 'kia_display_email_order_meta', 30, 3 );
Note that the $order object is the first parameter available to the kia_display_email_order_meta function. So you'd be able to get the ID via $order->id. This should add the data after the customer address details, but there are other hooks available if woocommerce_email_customer_details isn't appropriate.
I finally managed to solve this, and here's what i did if someone is interested:
<?PHP
add_action('fue_before_variable_replacements', 'register_variable_replacements', 11, 4);
add_action('fue_email_variables_list', 'email_variables_list');
/**
* This gets called to replace the variable in the email with an actual value
* #param $var - Modify this: array key is the variable name, value is the replacement
*/
function register_variable_replacements($var, $email_data, $queue_item, $email){
global $wpdb;
// Look up UID from order number
$orderNumber = $var->get_variables()['order_number'];
$sql = " //Your sql statement here...//";
$results = $wpdb->get_results($sql);
$UID = $results[0]->meta_value;
$variables = array(
'uid' => $UID
);
$var->register($variables);
}
function email_variables_list($email)
{
global $woocommerce;
?>
<li class="var hideable var_subscriptions">
<strong>{uid}</strong>
<img class="help_tip" title="<?php _e('Order UID', 'follow_up_emails'); ?>" src="<?php echo $woocommerce->plugin_url(); ?>/assets/images/help.png" width="16" height="16"/>
</li>
<?php
}
Now on your follow up email plugin, you can use {uid} as a variable and this will be replaced with the correct value on every email.
I though the best way was to use short code, but then i discovered this filter and i think this is the best way to handle this. This code is also pretty flexible and you can add as many variables as you want.
Hope this can help someone.

Resources