How to duplicate a Woocommerce product and include the WooThemes Per Product Shipping entries?
Tag : php , By : foxthrot
Date : March 29 2020, 07:55 AM
it helps some times I am using Woocommerce together with the WooThemes Per Product Shipping plugin. , 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);
}
}
}
|
(woocommerce) different shipping method per product
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Yes per product shipping is possible in woocommerce, have you read this? There are also multiple Wordpress plugins to achieve this if the default behaviour doesn't fit your needs. I'm not gonna link them though, it's easeri for you to choose if you Google them :)
|
Disable a specific of flat rate shipping method for a selected product in Woocommerce
Tag : php , By : Jay Crockett
Date : March 29 2020, 07:55 AM
it should still fix some issue This can be done with the following function code, where you will define the related product ID and the shipping method ID to be disabled. To find out the correct shipping method ID, just inspect with your browser dev tools the corresponding "flat rate" radio button under "value" argument. add_filter('woocommerce_package_rates', 'product_hide_shipping_method', 10, 2);
function product_hide_shipping_method( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return $rates;
// HERE set your Shipping Method ID to be removed
$shipping_method_id = 'flat_rate:12';
// HERE set your targeted product ID
$product_id = 37;
$found = false;
// Loop through cart items and checking for the specific product ID
foreach( $package['contents'] as $cart_item ) {
if( $cart_item['data']->get_id() == $product_id )
$found = true;
}
if( $found ){
// Loop through available shipping methods
foreach ( $rates as $rate_key => $rate ){
// Remove the specific shipping method
if( $shipping_method_id === $rate->id )
unset($rates[$rate->id]);
}
}
return $rates;
}
// HERE set your targeted product ID
$product_id = 37;
// Define your targeted product IDs
$product_ids = array(37, 39, 52, 58);
if( $cart_item['data']->get_id() == $product_id )
if( in_array( $cart_item['data']->get_id(), $product_ids ) )
$cart_item['data']->get_id()
$cart_item['product_id']
|
Restore a discounted product price for a specific shipping method in Woocommerce
Date : March 29 2020, 07:55 AM
will be helpful for those in need To restore cart items price based on a product category and on a specific chosen shipping method, try the following (for Woocommerce Dynamic Pricing): add_filter('woocommerce_add_cart_item_data', 'add_default_price_as_cart_item_custom_data', 50, 3 );
function add_default_price_as_cart_item_custom_data( $cart_item_data, $product_id, $variation_id ){
// HERE define your product category(ies)
$categories = array('t-shirts');
if ( has_term( $categories, 'product_cat', $product_id ) ) {
$product_id = $variation_id > 0 ? $variation_id : $product_id;
// The WC_Product Object
$product = wc_get_product($product_id);
// Get product default base price
$price = (float) $product->get_price();
// Set the Product default base price as custom cart item data
$cart_item_data['default_price'] = $price;
}
return $cart_item_data;
}
add_action( 'woocommerce_before_calculate_totals', 'restore_cart_item_price', 900, 1 );
function restore_cart_item_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE set the targeted Chosen Shipping method
$targeted_shipping_method = 'local_pickup';
// Get the chosen shipping method
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping_method_id = explode(':', reset($chosen_methods) );
$chosen_shipping_method = reset($chosen_shipping_method_id);
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item ) {
if( $targeted_shipping_method == $chosen_shipping_method && isset($cart_item['default_price']) ){
// Set back the default cart item price
$cart_item['data']->set_price($cart_item['default_price']);
}
}
}
|
Display the product shipping class on Woocommerce single product pages
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You are getting the WP_term object and not the term name⦠So instead use the following: add_action('woocommerce_single_product_summary', 'display_specific_shipping_class', 15 );
function display_specific_shipping_class(){
global $product;
// HERE define your targeted shipping class name
$defined_shipping_class = "Estimated Delivery in 7-15 days";
// Get the product shipping class WP_Term Object
$term = get_term_by( 'slug', $product->get_shipping_class(), 'product_shipping_class' );
if( is_a($term, 'WP_Term') && $term->name == $defined_shipping_class ){
echo '<p class="product-shipping-class">' . $term->name . '</p>';
}
}
|