Wordpress create lists of favorites for users - wordpress

I have to create lists of favorites products in woocommerce.
First I've created a new custom post type to manage my products list.
Now I need to add my products list to my custom type but I don't know how to do this.
The final result will be a page listing all my lists for a user contains all added products by user.
Can anyone help ?

I found a pretty comprehensive (paid) plugin by WooCommerce that does exactly this: WooCommerce Wishlists. I haven't tried it.
WooCommerce Wishlists allows guests and customers to create and add products to an unlimited number of Wishlists. From birthdays to weddings and everything in between, WooCommerce Wishlists is a welcome addition to any WooCommerce store.
Plugin link: https://woocommerce.com/products/woocommerce-wishlists/

Thanks for your answer but i've needed to make my own plugin.
WC Wishlist allow you to make only one list ;)
After some works i've found a solution with meta_user_data to manage my lists.
public function action_create_favorite_list(){
if( ! is_user_logged_in() ) :
return;
endif;
$list = new stdClass();
$list->name = $_POST["list_name"];
$list->items = array();
$fav = get_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', true);
if(empty($fav))
{
add_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', array());
$fav = get_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', true);
}
array_push($fav,$list);
update_user_meta( get_current_user_id(), 'user_'.get_current_user_id().'_favlist', $fav);
wp_send_json($fav);
wp_die();
}

Related

Woocommerce Product Vendor extension - Loading ACF fields into a vendor's store front

I am using ACF to add fields to my vendors' dashboard profile pages. I currently have a test ACF field loading the field from only the WP Admin profile page on all the vendors' product listing page using this simple hook in my child theme's functions.php:
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() { ?>
<?php if(get_field('founded_on')) { ?>
<?php the_field('founded_on'); ?>
<?php }
}
Perhaps I'm pulling the wrong hook, but I can't seem to find the right hook in the Product Vendor frontend page.
I need help customizing it so that when you are on a vendor's product page, it pulls the ACF fields from that particular vendor's profile. Currently, it partially works; however it only pulls the WP main admin's data for all the different vendors'.
I know there is a way to pull the vendor's ID for each particular vendor's page and have it load their data for their page, but my php knowledge is very limited. I usually just hunt for existing code and tweak it. Unfortunately I haven't found any solutions that have worked, and this is the closest I've come to getting custom fields to work on a vendor's page.
Or if anyone can point me to a better solution to allow me to create customer fields for a vendor to fill out that will be loaded on their front end page, that would be great. I've tried Nicola Mustone's solution ( here ), which would have been perfect, except I couldn't get it to load the new custom fields on the vendor's store profile form page, nor have it load the fields into that vendor's storefront page. Based on comments, it only shows up for the site's Admin and only they can edit it. There's no visible way to have it load on the storefront, which defeats the purpose.
I imagine that the providers are users with a certain level within the WordPress system?
Considering that this is your case, the ACF fields need some additional parameters to become visible:
$post_id = false; // current post
$post_id = 1; // post ID = 1
$post_id = "user_2"; // user ID = 2
$post_id = "category_3"; // category term ID = 3
$post_id = "event_4"; // event (custom taxonomy) term ID = 4
$post_id = "option"; // options page
$post_id = "options"; // same as above
$value1 = the_field( 'my_field', $post_id );
$value2 = get_field( 'my_field', $post_id );
take some examples found in the ACF documentation, but in your particular case you have to pass the user's ID
the_field('founded_on', 'user_' . $user->ID );
echo get_field('founded_on', 'user_' . $user->ID );
function documentation the_field()
You need to pull the user ID of the user and then use the format user_{$user->ID} for the post ID as the second parameter of the ACF field.
If I understand your question, then this should work.
add_action( 'woocommerce_archive_description', 'vendor_profile', 7 );
function vendor_profile() {
$user = wp_get_current_user();
if ( get_field( 'founded_on', 'user_' . $user->ID ) ) {
the_field( 'founded_on', 'user_' . $user->ID );
}
}

Display posts on Elementor that belong to Page Author ID (Page Creator)

Hi I wanted to use elementor widgets and show some custom posts for current post author. I thought maybe with Query ID in Elemntor I can do it easily so the widget will show **some custom posts of "Page Author" I wrote a query that show the custom-post-type1 and custom-post-type2 of current post author
My query doesn't work unfortunately
function mzba_post_types( $query ) {
$query->set( 'post_type', [ 'custom-post-type1', 'custom-post-type2' ] );
$author = get_queried_object_id();
if( is_single() ){
$author = get_the_author_meta( 'ID' );
}
$query_vars[ 'author' ] = $author;
}
add_action( 'elementor/query/{$query_id}', 'mzba_post_types' );
anyone can help for a query (or any other way) that shows the Page creator custom posts in Elementor?
thank you so so mcuh for helping
This might help you. However, you can try pre-build filters as well to achieve this.
https://elementor.com/blog/introducing-advanced-query-control/

Woocommerce custom Product slug

Is there a way to create custom product URL based on the product attributes, I have a product sunglasses which has a couple of attributes tied to it: metal, blue and round, so the current URL is:
website.com/glasses/sunglasses/abram-widana-629/
What I am trying to get is a URL which has those attributes included:
website.com/glasses/sunglasses/abram-widana-meta-blue-round-629/
I would really appreciate if someone would even just point me to the right direction on how to tackle this.
There are two ways to do this, either Manually or Programmatically.
Manually adjusting permalinks:
In your example, you are simply adjusting the product URL to include attributes. This can be achieved manually by editing the permalink on the product itself.
Once the product has been added/saved, you will see the permalink showing directly under the title field like this:
Simply click the Edit button next to it and change it from abram-widana-629 to abram-widana-meta-blue-round-629
Programmatically adding attributes to permalinks:
If you want to try to achieve this permanently for all products you will have to work through the "save_post" filter/hook to add all the attributes to the permalink. The only downfall from this is that you will no longer be able to adjust your individual permalinks for your products as they will simply revert back once you click save.
Below is a code example of how to achieve that:
add_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
function add_custom_attributes_to_permalink( $post_id, $post, $update ) {
//make sure we are only working with Products
if ($post->post_type != 'product' || $post->post_status == 'auto-draft') {
return;
}
//get the product
$_product = wc_get_product($post_id);
//get the "clean" permalink based on the post title
$clean_permalink = sanitize_title( $post->post_title, $post_id );
//next we get all of the attribute slugs, and separate them with a "-"
$attribute_slugs = array(); //we will be added all the attribute slugs to this array
foreach ($_product->get_attributes(); as $attribute_slug => $attribute_value) {
$attribute_slugs[] = $attribute_value;
}
$attribute_suffix = implode('-', $attribute_slugs);
//then add the attributes to the clean permalink
$full_permalink = $clean_permalink.$attribute_suffix;
// unhook the save post action to avoid a broken loop
remove_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
// update the post_name (which becomes the permalink)
wp_update_post( array(
'ID' => $post_id,
'post_name' => $full_permalink
));
// re-hook the save_post action
add_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
}

Woocommerce variable product on custom single product template

I've developed custom single product template for Woocommerce for products which works correctly and as intended. The problem is that now the customer wants some different variations of products that are being sold and I can't work out how to add this functionality.
Currently on the product page I have used functions such as the_post_thumbail() and get_post_meta() to display various information about the product such as price and excerpt entered in the product post type. I then use do_action('woocommerce_simple_add_to_cart') along with a few other buttons.
This all works fine but the problem is now with the variation feature. I found the following code which echos out the product variations ID and Variation Type but I'm not sure how to implement this into a working system. Any help or guidance would be appreciated.
global $product, $post;
$variations = $product->get_available_variations();
foreach ($variations as $key => $value) {
echo 'variation ID'.$value['variation_id'];
foreach ($value['attributes'] as $attr_key => $attr_value) {
echo $attr_key.': '.$attr_value;
}
}
I found the answer so I'm just posting this here in case anyone else has the same question. It was as simple as changing do_action('woocommerce_simple_add_to_cart') to do_action('woocommerce_variable_add_to_cart'). This displays the product variations in a list box and has the add to cart button.

Limit number of custom post type posts

Is it possible to limit the number of posts a admin can create in a custom post type? It's for a plugin and the admin should only be able to create 10 posts. Thanks.
Here you go (untested, but you should get the idea):
function check_allowed( $post_id ) {
$current_user = wp_get_current_user();
$q = new WP_Query(array('post_type'=>'limited','post_author'=>$current_user->ID));
if($q->found_posts >=10) {
return false;
}
}
add_action( 'publish_post', 'check_allowed' );
Btw: You probably do not want to limit admins in the number of posts they may publish, use editors or a custom role for this purpose.

Resources