How can I customize a template for custom Woocommerce fields? - wordpress

I created some fields with Advanced Custom Fields for the woocommerce orders. I want to custom the template orders.php to show the ACF fields in the orders table. These fields includ images. I found no plugin or hook to do this for the orders users page.
This hook below shows how to add a button to orders action. What I need is add a column to show a custom field.
function sv_add_my_account_order_actions( $actions, $order ) {
$actions['help'] = array(
// adjust URL as needed
'url' => '/contact/?&order=' . $order->get_order_number(),
'name' => __( 'Get Help', 'my-textdomain' ),
);
return $actions;
}
add_filter( 'woocommerce_my_account_my_orders_actions', 'sv_add_my_account_order_actions', 10, 2 );
Thanks!

Related

Elementor custom filter query in custom post type template - How to query field value to filter by?

I have a custom post type which is a sort of profile page for suppliers. On the end of each page, I have a products-widget, which is supposed to show the products of this supplier.
All products are assigned to a supplier-specific category. So profile-page of supplier A should show all products assigned to product_cat A.
The corresponding product_cat is specified by an acf-field on each profile page.
So my custom filter query needs to get the specified product_cat and then filter all products by that category.
I'm struggling on how to get that custom field value from the post into my filter query.
This is what I have so far:
add_action( 'elementor/query/my-custom-query', function( $query ) {
$filtre = get_field( "supplier_cat" );
$query-> set('product_cat' , $filtre );
} );
The get_field function might not working as if ACF is causing somehow an infinite loop in Elementor. That is why you have to use get_post_meta.
Here is a suggestion. In the assumption, you have "Product" and "Supplier" as custom post types and "Product" has as a custom taxonomy (category) "Supplier". "Supplier" has an Advanced Custom Field with supplier_cat.
To show all to the supplier related products you can select in the Query section of the Posts Widget of Elementor:
Source: Product
Query ID: my-custom-query
With this code in your theme's function.php file:
add_action( 'elementor/query/my-custom-query', function( $query ) {
$postid = get_the_ID();
$supplier_cat = get_post_meta( $postid, 'supplier_cat', true );
$tax_query = array(
array(
'taxonomy' => 'supplier_cat',
'field' => 'slug',
'terms' => $supplier_cat,
)
);
$query->set( 'tax_query', $tax_query );
} );
Reference of this action hook:
https://developers.elementor.com/custom-query-filter/

WooCommerce: add a few lists to wishlist by custom plugin

I have custom plugin for first adding lists to Wishlist plugin (Woocommerce) by users.
I have custom step by step form, where user can choose number of lists (from 1 to 10) and enter titles and descriptions for these new lists.
There is Ajax request on the last step of my form.
How do I add these lists to database?
I'm trying to add by wp_insert_post( $my_post ) but I should add settings for postmeta table too.
You could save the form results as custom post type with the results as custom fields.
Set the post type as not public and not set to not be in search results.
If they is no logic preformed on the wishlist you could set the data to array, and save it serialized in one field.
To send the data from front end (user page) to back end (server) you could or use wp ajax admin or through wp-rest api
Save fields to custom post type example. Could be the field name is different in your site so set to according to your fields key
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['post_title'] ),
'post_content' => $_POST['post_content'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 ),
'post_type' => 'event'
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post );
// Updating the meta data (custom fields values)
if ( isset( $_POST['_wishlist_email'] ) ) {
update_post_meta( $post_id, '_wishlist_email', sanitize_text_field( $_POST['_wishlist_email'] ) );
}
More info about saving custom fields in docs
If this wishlist is from a ready plugin, you could look in the plugin code to see how the plugin handles the saving wishlist data.
I have found:
WC_Wishlists_Wishlist::create_list($tittle));

How to update shipping calculations on change of custom shipping field in woocommerce?

I have added new custom shipping field ( select option ) to woocommerce checkout page.
I am using that for shipping calculations.
that works perfect as well. but issue is while I change values in that field it do not update instantly.
It gives correct calculations on next page, after page submit.
I need it to work as change in custom field.
How to trigger WooCommerce Ajax which updates shipping calculation on change of my custom field ?
This is actually extremely simple to do if you are adding your fields in the correct way(using the woocommerce_checkout_fields filter). The only thing you need to do is to add the classes address-field and update_totals_on_change like this:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['custom_field'] = array(
'label' => 'Custom field',
'required' => 1,
'class' => array ('address-field', 'update_totals_on_change' )
);
return $fields;
}

Woocommerce product tabs (show other products)

I am using woo commerce in my wordpress store online, I got a requirement that when a product is clicked and we navigate to the product detail page. I need to show the tabs into the product page like in the link
http://www.designbyhumans.com/shop/t-shirt/men/wolf-i/17011/
In this link two additional tabs are shown
1. Phone Cases
2. Art prints
Is there any way to achieve this is woo commerce?
Please help.
You can add custom tabs using woocommerce filters
The below code adds an extra tab called 'Product Description'.
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['desc_tab'] = array(
'title' => __( 'Product Description', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
}
function woo_new_product_tab_content() {
// The new tab content
$prod_id = get_the_ID();
echo '<p>'.get_post_meta($prod_id,'product_description',true).'</p>';
}
You would need to fetch the product data into these tabs. In the above example data from a custom field(product_description) is fetched into the tab.

how do i add the post list to a newly admin panel in wordpress?

I have created a new sub menu for a custom post type in wordpress admin, and I would like to add the a modified list of posts in there, i accordance to the newly variables that i have in my database; is there a function in wordpress to display the post lists? or do i have to do it manually?
add_action('admin_menu', 'my_plugin_menu');
function my_plugin_menu() {
add_submenu_page( 'edit.php?post_type=product', __( 'My product list', 'zeeneo-tenders' ), __( 'My product list', 'woo-tenders' ), 'manage_options', 'my-products', 'my_plugin_options');
}
function my_plugin_options() {
if (!current_user_can('manage_options')) {
wp_die( __('You do not have sufficient permissions to access this page.') );
}
echo "<h2>" . __( 'My products list', 'zeeneo-tenders' ) . "</h2>";
// Here is the code to display the list of posts in this new panel
}
WordPress doesn't have wp_dropdown_posts just like it has wp_dropdown_categories and wp_dropdown_users.
It has to be made by hand with get_posts or WP_Query, loop the results and make the <select><option>. For your use case, I think you'll need to use the Custom Fields parameters.
You can find a sample code at WPSE: Dropdown list of a custom post type.

Resources