How to add an expectation list in woocomerce

=> esc_html__(\
‘noWishlist’ => esc_html__(\
)
);
}
add_action( ‘wp_enqueue_scripts’, ‘wishlist_plugin_scripts_styles’ );
In this code, the plug-in’s main style. CSS file and main. Put the JS file in the queue. In addition, in order to work, set some parameters to main. Successfully passed to JS file.
After adding the Ajax URL Ajax post resturl store name code, create the CSS and JS folders and put the file in the folder. CSS folder style. Main of CSS and JS folders. js. To connect the expectation list switchhook the expectation list switchadd the following code in the init task:
\/\/ Add wishlist to product
add_action(‘woocommerce_before_shop_loop_item_title’,’wishlist_toggle’,15);
add_action(‘woocommerce_single_product_summary’,’wishlist_toggle’,25);
function wishlist_toggle(){
global $product;
echo ‘‘. esc_attr__(\
}
The above code adds the above list switch to each product and each single product layout in the loop. Woocommerce_before_shop_loop_item_title and woocommerce_single_product_summary hook are used.
Add SVG icon the next step is to add SVG icon. Create an image folder in the plug-in folder that contains the next Icon. Insert SVG.



Svg animation has three states:
Default: heart path process: end of loading group (g label): to specify the style of group confirmation (g label) icon, use style. Open the CSS file and paste the following code:
.wishlist-toggle {
display: block;
position: absolute;
top: 16px;
left: 16px;
z-index: 5;
width: 24px;
height: 24px;
outline: none;
border:none;
}
.wishlist-title {
display: none;
}
.entry-summary . wishlist-toggle {
position: relative;
top: 0;
left: 0;
display: inline-block;
vertical-align: middle;
margin-bottom: 8px;
}
.entry-summary . wishlist-title {
display: inline-block;
vertical-align: middle;
margin-right: 8px;
margin-bottom: 8px;
}
.wishlist-toggle:focus {
outline: none;
border:none;
}
.wishlist-toggle svg {
fill:#bdbdbd;
transition: all 200ms ease-out;
}
.wishlist-toggle:ho
ver svg,
.wishlist-toggle. active svg {
fill:#000000;
}
.wishlist-toggle svg . loading,
.wishlist-toggle svg . check {
opacity: 0;
}
.wishlist-toggle. active svg . check {
opacity: 1;
}
.wishlist-toggle. active svg . heart {
opacity: 0;
}
.wishlist-toggle. loading svg . loading,
.wishlist-table. loading:before {
animation:loading 500ms 0ms infinite normal linear;
transform-origin: center;
opacity: 1;
}
.wishlist-toggle. loading svg . heart {
opacity:0;
}
@keyframes loading {
from {transform: rotate(0deg);}
to {transform: rotate(360deg);}
}
Shows the heart path of SVG. When the user clicks, the heartbeat path is hidden and the loading path is displayed.
A check mark is displayed to indicate that the product has been successfully added to the desired list after loading. Generate the desired list table shortcut. Now you need to generate the desired list table shortcut. You can add this shortcut to all pages and display the desired list items in them. Add the following code to the initialization plug-in operation to perform this operation:\/\/ Wishlist table shortcode
add_shortcode(‘wishlist’, ‘wishlist’);
function wishlist( $atts, $content = null ) {
extract(shortcode_atts(array(), $atts));
return ‘

‘. esc_html__(\

‘;
}
Then, you must create the expectation list page.
Create a page called \
width:100%;
position: relative;
}
.wishlist-table. loading:after {
display: block;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
content: \
background: #ffffff;
opacity: 0.5;
z-index: 5;
}
.wishlist-table. loading:before {
display: block;
width: 24px;
height: 24px;
position: absolute;
top: 50%;
left: 50%;
margin-top:-12px;
margin-left:-12px;
content: \
background-image: url(‘..\/images\/loading.svg’);
background-repeat: no-repeat;
background-size: 100%;
z-index: 6;
}
.wishlist-tabl
e td {
position: relative;
}
.wishlist-table a.details {
padding:4px 16px;
background: #000000;
color: #ffffff;
text-align: center;
border:none ! important
}
.wishlist-table a.wishlist-remove {
display: block;
width: 24px;
height: 24px;
position: absolute;
top: 50%;
left: 50%;
margin-top:-12px;
margin-left:-12px;
background-image: url(‘..\/images\/remove.svg’);
background-repeat: no-repeat;
background-size: 100%;
z-index: 6;
border:none;
opacity:0;
}
.wishlist-table td:hover > a.wishlist-remove {
opacity:1;
}
Then load. Add SVG images to the images folder.

‘. esc_html__(\
‘. esc_html__(\

Add the following code to the user profile’s expectation list custom option init operation:
\/\/ Wishlist option in the user profile
add_action( ‘show_user_profile’, ‘wishlist_user_profile_field’ );
add_action( ‘edit_user_profile’, ‘wishlist_user_profile_field’ );
function wishlist_user_profile_field( $user ) { ?>

ID ) ); ?> \

‘+object.image+’

(function($){
\
})(jQuery);
The next step is to collect the required data and create several variables. var shopName = opt. shopName+’-wishlist’,
inWishlist = opt. inWishlist,
restUrl = opt. restUrl,
wishlist = new Array,
ls = sessionStorage. getItem(shopName),
loggedIn = ($(‘body’). hasClass(‘logged-in’)) ? true : false,
userData = ”;
If the user logs in: if the current user data is successfully imported with Ajax, update the expectation list and highlight the expectation list. If the session \/ delete local repository fails, the error message if (loggedin) is displayed on the developer console{
\/\/ Fetch current user data
$.ajax({
type: ‘POST’,
url: opt. ajaxUrl,
data: {
‘action’ : ‘fetch_user_data’,
‘dataType’: ‘json’
},
success:function(data) {
userData = JSON. parse(data);
if (typeof(userData[‘wishlist’]) != ‘ undefined’ && userData[‘wishlist’] != null && userData[‘wishlist’] != \
var userWishlist = userData[‘wishlist’];
userWishlist = userWishlist. split(‘,’);
if (wishlist.l
wishlist = get_user_meta( $current_user->ID, ‘wishlist’,true);
echo json_encode(array(‘user_id’ => $current_user->ID,’wishlist’ => $current_user_wishlist));
}
die();
}
add_action( ‘wp_ajax_fetch_user_data’, ‘fetch_user_data’ );
add_action( ‘wp_ajax_nopriv_fetch_user_data’, ‘fetch_user_data’ );
If the expectation list entry already exists in the session \/ local store, the following Ajax request will update the user expectation list. ffunction update_wishlist_ajax(){
if (isset($_POST[\
$user_id = $_POST[\
$user_obj = get_user_by(‘id’, $user_id);
if (!is_wp_error($user_obj) && is_object($user_obj)) {
update_user_meta( $user_id, ‘wishlist’, $_POST[\
}
}
die();
}
add_action(‘admin_post_nopriv_user_wishlist_update’, ‘update_wishlist_ajax’);
add_action(‘admin_post_user_wishlist_update’, ‘update_wishlist_ajax’);
Before going to the help function Events section, we will learn more about the help function. Array. prototype. unique = function() {
return this. filter(function (value, index, self) {
return self. indexOf(value) === index;
});
}
function isInArray(value, array) {return array.indexOf(value) > -1;}
function onWishlistComplete(target, title){
setTimeout(function(){
target
.removeClass(‘loading’)
.addClass(‘active’)
.attr(‘title’,title);
},800);
}
function highlightWishlist(wishlist,title){
$(‘.wishlist-toggle’). each(function(){
var $this = $(this);
var currentProduct = $this. data(‘product’);
currentProduct = currentProduct. toString();
if (isInArray(currentProduct,wishlist)) {
$this. addClass(‘active’). attr(‘title’,title);
}
});
}
The first helper function eliminates duplication and makes the array unique. On the other hand, the second checks whether the given value exists in the given array. The last function runs when an item is added to the expected list, and the last function displays the items in the expected list. The step after adding the switch is to add the click event to the desired list switch to enhance the actual function. Navigate to the store page, refresh the browser, click to switch the expectation list, and you can see its work$ (‘.wishlist-toggle’). each(function(){
var $this = $(this);
v
ar currentProduct = $this. data(‘product’);
currentProduct = currentProduct. toString();
if (!loggedIn && isInArray(currentProduct,wishlist)) {
$this. addClass(‘active’). attr(‘title’,inWishlist);
}
$(this). on(‘click’,function(e){
e.preventDefault();
if (!$this.hasClass(‘active’) && !$ this. hasClass(‘loading’)) {
$this. addClass(‘loading’);
wishlist. push(currentProduct);
wishlist = wishlist. unique();
if (loggedIn) {
\/\/ get user ID
if (userData[‘user_id’]) {
$.ajax({
type: ‘POST’,
url:opt. ajaxPost,
data:{
action:’user_wishlist_update’,
user_id :userData[‘user_id’],
wishlist :wishlist. join(‘,’),
}
})
.done(function(response) {
onWishlistComplete($this, inWishlist);
})
.fail(function(data) {
alert(opt.error);
});
}
} else {
sessionStorage. setItem(shopName, wishlist.toString());
onWishlistComplete($this, inWishlist);
}
}
});
});
In the list table above, list the main at the bottom of the project rapper function. Add the following code to JS: setTimeout(function(){
if (wishlist.length) {
restUrl += ‘? include=’+wishlist. join(‘,’);
restUrl += ‘&per_page=’+wishlist. length;
$.ajax({
dataType: ‘json’,
url:restUrl
})
.done(function(response){
$(‘.wishlist-table’). each(function(){
var $this = $(this);
$.each(response,function(index,object){
$this. append(‘
<input type="text" name="wishlist" id="wishlist" value="

‘+object.title[\
+'<?php }
add_action( 'personal_options_update', 'save_wishlist_user_profile_field' );
add_action( 'edit_user_profile_update', 'save_wishlist_user_profile_field' );
function save_wishlist_user_profile_field( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'wishlist', $_POST['wishlist'] );
}
파워 잇 이제 중요한 섹션을 추가했으므로 켤 준비가 되었습니다. main.js 파일을 열고 다음 코드를 넣습니다.

‘);
});
});
})
.fail(function(response){
alert(opt.noWishlist);
})
.always(function(response){
$(‘.wishlist-table’). each(function(){
$(this). removeClass(‘loading’);
});
});
} else {
$(‘.wishlist-table’). each(function(){
$(this). removeClass(‘loading’);
});
}
},1000);
There are two rest API options. Use word press rest API and woommerce rest API. Navigate to the default plug-in file and add the following code at the bottom:\/\/ Extend REST API
function rest_register_fields(){
register_rest_field(‘product’,
‘price’,
array(
‘get_callback’ => ‘rest_price’,
‘update_callback’ => null,
‘schema’ => null
)
);
register_rest_field(‘product’,
‘stock’,
array(
‘get_callback’ => ‘rest_stock’,
‘update_callback’ => null,
‘schema’ => null
)
);
register_rest_field(‘product’,
‘image’,
array(
‘get_callback’ => ‘rest_img’,
‘update_callback’ => null,
‘schema’ => null
)
);
}
add_action(‘rest_api_init’,’rest_register_fields’);
function rest_price($object,$field_name,$request){
global $product;
$id = $product->get_id();
if ($id == $object[‘id’]) {
return $product->get_price();
}
}
function rest_stock($object,$field_name,$request){
global $product;
$id = $product->get_id();
if ($id == $object[‘id’]) {
return $product->get_stock_status();
}
}
function rest_img($object,$field_name,$request){
global $product;
$id = $product->get_id();
if ($id == $object[‘id’]) {
return $product->get_image();
}
}
function maximum_api_filter($query_params) {
$query_params[‘per_page’][\
return $query_params;
}
add_filter(‘rest_product_collection_params’
, ‘maximum_api_filter’);
Delete the item main from the expectation list. Add the following code at the bottom of the rapper function of the JS file:$ (document). on(‘click’, ‘.wishlist-remove’, function(){
var $this = $(this);
$this. closest(‘table’). addClass(‘loading’);
wishlist = [];
$this. closest(‘table’). find(‘tr’). each(function(){
if ($(this). data(‘product’) != $ this. closest(‘tr’). data(‘product’)) {
wishlist. push($(this). data(‘product’));
if (loggedIn) {
\/\/ get user ID
if (userData[‘user_id’]) {
$.ajax({
type: ‘POST’,
url:opt. ajaxPost,
data:{
action:’user_wishlist_update’,
user_id :userData[‘user_id’],
wishlist :wishlist. join(‘,’),
}
})
.done(function(response) {
$this. closest(‘table’). removeClass(‘loading’);
$this. closest(‘tr’). remove();
})
.fail(function(data) {
alert(opt.error);
});
}
} else {
sessionStorage. setItem(shopName, wishlist.toString());
setTimeout(function(){
$this. closest(‘table’). removeClass(‘loading’);
$this. closest(‘tr’). remove();
},500);
}
}
});
});
The results are as follows: Conclusion this tutorial creates a custom plug-in for adding the expectation list to woocomerce. We hope this feature will help you add this post to the store. Similar articles: woocommerce redirection after logout [Ultimate Guide] woocommerce redirection after checkout: redirect the complete guide used by WordPress custom hook. Do_action and apply_filters with example: WP. Template is not a function. If you don’t log in to WordPress, how to redirect users? How to add a search window at the top of the front theme of the store on the page redirection page woodpress how to get the name of the current product category woodpress how to hide tax labels how users in WordPress
Confirm whether to log in to the method of s to programmatically create the method of product woocommerce product description and display woocommerce method woocommerce disable distribution by category method woocommerce get settlement URL woocommerce change call symbol method add to woocommerce shopping basket by product ID How to create button woodpress custom store page woodpress how to set woodpress storefront blog, how to increase the participation of more than 40 WordPress contact form plug-ins WordPress uses more than 30 best comment plug-ins woodpress login form to add fields Method of adding social media icon to: send mpesa woocommerce settlement method: set woocommerce storefont Mega menu method: 5 + method of programmatically adding products to shopping basket in the best woocommerce store ye woocommerce settlement page: method of obtaining order ID woocommerce woocommerce login reset backward Add a description of the account page redirection and example in woocomer.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *