WooCommerce pull report with dynamically added fee - woocommerce

I am adding a fee to the users cart based on the cart totals.
add_action('woocommerce_cart_calculate_fees', function() {
if (is_admin() && !defined('DOING_AJAX')) {
return;
}
$cart_total = WC()->cart->get_cart_contents_total();
switch (true) {
case $cart_total <= 300:
$cart_fee = 4.99;
break;
case $cart_total > 300 && $cart_total <= 400:
$cart_fee = 6.64;
break;
case $cart_total > 400 && $cart_total <= 500:
$cart_fee = 8.30;
break;
case $cart_total > 500
$cart_fee = 9.96;
break;
default:
$cart_fee = 0;
}
if ($cart_total > 0) {
WC()->cart->add_fee(__('Shipping & Handling', 'txtdomain'), $cart_fee);
}
});
This works fine but now I need to get a report on these fees, daily, monthly, annually, etc.
Can someone point me in the right direction? Would I need to use custom SQL queries on a custom page or might there be a plugin to generate this type of report?

Related

How to calculate shipping charge based on product count and cart value?

In my Woocommerce store I want to calculate shipping charge based on product count and cart value eg if product count is more than 3 and cart value is less than 900 then shopping charge is 80. I want to do this type of calculations. Is it possible?
Try this one
add_filter('woocommerce_package_rates','shipping_charge_product_count_cart_value',100,2);
function shipping_charge_product_count_cart_value($rates, $package) {
global $woocommerce;
// Get cart items
$cart_items = $woocommerce->cart->get_cart();
$count_for_apply_shipping_charge = 0;
foreach ( WC()->cart->get_cart() as $cart_item ) {
if($cart_item['quantity'] > 3 && $cart_item['data']->get_price() < 900){
$count_for_apply_shipping_charge += 1;
}
}
if($count_for_apply_shipping_charge >= 1) {
foreach ($rates as $rate) {
//Set the price
$rate->cost = 80 * $count_for_apply_shipping_charge;
}
}
return $rates;
}
Are you looking for something like that? Here I add an example for the Florida state of the USA
if ( !empty($_POST['post_data']) ) {
parse_str($_POST['post_data'], $post_data);
if ( !empty($post_data['shipping_state']) ) {
$shipping_state = sanitize_text_field($post_data['shipping_state']);
if ($shipping_state == 'FL') {
$cart->add_fee('Florida Shipping', 25.00);
}
}
}

Is there a function to check the existence of a translation in WordPress?

I have developed a plugin that has over 400 words and phrases. There are a few phrases that are very important to the plugin's functionality, but it is difficult to get 90% of all translations completed [at wordpress.org] just to ensure the essential 20 are set.
I need to check if a translation for the plugin currently exists, otherwise present a fall-back for a limited number of languages.
Example:
<?php
if (wp_current_translation_exists())
{
$local_phrase = __('Some word', 'text-domain');
}
else
{
$language_code = substr(WP_LANG, 0, 2);
switch ($language_code)
{
case 'fr':
$local_phrase = 'Un mot';
break;
case 'de':
$local_phrase = 'Ein Wort';
break;
default:
$local_phrase = __('Some word', 'text-domain');
break;
}
}
?>
The missing function is shown as: wp_current_translation_exists().
It looks like I'll be answering my own question... As WordPress defaults to English, I can presume this translation exists and look to others with a word that will vary across languages.
<?php
$language_code = strtolower(substr(WP_LANG, 0, 2));
function wp_current_translation_exists($test_word = NULL, $text_domain = NULL)
{
global $language_code;
if ($test_word == NULL)
{
$test_word = 'Welcome';
}
return ($language_code == 'en' || __($test_word, $text_domain) != $test_word);
}
if (wp_current_translation_exists('Welcome', 'text-domain'))
{
$local_phrase = __('Some word', 'text-domain');
}
else
{
switch ($language_code)
{
case 'fr':
$local_phrase = 'Un mot';
break;
case 'de':
$local_phrase = 'Ein Wort';
break;
default:
$local_phrase = __('Some word', 'text-domain');
break;
}
}
?>
This isn't a great answer, but it is the best one available at the moment.

Is there any plugin that limits number of photos upload to a woo commerce product to 5

Is there any plugin to limit the number of images uploaded to a woo-commerce product
I have gone through different plugins but didn't find any that meets my requirement.
You can set limit to post for max attachments.
add_filter('wp_handle_upload_prefilter', 'limit_wp_handle_upload_prefilter');
function yoursite_wp_handle_upload_prefilter($file) {
// This bit is for the flash uploader
if ($file['type']=='application/octet-stream' && isset($file['tmp_name'])) {
$file_size = getimagesize($file['tmp_name']);
if (isset($file_size['error']) && $file_size['error']!=0) {
$file['error'] = "Unexpected Error: {$file_size['error']}";
return $file;
} else {
$file['type'] = $file_size['mime'];
}
}
if ($post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : false)) {
if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>4)
$file['error'] = "Sorry, you cannot upload more than four (5) image.";
}
return $file;
}

How to add an existing coupon to a Woocommerce Subscription?

My users can request a discount (or cancel their subscription) on their renewal order through a form.
Using the form submission hook I created a function that if the action is === "cancel" it cancels the subscription, but if the action is === "discount" I don't know how to apply a (existing) coupon to the subscription.
The coupon name is "40off-renewal"
add_action('frm_after_create_entry', 'csuf_cancel_subscription', 100, 2);
add_action('frm_after_update_entry', 'csuf_cancel_subscription', 100, 2);
function csuf_cancel_subscription($entry_id, $form_id)
{
if ($form_id == 3 && isset($_POST['item_meta'][11]) && isset($_POST['item_meta'][39]) && 'cancel' == $_POST['item_meta'][39]) {
$user = get_user_by('email', $_POST['item_meta'][11]);
if (!empty($user) && function_exists('wcs_get_users_subscriptions')) {
$users_subscriptions = wcs_get_users_subscriptions($user->ID);
if (is_array($users_subscriptions)) {
foreach ($users_subscriptions as $subscription) {
if ($subscription->has_status(array('active'))) {
$subscription->update_status('cancelled');
break;
}
}
}
}
}
}

WordPress Plugin Function

I had a developer write this plugin for me to add functionality to import bulk pricing to products through WP All Import, a while back. He has not been getting back to me regarding this. I had to delete the auto import that we had set up together and I don't remember how to use it to import the bulk pricing with the system he built. Could someone explain what the code indicate that I would do to use it?
<?php
/*
Plugin Name: WP All Import Woo Bulk Pricing Add-On
Description: Import data related to bulk pricing
Version: 1.0 */
//
function import_pricing_fields($id, $xml_node) {
// return;
$post_type = get_post_type($id);
if($post_type == 'product' || $post_type == 'product_variation' ){
$xml_node = (array) $xml_node;
$_product = wc_get_product( $id );
$number_of_prices = 3;
if( $_product->is_type( 'simple' ) ) {
update_post_meta($id,'_regular_price', $xml_node['price']);
update_post_meta($id,'_price', $xml_node['price']);
$pricing_array = array();
for($i=1;$i<$number_of_prices;$i++){
if(isset($xml_node['qb_'.$i]) && $xml_node['qb_'.$i] != 0){
$pricing_array[$i]['min'] = $xml_node['qb_'.$i];
if($i > 1){
$pricing_array[$i-1]['max'] = $xml_node['qb_'.$i]-1;
$pricing_array[$i]['max'] = "*";
} else {
$pricing_array[$i]['max'] = "*";
}
$pricing_array[$i]['val'] = $xml_node['price_'.$i];
}
}
$pricing_array = array_values($pricing_array);
if(!empty($pricing_array)) {
update_post_meta($id,'_wc_bulk_pricing_ruleset','_custom');
update_post_meta($id,'_wc_bulk_pricing_custom_ruleset', $pricing_array);
}
} else{
$var_id = wc_get_product_id_by_sku($xml_node['catalog']);
$variations = $_product->get_children();
if(!empty($variations)) {
foreach ($variations as $variation) {
$sku = get_post_meta($variation, '_sku', true);
if(!empty($sku) && $sku == $xml_node['catalog']) {
$var_id = $variation;
}
}
}
update_post_meta($var_id,'_regular_price', $xml_node['price']);
update_post_meta($var_id,'_price', $xml_node['price']);
if(isset($xml_node['qb_1'])) {
$pricing_array = array();
for($i=1;$i<$number_of_prices;$i++){
if(isset($xml_node['qb_'.$i]) && $xml_node['qb_'.$i] != 0){
$pricing_array[$i]['min'] = $xml_node['qb_'.$i];
if($i > 1){
$pricing_array[$i-1]['max'] = $xml_node['qb_'.$i]-1;
$pricing_array[$i]['max'] = "*";
} else {
$pricing_array[$i]['max'] = "*";
}
$pricing_array[$i]['val'] = $xml_node['price_'.$i];
}
}
$pricing_array = array_values($pricing_array);
if(!empty($pricing_array)) {
update_post_meta($var_id,'_wc_bulk_pricing_ruleset','_custom');
update_post_meta($var_id,'_wc_bulk_pricing_custom_ruleset', $pricing_array);
}
}
}
}
}
add_action('pmxi_saved_post','import_pricing_fields', 10, 2);
Could someone explain what the code indicate that I would do to use
it?
The function import_pricing_fields() is hooked into the 'pmxi_saved_post' action.
http://www.wpallimport.com/documentation/advanced/action-reference/
What this means is that every time you import a post using WP All Import it will also run the function import_pricing_fields().
However this is one check to pass before the majority of the code in this function will run, and that is on line 4 if($post_type == 'product' || $post_type == 'product_variation' ). Which simply means if the post is a product or product_variation continue running this code.
The rest of the code looks like it does what you said, import bulk pricing...

Resources