WooCommerce-Versandkostenberechnung im Plugin für benutzerdefinierte Versandmethoden [duplicate]

Lesezeit: 4 Minuten

Benutzeravatar von michellemyers90
michellemyers90

Ich versuche, ein Plugin für WooCommerce zu erstellen, das die Versandkosten in Abhängigkeit vom Produktgewicht berechnet. Ich bekomme immer wieder diesen Fehler:

Warnung: Deklaration von WC_Your_Shipping_Method::calculate_shipping($package) sollte kompatibel sein mit 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 auf Zeile 18

Und das ist mein gesamtes 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' );
}

?>

Ich kann den Fehler wirklich nicht finden … wenn ich den Code versuche, sehe ich nur “Mici Shipping”, aber überhaupt keinen Preis, was bedeutet, dass irgendwie die if else-Anweisung nicht gelesen wird? Vielen Dank im Voraus für Ihre Hilfe!

Ihre Klasse WC_Your_Shipping_Method erweitert WC_Shipping_Method, die diese Methode bereits hat. Drehen Sie das:

public function calculate_shipping( $package ) 

Das mögen:

public function calculate_shipping( $package = array() )

  • Vielen Dank!! Alles funktioniert jetzt! Ich habe jedoch ein anderes Problem, wenn Sie so super nett sind: p Das Plugin funktioniert, aber NUR, wenn es sich um verschiedene Produkte handelt. Es berechnet nicht, ob ich eine andere Menge für dasselbe Produkt habe? Irgendeine Idee warum?

    – michellemyers90

    24. April 2020 um 11:02 Uhr


  • Gib mir ein Beispiel. Sie haben also zum Beispiel ein Fahrrad mit Menge 1 und ein Fahrrad mit Menge 2? Zweimal derselbe Artikel, aber mit unterschiedlicher Menge?

    – Robert Vlasius

    24. April 2020 um 11:10 Uhr

  • Ich teste mein Plugin mit einer Gitarrenseite, also zB – 1 E-Gitarre (Gewicht: 2 kg) – 3 Akustikgitarren (jeweils Gewicht: 1 kg) Das Gesamtgewicht sollte über 5 kg liegen, es sind aber 3kg, weil es geht berechnet das Gewicht der Akustikgitarre nicht dreimal, sondern nur einmal

    – michellemyers90

    24. April 2020 um 11:12 Uhr

  • Können Sie vielleicht versuchen, zu sehen, ob das Problem darin besteht, dass es vielleicht nur die Akustikgitarren berechnet? versuchen Sie es mit 1 elektrisch (2 kg) und 2 akustisch (1 kg * 2). Ihr Gesamtgewicht sollte 4 betragen. Wenn das Gesamtgewicht 3 beträgt, bedeutet dies, dass 1 elektrisch und 1 akustisch berechnet wird. Wenn es nur die Akustik berechnet, sollte es 2 zurückgeben. Wir können an dem Problem arbeiten, nachdem wir dies geklärt haben

    – Robert Vlasius

    24. April 2020 um 11:21 Uhr

  • Können Sie auch einen var_dump des “Paket” -Arrays posten?

    – Robert Vlasius

    24. April 2020 um 11:23 Uhr

1435540cookie-checkWooCommerce-Versandkostenberechnung im Plugin für benutzerdefinierte Versandmethoden [duplicate]

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy