Home > Software design >  I'm having a problem calculating shipping percentage in woocommerce
I'm having a problem calculating shipping percentage in woocommerce

Time:10-07

I'm trying to calculate 30% of the total shipping cost of a purchase on woocomerce in a fixed table, but I'm not getting it via the table because the fee percent doesn't work.

is there any code that does this in php?

CodePudding user response:

Try this as a plugin.

<?php

/**
* Plugin Name: Custom Shipping Charge method
* Description: Custom shipping method plugin for WooCommerce
*/

if ( ! defined( 'WPINC' ) ){
    die('security by preventing any direct access to your plugin file');
}

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

    function custom_shipping_charge_init() {
        if ( ! class_exists( 'WC_Custom_Shipping_Charge' ) ) {
            class WC_Custom_Shipping_Charge extends WC_Shipping_Method {
                /**
                 * Constructor for your shipping class
                 *
                 * @access public
                 * @return void
                 */
                public function __construct($instance_id = 0) {
                    $this->id                 = 'custom_shipping_charge'; // Id for your shipping method. Should be uunique.
                    $this->instance_id  = absint( $instance_id );
                    $this->method_title       = __( 'Custom Shipping Charge Method' );  // Title shown in admin
                    $this->method_description = __( 'Apply 30 percentage shipping charge on order total' ); // Description shown in admin

                    $this->init();

                    $this->title = !empty($this->settings['title']) ? $this->settings['title'] : 'Custom shipping charge plugin'; // This can be added as an setting but for this example its forced.
                    $this->enabled = isset($this->settings['enabled']) ? $this->settings['enabled'] : 'yes';

                }

                /**
                 * Init your settings
                 *
                 * @access public
                 * @return void
                 */
                function init() {

                    // Save settings in admin if you have any defined
                    add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
                }


                /**
                 * calculate_shipping function.
                 *
                 * @access public
                 *
                 * @param mixed $package
                 *
                 * @return void
                 */

                public function calculate_shipping( $package = array() ) {

                    global $woocommerce;

                    $carttotal = $woocommerce->cart->subtotal;

                    $fee = (float) (($carttotal * 30) / 100);

                    //Add the fee you calculated
                    $rate = array(
                        'id'       => $this->id,
                        'label'    => $this->title,
                        'cost'     => $fee,
                        'calc_tax' => 'per_item',
                        'package' => $package
                    );

                    // Register the rate
                    $this->add_rate( $rate );

                }
            }
        }
    }

    add_action( 'woocommerce_shipping_init', 'custom_shipping_charge_init' );

    function custom_shipping_charge_method( $methods ) {
        $methods['custom_shipping_charge'] = 'WC_Custom_Shipping_Charge';

        return $methods;
    }

    add_filter( 'woocommerce_shipping_methods', 'custom_shipping_charge_method' );
}

CodePudding user response:

he added but added as a new field with the 30% and not in the shipping field

enter image description here

and he is not calculating 30% on the cost, but adding on the product

  • Related