Duplicate per-product shipping extension information - WooCommerce - php

on my wordpress site I have WooCommerce and the WooCommerce per product shipping extension (https://docs.woocommerce.com/document/per-product-shipping/). I currently have an issue, this is that whenever I duplicate a woocommerce product the per-product shipping information is not copied. I have tried to find a solution for this and have got the following code however I have not been able to get it work, can anyone see what is going wrong in the following
add_action( 'woocommerce_product_duplicate', 'wdm_duplicate_pps_entries',10,2);
function wdm_duplicate_pps_entries( $new_id, $post) {
global $wpdb;
$id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : '';
if(!empty($id)) {
$query = "Select * From " . $wpdb->prefix . "woocommerce_per_product_shipping_rule
Where product_id = '" . $id . "'";
$result = $wpdb->get_results($query);
$table_name = $wpdb->prefix . "woocommerce_per_product_shipping_rule";
foreach($result as $single_result) {
$data = array('product_id' => $new_id, 'rule_country' => $single_result->rule_country, 'rule_state' => $single_result->rule_state,'rule_postcode' => $single_result->rule_postcode,'rule_cost' => $single_result->rule_cost,'rule_item_cost' => $single_result->rule_item_cost,'rule_order' => $single_result->rule_order);
$wpdb->insert($table_name,$data);
}
}
}
This is an example of the additonal info I am trying to copy -

UPDATE: Wordpress has fixed this bug

The below code working perfect.
add_action( 'woocommerce_duplicate_product', 'wdm_duplicate_pps_entries',10,2);
function wdm_duplicate_pps_entries( $new_id, $post) {
global $wpdb;
$id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : '';
if(!empty($id)) {
$query = "Select * From " . WC_PRODUCT_VENDORS_PER_PRODUCT_SHIPPING_TABLE . "
Where product_id = '" . $id . "'";
$result = $wpdb->get_results($query);
$table_name = WC_PRODUCT_VENDORS_PER_PRODUCT_SHIPPING_TABLE;
foreach($result as $single_result) {
$data = array('product_id' => $new_id, 'rule_country' => $single_result->rule_country, 'rule_state' => $single_result->rule_state,'rule_postcode' => $single_result->rule_postcode,'rule_cost' => $single_result->rule_cost,'rule_item_cost' => $single_result->rule_item_cost,'rule_order' => $single_result->rule_order);
$wpdb->insert($table_name,$data);
}
}
}

Related

Function redirect in WP boilerplate

I'm trying in the WP boilerplate in the public area to redirect after form submittion. But i'm getting a error.
Cannot modify header information - headers already sent by (output started at
public\partials\filename.php:
public function redirect_to_dashboard($value) {
if ( isset( $_POST) ) {
$redirect = get_permalink( $value );
wp_redirect($redirect);
exit;
}}
I calling the function with a valid permalink (same file public\partials\filename.php):
redirect_to_dashboard($escape_room_data->id)
I also added in the includes\filename-plugin.php the add_action
$this->loader->add_action( 'wp-loaded', $plugin_public, 'redirect_to_dashboard' );
Calling
global $wpdb;
if (!empty($_POST)) {
$code = $_POST['code'];
$like = "'%" . $wpdb->esc_like( $code ) . "%'";
$result = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_teptickets WHERE code = %s", $wpdb->esc_like($code)));
if (!empty($result)) {
if ($result->used == false) {
$table_name = $wpdb->prefix . "teptickets";
$wpdb->update( $table_name, array(
'used' => '1'
), array(
'id' => $result->id
) );
} else {
echo 'code already used';
$escape_room_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_tepescapes WHERE id = %s", $wpdb->esc_like($result->escape_room)));
redirect_to_dashboard($escape_room_data->id);
}
} else {
print_r('code not valid');
}}
Thank for helping!

Get all persons as a sum from all WooCommerce bookings

I use the official woocommerce booking plugin and I try do get the quantity of all persons that have booked a product.
for a single order that's no problem with:
if ( is_callable( 'WC_booking_Data_Store::get_booking_ids_from_order_id') ) {
$booking_data = new WC_booking_Data_Store();
$booking_ids = $booking_data->get_booking_ids_from_order_id( $order->get_id() );
}
foreach ( $booking_ids as $booking_id ) {
$booking = new WC_booking( $booking_id );
$person_count = array_sum( $booking->get_person_counts() );
$total_person_count .= 'Booking id: ' . $booking_id . ' Person Count: ' . $person_count . ' ';
}
but how can I collect the sum of all bookings? Hope you can help me
To get all "complete" bookings persons count use the following:
// get all bookings Ids with a status "complete"
$bookings_ids = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'wc_booking', // booking post type
'post_status' => 'complete',
'fields' => 'ids',
));
$persons_count = 0; // Initializing
$persons_html = '<table><tr><th>Booking id</th><th>Persons count</th></tr>'; // Initializing
// Loop through booking Ids
foreach ( $bookings_ids as $booking_id ) {
$booking = new WC_Booking( $booking_id ); // Get the WC_Booking instance object
$count = array_sum( $booking->get_person_counts() );
$persons_count += $count;
$persons_html .= '<tr><td>' . $booking_id . '</td><td>' . $count . '</td></tr>';
}
$persons_html .= '<tr><th><strong>Total count</th><td style="color:red">' . $persons_count . '</td></tr>';
// Output
echo $persons_html . '</table>';
Tested and works.
To make it lighter, you could replace:
$booking = new WC_Booking( $booking_id ); // Get the WC_Booking instance object
$count = array_sum( $booking->get_person_counts() );
simply by:
$count = array_sum( get_post_meta($booking_id, '_booking_persons', true) );
following your updated code, please try this
<?php
if ( is_callable( 'WC_booking_Data_Store::get_booking_ids_from_order_id') ) {
$booking_data = new WC_booking_Data_Store();
$booking_ids = $booking_data->get_booking_ids_from_order_id( $order->get_id() );
}
$total_count_data = array();
$total_count_persons = 0;
foreach ( $booking_ids as $booking_id ) {
$booking = new WC_booking( $booking_id );
$person_count = array_sum( $booking->get_person_counts() );
$total_count_persons+= $person_count;
$total_count_data[] = array(
'Booking' => $booking_id,
'Person' => $person_count
);
}

Limit output results of an array

I wanna limit the result to 10 elements (10 words). This is the code I got:
function ls_bp_hashtags_cloud() {
$args = array () ;
if ( bp_is_activity_component() ) {
$toHead = __( 'Popular Hashtags across network' , 'bp-hashtags' ) ;
}
if ( bp_is_user_activity() ) {
$toHead = __( 'Hashtags by user' , 'bp-hashtags' ) ;
$args[ 'user_id' ] = bp_displayed_user_id() ;
}
if ( bp_is_group_activity() || bp_is_group_home() ) {
$toHead = __( 'Hashtags in group' , 'bp-hashtags' ) ;
$args[ 'if_activity_item_id' ] = bp_get_current_group_id() ;
}
echo '<div align="right"><h5>' . $toHead . '</h5>' ;
echo ls_bp_hashtags_generate_cloud( $args ) ;
echo '</div>' ;
}
And thisone below is the ls_bp_hashtags_generate_cloud() function:
function ls_bp_hashtags_generate_cloud( $args = array() ) {
$hashtags = ls_bp_hashtags_get_hashtags( $args );
$defaults = array(
'smallest' => 10, 'largest' => 10, 'unit' => 'pt', 'number' => 0,
'format' => 'flat', 'separator' => ",\n\n", 'orderby' => 'count', 'order' => 'DESC',
'topic_count_text_callback' => 'default_topic_count_text',
'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1
);
$args = wp_parse_args( $args, $defaults );
extract( $args );
$tag_cloud = wp_generate_tag_cloud( $hashtags, $args );
$tag_cloud = '<div class="hashtags">' . $tag_cloud . '</div>';
return $tag_cloud;
}
I got this other one too, not sure if it's needed:
function ls_bp_hashtags_get_hashtags( $args = array() ) {
global $wpdb;
$bp = buddypress();
$link = $bp->root_domain . "/" . $bp->activity->slug . "/" . BP_ACTIVITY_HASHTAGS_SLUG . "/";
bp_hashtags_set_constants();
$data = maybe_unserialize( get_site_option( 'ls_bp_hashtags' ) );
if ( $data['style']['show_hashsymbol'] == '1' ) {
$hashtag_name = ' CONCAT( "#", hashtag_name)';
} else {
$hashtag_name = 'hashtag_name ';
}
$toWhere = ls_bp_hashtags_generate_query_limitations( $args );
$results = $wpdb->get_results( 'SELECT COUNT(hashtag_name) as count, '
. $hashtag_name . ' as name, '
. 'CONCAT("' . $link . '", hashtag_slug) as link
FROM ' . BP_HASHTAGS_TABLE . ' WHERE 1=1 ' . $toWhere . ' GROUP BY hashtag_name' );
return $results;
}
The first snippet I pasted here is in one file while the last 2 snippets are in another file.
I think I have to modify the function ls_bp_hashtags_generate_cloud(), the point is I don't know how to do that. I tried with array_slice() and with $sql = "SELECT * FROM bp_activity LIMIT 10"; I checked mysql database, my ashtags are located in bp_ashtags table, but when I open this table I see each hashtag is referred to be in bp_activity table.
This function gives me the ashtags cloud of my buddypress activity stream page, so it gives me all the ahstags present in the database, instead I wanna limit the result to just 10 #ashtags (it's a wordpress website). That said I'm really a novice so please if you can help me giving me the whole snippet to add and where to put it, I know almost nothing about php coding. If you need to know other things about this code, just tell me; thisone is an extract so I don't know if I'm missing something for the solution. Thank you in advance
Yes, I have to modify the function ls_bp_hashtags_generate_cloud(), the point is I don't know how to do that. I tried with array_slice() and with $sql = "SELECT * FROM bp_activity LIMIT 10"; I checked mysql database, my ashtags are located in bp_ashtags table, but when I open this table I see each hashtag is referred to be in bp_activity table. This function gives me the ashtags cloud of my buddypress activity stream page, so it gives me all the ahstags present in the database, instead I wanna limit the result to just 10 #ashtags (it's a wordpress website). That said I'm really a novice so please if you can help me giving me the whole snippet to add and where to put it.

How to duplicate a Woocommerce product and include the WooThemes Per Product Shipping entries?

I am using Woocommerce together with the WooThemes Per Product Shipping plugin.
The problem I have is that when using the “Duplicate” a product feature in Woocommerce, the PPS entries are not copied over to the new product.
Can anyone help in what I would need to add to the following file to get this to work?
https://github.com/woothemes/woocommerce/blob/master/includes/admin/class-wc-admin-duplicate-product.php
The PPS entries are stored in the following table:
http://www.awesomescreenshot.com/image/471866/f815997f78d9f2505919db880ffe35b5
You can use following code,
add_action( 'woocommerce_duplicate_product', 'wdm_duplicate_pps_entries',10,2);
function wdm_duplicate_pps_entries( $new_id, $post) {
global $wpdb;
$id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : '';
if(!empty($id)) {
$query = "Select * From " . $wpdb->prefix . "woocommerce_per_product_shipping_rule
Where product_id = '" . $id . "'";
$result = $wpdb->results($query);
$table_name = $wpdb->prefix . "woocommerce_per_product_shipping_rule";
foreach($result as $single_result) {
$data = array('product_id' => $new_id, 'rule_country' => $single_result->rule_country, 'rule_state' => $single_result->rule_state,'rule_postcode' => $single_result->rule_postcode,'rule_cost' => $single_result->rule_cost,'rule_item_cost' => $single_result->rule_item_cost,'rule_order' => $single_result->rule_order);
$wpdb->insert($table_name,$data);
}
}
}

Adding custom meta into wordpress search

I'm currently trying to add my theme posts custom meta values into search query, but it's not working for me. For example _My_meta_value_key3 is product code, but if i try to search it, wp don't find the match. I really want to make this without any plugins so any suggestions are welcome. Also, this code is currently located in theme functions.
function My_custom_search_query( $query ) {
if ($query->is_search()) {
$query->set('meta_query', array(
array(
'key' => '_My_meta_value_key',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
),
array(
'key' => '_My_meta_value_key2',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
),
array(
'key' => '_My_meta_value_key3',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
)
));
return $query;
};}
add_filter( 'pre_get_posts', 'My_custom_search_query');
I've spent an hour searching for a solution and there was nothing that works so I had to write the solution on my own. It is tested and working with Wordpress 5.3.1 and is the only solution that solves this problem I could find on the internet.
Place the following code in your functions.php file to hook into posts_clauses filter.
/**
* Include meta fields in search
*
* #author Mindaugas // meevly.com
* #link https://meevly.com/services/custom-wordpress-themes-and-plugins/
*
* #param array $pieces query pieces.
* #param WP_Query $args query object.
* #return array
*/
function mv_meta_in_search_query( $pieces, $args ) {
global $wpdb;
if ( ! empty( $args->query['s'] ) ) { // only run on search query.
$keywords = explode(' ', get_query_var('s'));
$escaped_percent = $wpdb->placeholder_escape(); // WordPress escapes "%" since 4.8.3 so we can't use percent character directly.
$query = "";
foreach ($keywords as $word) {
$query .= " (unique_postmeta_selector.meta_value LIKE '{$escaped_percent}{$word}{$escaped_percent}') OR ";
}
if ( ! empty( $query ) ) { // append necessary WHERE and JOIN options.
$pieces['where'] = str_replace( "((({$wpdb->posts}.post_title LIKE '{$escaped_percent}", "( {$query} (({$wpdb->posts}.post_title LIKE '{$escaped_percent}", $pieces['where'] );
$pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS unique_postmeta_selector ON ({$wpdb->posts}.ID = unique_postmeta_selector.post_id) ";
}
}
return $pieces;
}
add_filter( 'posts_clauses', 'mv_meta_in_search_query', 20, 2 );
And your query should look like the following. Please note that suppress_filters => false is mandatory! It will not work without it.
$search_posts_array = array(
'suppress_filters' => false,
's' => $keyword,
);
$search_results = get_posts( $search_posts_array );
Please do not hesitate to credit if I saved you tons of time! :)
Found this http://wpdevsnippets.com/extend-search-include-custom-fields-without-plugin/ :
function custom_search_where($pieces) {
// filter to select search query
if (is_search() && !is_admin()) {
global $wpdb;
$custom_fields = array('field1','field2'); // Array for custom meta fields
$keywords = explode(' ', get_query_var('s'));
$query = "";
foreach ($custom_fields as $field) {
foreach ($keywords as $word) {
$query .= "((mypm1.meta_key = '".$field."')";
$query .= " AND (mypm1.meta_value LIKE '%{$word}%')) OR ";
}
}
if (!empty($query)) {
// add to where clause
$pieces['where'] = str_replace("(((wp_posts.post_title LIKE '%", "( {$query} ((wp_posts.post_title LIKE '%", $pieces['where']);
$pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
}
}
return ($pieces);
}
add_filter('posts_clauses', 'custom_search_where', 20, 1);

Categories