4

I am trying to create a plugin for WooCommerce that calculates the shipping costs depending on the product weight. I keep on getting this error:

Warning: Declaration of WC_Your_Shipping_Method::calculate_shipping($package) should be compatible with WC_Shipping_Method::calculate_shipping($package = Array) in C:\Users\Michelle\Dropbox\MEDIEINSTITUTET\Utveckling mot e-handel\WooCommerce\WC-kurs\wp-content\plugins\frakt\frakt.php on line 18

And this is my entire plugin:

<?php 
/* *
* Plugin Name: Min frakt modul
  Description: Ett plugin som beräknar frakt kostnad beroende på vikten
*/

/**
 * Check if WooCommerce is active
 */

if (!defined('ABSPATH')) {
    exit;
}

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    function your_shipping_method_init() {
        if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
            class WC_Your_Shipping_Method extends WC_Shipping_Method {
                /**
                 * Constructor for your shipping class
                 *
                 * @access public
                 * @return void
                 */
                public function __construct() {
                    $this->id                 = 'mitt-plugin-frakt'; // Id for your shipping method. Should be uunique.
                    $this->method_title       = __( 'Min frakt plugin' );  // Title shown in admin
                    $this->method_description = __( 
                    'Frakten skall beräknas på totalvikten av kundvagnen.<br>
                    Listan är följande:<br>
                    < 1kg = 30kr <br>
                    < 5kg = 60kr <br>
                    < 10kg = 100kr <br>
                    < 20kg = 200kr <br>
                    > 20kg = (Vikten * 10)kr' ); // Description shown in admin

                    $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
                    $this->title              = "My Shipping Method"; // This can be added as an setting but for this example its forced.

                    $this->init();
                }

                /**
                 * Init your settings
                 *
                 * @access public
                 * @return void
                 */
                function init() {
                    // Load the settings API
                    $this->init_form_fields(); // This is part of the settings API. Override the method to add your own settings
                    $this->init_settings(); // This is part of the settings API. Loads settings you previously 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 ) {

                   $alla_items = $package['contents'];
                   $total_weight = 0;
                    foreach ($alla_items as $orderline) {
                        $product_weight = $orderline['data']->weight;
                        $total_weight += $product_weight;
                    };

                    if($total_weight < 1) {
                        $cost = '30';
                    } elseif($total_weight > 1 && $total_weight < 5) {
                        $cost = '60';
                    } elseif($total_weight > 5 && $total_weight < 10) {
                        $cost = '100'; 
                    } elseif($total_weight > 10 && $total_weight < 20) {
                        $cost = '200'; 
                    } elseif($total_weight > 20) {
                        $cost = ($total_weight * 10);
                    } else {
                        echo 'ingen vikt';
                    }

                    echo $total_weight;

                    $rate = array( 
                        'id' => $this->id,
                        'label' => $this->title,
                        'cost' => $cost,
                        'calc_tax' => 'per_item'
                    );

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



    add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );

    function add_your_shipping_method( $methods ) {
        $methods['mitt-plugin-frakt'] = 'WC_Your_Shipping_Method';
        return $methods;
    }

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

?>

I really cant find the error... when I try the code I just see "Mici Shipping" but no price at all, meaning that somehow it doesnt read the if else statement? Thanks in advance for your help!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

9

Your class WC_Your_Shipping_Method extends WC_Shipping_Method which has that method already. Turn this:

public function calculate_shipping( $package ) 

Into this:

public function calculate_shipping( $package = array() )
Robert Vlasiu
  • 392
  • 2
  • 3
  • 12
  • Thank you!! Everything works now! I have another problem though if you are so super kind :p The plugin works but ONLY if its different products, its like it doesnt calculate if I have a different quantity on the same product? Any idea why? – michellemyers90 Apr 24 '20 at 11:02
  • Give me an example. So you have, for example, One Bike with quantity 1 and One Bike with Quantity 2? Like, the same item two times but with different quantity? – Robert Vlasiu Apr 24 '20 at 11:10
  • I am testing my plugin with a guitar website, so for example - 1 Electric Guitar (weight: 2 kg) - 3 Acoustic Guitar (each weight: 1 kg) The total weight should be than 5 kg, but it is 3kg, because it doesnt calculate the Acoustic Guitar kg 3 times, just once – michellemyers90 Apr 24 '20 at 11:12
  • Can you maybe try and see if the issue is that it maybe calculates just the acoustic guitars? try with 1 electric(2kg) and 2 acoustics(1kg * 2). your total weight should be 4. If the total weight is 3 it means it calculates 1 electric and 1 acoustic. If it calculates only the acoustics it should return 2. We can work on the problem after we get this clarified – Robert Vlasiu Apr 24 '20 at 11:21
  • Also can you post a var_dump of the "package" array? – Robert Vlasiu Apr 24 '20 at 11:23
  • I checked to see if it was a particular category of guitar, but the problem seems to apply to any product, the weight its calculated just once per product, no matter how many items I have in my cart. I will check with var_dump now – michellemyers90 Apr 24 '20 at 11:28
  • You'll have to extract the quantity from each package['contents'] elements and at total_weight do : `$total_weight += $product_weight * quantity;` – Robert Vlasiu Apr 24 '20 at 11:53