Hi Shane,
No problem, yes of course. I need to display the SKU outside of the product page. There for I've found a function with shortcode, but this trowing an error like described earlier in the ticket. Toolset won't save my content template or view whit this code in my child theme's function.php. Is there a workaround available? Same thing whas happening with the function for the attributes Diego modified, but I'm not sure why this is happening.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Jelle,
I believe I know what the issue is, however i would need to test the fixes on the site ?
Would it be possible to provide me with access so that I can do this and then let you know what the fix is ?
Also could you send me an example page where the issue is ?
Looking forward to hearing from you.
Thanks,
Shane
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Jelle,
Could you let me know which template you are using the shortcode in ? I want to ensure that i'm looking in the right place.
Thanks,
Shane
Hi Shane,
I;m using the shortcode in Datasheet Child view, first th in the loop item section. I have removed the shortcode from functions.php to prefend Toolset from giving any errors. The code I have used and which gave the errors is:
function display_woo_sku() {
global $product;
return $product->get_sku();
}
add_shortcode( 'woo_sku', 'display_woo_sku' );
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Jelle,
Try using this shortcode.
function display_woo_sku($atts) {
// Attributes
$atts = shortcode_atts(
array(
'id' => '',
),
$atts
);
$product_id = $atts['id'];
if(empty($atts['id']) ){
$product_id = get_the_ID() ;
}
$sku = "Woocommerce is not active";
if ( class_exists( 'WooCommerce' ) ) {
$product = wc_get_product($product_id);
$sku = $product->get_sku();
}
return $sku
}
add_shortcode( 'woo_sku', 'display_woo_sku' );
Please try this and let me know if it works. Also you will need to change your shortcode to [woo_sku id='[wpv-post-id]']
Thanks,
Shane
Hi Shane,
Thanks. When I try so save the snippet an error returs:
Your PHP code changes were rolled back due to an error on line 120 of file wp-content/themes/Zephyr-child/functions.php. Please fix and try saving again.
syntax error, unexpected '}', expecting ';'
What should I do?
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Jelle,
I forgot the semi colon on the return statement.
Replace this line
return $sku
with
return $sku;
This should fix it.
Hi Shane,
Thanks again. Looks like the snippet and shortcode are working, but again after modifying a content template, in this case: Loop item in Datasheet Child view, Toolset throws an error like shown in the image.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Jelle,
I checked on the template and it was saving fine from my end.
Hi Shane,
I guess that’s because I’ve removed the snippet from functions.php. Does it also saves on your end with the snippet active in the child theme functions.php?
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Jelle,
Could you let me know where this view is loaded on the frontend?
You can just send a link to the page.
I need to know this so that I can fully understand.
Thanks,
Shane
Hi,
Please follow this link: hidden link
The field sku shows: [woo_sku id='57025'] this is because I removed the snippet from child theme's function.php in order to get the view saveable. With the snippet in functions.php the SKU shows up, but I'm not able to make modifications and save the view. In this case the red warning appears like I showed you in the screenshot. Thanks.
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Jelle,
The issue was being caused by the get_sku() function that woocommerce uses. I was still able to get the sku from the product object without it.
So it should be fine now.
Thanks,
Shane
Hi Shane,
Thanks. Is this normal behaviour and do I need to modify anything or did you do it for me?
Shane
Supporter
Languages:
English (English )
Timezone:
America/Jamaica (GMT-05:00)
Hi Jelle,
I made the modification.
No this isn't normal, however the template does save even if it says cannot save. I tested this scenario and it did save but I still went ahead to debug and resolve it.
Thanks,
Shane