I am trying to update the a 'product_themes' user field in WooCommerce/WP with a function that collects the term_ids for the items in an order and adds them to the user's profile under the 'product_themes' field at the time when the order is placed. This is working when I specify an array of IDs in the code but does not seem to be collecting the term IDs from each item for a custom 'theme' taonomy.
add_action('woocommerce_thankyou', 'add_theme_to_user');
add_action('woocommerce_new_order', 'add_theme_to_user');
function add_theme_to_user($order_id){
$current_user = wp_get_current_user();
$userID = $current_user->ID;
$user = 'user_' . $userID;
$prodCats = array();
$order = new WC_Order( $order_id );
$items = $order->get_items();
$push = array();
foreach ( $items as $item ) {
$item_id = $item['order_item_id'];
$product_name = $item['name'];
$terms = wp_get_post_terms( $itemID, 'theme' );
foreach ( $terms as $term){
array_push($push, $term->term_id);
}
}
update_field('product_themes', $push, 'user_' . $userID);
}
I have consulted the WooCommerce documentation and searched for a solution on any similar problems but I cannot seem to get this to work. Am I missing something?
Many thanks.
I found the solution myself, changed $itemID = $item['order_item_id']; to $itemID = $item['product_id'];
add_action('woocommerce_thankyou', 'add_theme_to_user');
add_action('woocommerce_new_order', 'add_theme_to_user');
function add_theme_to_user($order_id){
$current_user = wp_get_current_user();
$userID = $current_user->ID;
$user = 'user_' . $userID;
$prodCats = array();
$order = new WC_Order( $order_id );
$items = $order->get_items();
$push = array();
foreach ( $items as $item ) {
$itemID = $item['product_id'];
$terms = wp_get_post_terms( $itemID, 'theme' );
foreach ( $terms as $term){
array_push($push, $term->term_id);
update_field('product_themes', $push, 'user_' . $userID);
}
}
update_field('product_themes', $push, 'user_' . $userID);
}
Related
I am looking for a way to display a specific attribute on the single Product page of WooCommerce.
I found this code in the folling topic; Shortcode that display all product attributes set for a WooCommerce product
function get_product_attributes_shortcode($atts ) {
// Extract shortcode attributes
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$html = []; // Initializing
foreach ( $product->get_attributes() as $attribute => $values ) {
$attribute_name = wc_attribute_label($values->get_name());
$attribute_data = $values->get_data();
$is_taxonomy = $attribute_data['is_taxonomy'];
$option_values = array(); // Initializing
// For taxonomy product attribute values
if( $is_taxonomy ) {
$terms = $values->get_terms(); // Get attribute WP_Terms
// Loop through attribute WP_Term(s)
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $attribute );
$option_values[] = ''.$term->name.'';
}
}
// For "custom" product attributes values
else {
// Loop through attribute option values
foreach ( $values->get_options() as $term_name ) {
$option_values[] = $term_name;
}
}
$html[] = '<strong>' . $attribute_name . '</strong>: ' . implode(', ', $option_values);
}
return '<div class="product-attributes">' . implode(' | ', $html) . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
I can display all attributes by using the shortcode [display-attributes] but I want to display only one specific attribute (in my case the attribute is "Kunstenaar"). However I don't know how to adjust the code to make this work. Anyone who can help me out?
If you need just specific attribute, you don't have to get them all and then go through them. WC have function get_attribute() - documentation
function get_product_attributes_shortcode( $atts ) {
extract( shortcode_atts( array(
'id' => get_the_ID(),
), $atts, 'display-attributes' ) );
global $product;
if ( ! is_a($product, 'WC_Product') ) {
$product = wc_get_product( $id );
}
if ( is_a($product, 'WC_Product') ) {
$kunstenaar = $product->get_attribute( 'Kunstenaar' );
return '<div class="product-attributes"><strong>Kunstenaar</strong>: ' . $kunstenaar . '<div>';
}
}
add_shortcode( 'display-attributes', 'get_product_attributes_shortcode' );
Currently all posts is being listed by category,
i need to filter the list by a meta-key. I used the filter('posts_where') but all the query has been changed.
I need to add the WHERE condition in the existing sql that is being generated.
function get_filtered_post( $args, $meta, $value ){
$posts = get_posts( $args );
$ids = array();
foreach( $posts as $post ){
$id = $post->ID;
if( get_post_meta( $id, $meta, true ) == $value ){
$ids[] = $id;
}
}
return $ids;
}
$args = array( 'post_type' => 'post', 'posts_per_page' => -1 );
$IDofPost = get_filtered_post( $args, 'my-meta-key', 'the-metas-value' );
foreach( $IDofPost as $id ){
echo get_the_title( $id );
}
Hope this function will help. It returns post id with the given post meta values.
I added this function in my themes functions.php but it just not working.. any idea what i do wrong?
function woocommerce_thankyou_fun( $order_id )
{
$order = new WC_Order( $order_id );
// $ttotal = $order->get_order_total();
//echo trim( str_replace( '#', '', $order->get_order_number() ) );
//$order_id = absint( $wp->query_vars['order-received'] );
//$order = new WC_Order($GLOBALS['post']->ID);
echo $order;
}
add_shortcode('Woocommerce-Thankyou', 'woocommerce_thankyou_fun');
Use this code to see all datamember of order object.
function woocommerce_thankyou_fun( $order_id )
{
$order = new WC_Order( $order_id );
echo "<pre>";
print_r( $order);
echo "</pre>";
die();
}
add_shortcode('woocommerce-thankyou', 'woocommerce_thankyou_fun',20,1);
Is there anyone who knows why this is going on. Wordpress lists my custom taxonomies in the order they where inserted and not alphabetically. This is the code I'm using.
function get_the_term_list_inclusief( $id, $taxonomy, $before = '', $sep = '', $after = '') {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
$links = array();
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) ) {
return $link;
}
$links[] = '<a class="ajax" href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
}
/**
* Filter the term links for a given taxonomy.
*
* The dynamic portion of the filter name, `$taxonomy`, refers
* to the taxonomy slug.
*
* #since 2.5.0
*
* #param array $links An array of term links.
*/
$term_links = apply_filters( "term_links-$taxonomy", $links );
return $before . join( $sep, $term_links ) . $after;
}
Is there away to use this code and have the taxonomies order by title or ASC?
Thanks
Try if this works:
// ...
$links = array();
usort($terms, function($a, $b) {
return strcmp($a->name, $b->name);
});
foreach ( $terms as $term ) {
// ...
I am trying to display prices next to variations on Woocommerce, which I could do just fine. Now instead of showing prices on ALL variations I just want to show prices next to variations belonging to a specific category.
This is what I have come up with so far and logically it makes sense, but not working on this particular category. Any suggestions on how to troubleshoot?
//get prices of variations
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
$itemPrice = strip_tags (woocommerce_price( $_product->get_price() ));
//this is where you can actually customize how the price is displayed
return $term . ' (' . $itemPrice . ')';
}
return $term;
//Add prices to variations
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $termItem ) $categories[] = $termItem->slug;
if ( in_array( 'custom-pet-portrait-painting', $categories ) ) {
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
} //end function
}
Or can I use get_terms to wrap the below code in which shows the variations price? I pretty much just need to show the prices for variations pertaining to one category.
if (has_term ('my-category', 'product_cat')) {
//get prices of variations
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
$itemPrice = strip_tags (woocommerce_price( $_product->get_price() ));
//this is where you can actually customize how the price is displayed
return $term . ' (' . $itemPrice . ')';
}
return $term;
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
} //end has_term