Add Dropdown cities to woocommerce - wordpress

I want to add a dropdown for woocommerce cities.
I find a code for adding states as dropdown, but i dont know how to do this for cities. I try to make an analogy but it doesnt work.
Also, if posible i would like to shop cities based on the states, so i need a condition: if state = X then show this cities
The code for states was this:
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['RO'] = array(
'TL' => 'Tulcea',
'VS' => 'Vaslui',
'VL' => 'Valcea',
'VN' => 'Vrancea'
);
return $states;
}

I havent found another way to do that except through this plugin https://github.com/8manos/wc-city-select , it adds functionality so you can do something like so
add_filter( 'wc_city_select_cities', 'my_cities' );
function my_cities( $cities ) {
$cities['NG'] = array(
'LA' => array(
'Ikorodu',
'Ikeja'
),
'OY' => array(
'Ibadan',
'Saki'
)
);
return $cities;
}
Of course the snippet will be in your functions.php

Related

woocommerce_product_query dont work on pages with shop (dont show specific product)

i have some categories, product can be in some of them but when its in cat with ID 46 its should not list anymore in the product list.
The following code work in the shop sites but not when i add the shop over the bb/shortshort , thanks
<!-- wp:woocommerce/product-category {"columns":4,"rows":6,"categories":[31]} /-->
add_action( 'woocommerce_product_query','custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => [46],
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
edit:
after some research i found a way, not the best but solved the problem.
Its seems like the data used for these pages are written in the options, so maybe here is a way to block adding the id of the unwanted
The working code is following and on this way it would be maybe a good hook for use same html/functions like used in the shop pages because all the product hooks of wc don't work usually on none shop pages.
Maybe WC adds someday direct a ignore product cats in the code, im sure im not the only one who need it
function my_product_block( $html, $data, $product ) {
if(in_array(46, $product->category_ids) || $product->stock_status === "outofstock"){
return "";
}
return $html;
}
add_filter( 'woocommerce_blocks_product_grid_item_html','my_product_block', 10,3)

Woocommerce URL to hide out of stock product

I create a catalog website using Woocommerce to display all the product. When the product is sell, i dont remove it from the website (because we dont have a lot of product and we want to show to the customer what we sell before).
So, when you go on "All the products" you see the Sell products and the products available. I want, on the sidebar create a button "Show only available product". I dont find a plugin who can do this..
Whis woocommerce, can i create a URL like "mywebsite.com/products&instock=true" for example or something like this ? or if you know another solution. Thanks
You can use pre_get_posts to achieve it. Set the tax_query to not get the term outofstock of the product_visibility taxonomy.
In addition to my code, you will of course need to create a link with the prefix_instock=true parameter. You also can store it in a cookie, so it will be easily persistent.
add_action( 'pre_get_posts', 'prefix_hide_out_of_stock_products' );
function prefix_hide_out_of_stock_products( $q ) {
if ( ! $q->is_main_query() || is_admin() || empty($_GET['prefix_instock'])) {
return;
}
if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) && $_GET['prefix_instock'] == 'true') {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $outofstock_term->term_taxonomy_id ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
remove_action( 'pre_get_posts', 'prefix_hide_out_of_stock_products' );
}

Appending options to order table using the woocommerce_my_account_my_orders_actions filter

Im trying to add a few options to the orders list in the customer's account page, sadly im not being able to and can't find any information online.
Im attempting this hooking a function into the woocommerce_my_account_my_orders_actions like this:
add_filter ('woocommerce_my_account_my_orders_actions', 'mw_add_invoice_button_to_list' );
function ( $actions )
{
$actions[] = array(
"url" => "testURL",
"name" => "testNAME"
);
return $actions;
}
Im quite new to using filters so this is probably a simply mistake, can you help?
Thanks.
Yes indeed it is a very simple mistake :) . You forgot to add the name of the function
function ( $actions ) ??
try
function mw_add_invoice_button_to_list ( $actions )
Or another choose options.
add_filter ('woocommerce_my_account_my_orders_actions', function ( $actions ) {
$actions[] = array(
"url" => "testURL",
"name" => "testNAME"
);
return $actions;
}, 10, 2 );

Adding User Profile Fields to Wordpress Buddypress to Include "Favorites"

I'm trying to figure out a way for members on my Wordpress (Buddypress) site to pick their "favorite movies, books, etc."
It would be nice if, instead of members simply typing a list of these things, they could select from books already in the system, and add more as the please in the future.
I'm hoping that there is an easy answer to this, such as a plugin that I can use, or, at least, modify. Does anyone know of anything that I can look into?
Your title asks how to add a user profile field. Here is the code to add as many fields as you like. Once you add the field, you can easily place additional inputs or options on the custom tab page for users to enter their own favorites.
function my_test_setup_nav() {
global $bp;
$parent_slug = ‘test’;
$child_slug = ‘test_sub’;
//name, slug, screen, position, default subnav
bp_core_new_nav_item( array(‘name’ => __( ‘Test’ ),’slug’ => $parent_slug,’screen_function’ => ‘my_profile_page_function_to_show_screen’,'position’ => 40,’default_subnav_slug’ => $child_slug ) );
/* Add the subnav items to the profile */
// name, slug, parent_url, parent slug, screen function
bp_core_new_subnav_item( array( ‘name’ => __( ‘Home’ ), ‘slug’ => $child_slug, ‘parent_url’ => $bp->loggedin_user->domain . $parent_slug.’/', ‘parent_slug’ => $parent_slug, ‘screen_function’ => ‘my_profile_page_function_to_show_screen’ ) );
bp_core_new_subnav_item( array( ‘name’ => __( ‘Random Page’ ), ‘slug’ => ‘random’, ‘parent_url’ => $bp->loggedin_user->domain . $parent_slug.’/', ‘parent_slug’ => $parent_slug, ‘screen_function’ => ‘my_profile_page_function_to_show_screen234′ ) );
}
function my_profile_page_function_to_show_screen() {
//add title and content here – last is to call the members plugin.php template
add_action( ‘bp_template_title’, ‘my_profile_page_function_to_show_screen_title’ );
add_action( ‘bp_template_content’, ‘my_profile_page_function_to_show_screen_content’ );
bp_core_load_template( apply_filters( ‘bp_core_template_plugin’, ‘members/single/plugins’ ) );
}
function my_profile_page_function_to_show_screen_title() {
echo ‘wptaskforce title’;
}
function my_profile_page_function_to_show_screen_content() {
echo ‘wptaskforce content’;
}
//random page content:
function my_profile_page_function_to_show_screen234() {
//add content here – last is to call the members plugin.php template
add_action( ‘bp_template_content’, ‘my_profile_page_function_to_show_screen234_content’ );
bp_core_load_template( apply_filters( ‘bp_core_template_plugin’, ‘members/single/plugins’ ) );
}
function my_profile_page_function_to_show_screen234_content() {
echo ‘This is a random page.’;
}
add_action( ‘bp_setup_nav’, ‘my_test_setup_nav’ );

Listing post with this way "domain.com/taxonomy/category

I am triying to modify wordpress (version 3.3) to list post with one category and with one taxonomy.
I have got a taxonomy called "location". If I do example.org/location/canada, it works. Now I want url rewrite for example.org/location/canada/category/dogs, but i cant achieve it.
I added to functions.php this code:
function eg_add_rewrite_rules() {
global $wp_rewrite;
$new_rules = array(
'location/(.+)/category/(.+)/?$' => 'index.php?location=' . $wp_rewrite->preg_index(1) . '&category_name=' . $wp_rewrite->preg_index(2)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );
Also i have added this:
function eg_add_query_vars( $query_vars ) {
$new_vars = array( 'location' );
return array_merge( $new_vars, $query_vars );
}
add_filter( 'query_vars', 'eg_add_query_vars' );
Categories are taxonomy. So it depends on what taxonomy you want to use there. Tags, Categories, Custom Taxonomy.
If you want to create custom taxonomy, you can. Check out http://codex.wordpress.org/Taxonomies
Here is an example of registering custom taxonomy.
function people_init() {
// create a new taxonomy
register_taxonomy(
'people',
'post',
array(
'label' => __( 'People' ),
'rewrite' => array( 'slug' => 'person' ),
'capabilities' => array('assign_terms'=>'edit_guides', 'edit_terms'=>'publish_guides')
)
);
} add_action( 'init', 'people_init' );
You will then need to edit your permalinks. Go to settings > Permalinks. Using the custom, change the URL to reflect how you want your paths to display.
If you like, you may enter custom structures for your category and tag URLs here. For example, using topics as your category base would make your category links like http://example.org/topics/uncategorized/. If you leave these blank the defaults will be used.
Problem solved:
In functions.php (or in plugin if you want).
You must put this code to rewrite this url www.example.org/en/[mylocation]/de/[mycategory]
add_action('init', 'flush_rewrite_rules');
add_filter('category_rewrite_rules' , 'add_rules' ) ;
function flush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function add_rules($rules)
{
/**
* Loop em.
* -------------------------------------------- */
$feed_rule = 'index.php?location=$matches[1]&category_name=$matches[2]&feed=$matches[3]';
$paged_rule = 'index.php?location=$matches[1]&category_name=$matches[2]&paged=$matches[3]';
$base_rule = 'index.php?location=$matches[1]&category_name=$matches[2]';
$rules['en/([^/]+)/de/([^/]+)/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = $feed_rule;
$rules['en/([^/]+)/de/([^/]+)/page/?([0-9]{1,})/?$'] = $paged_rule;
$rules['en/([^/]+)/de/([^/]+)/?$'] = $base_rule;
$feed_rule2 = 'index.php?location=$matches[1]&feed=$matches[2]';
$paged_rule2 = 'index.php?location=$matches[1]&paged=$matches[2]';
$base_rule2 = 'index.php?location=$matches[1]';
$rules['en/([^/]+)/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = $feed_rule2;
$rules['en/([^/]+)/page/?([0-9]{1,})/?$'] = $paged_rule2;
$rules['en/([^/]+)/?$'] = $base_rule2;
return $rules;
}

Resources