WooCommerce – Wie erhalte ich die Versandklasse von Warenkorbartikeln in der Funktion compute_shipping?

Lesezeit: 2 Minuten

Benutzer-Avatar
Ban-Geoengineering

Ich habe ein WooCommerce-Plugin erstellt, das den kostenlosen Versand für Abonnenten ermöglicht. Es scheint nach einem kürzlichen WooCommerce-Upgrade kaputt gegangen zu sein.

Insbesondere scheint das Problem, dass die Versandklasse der Warenkorbartikel möglicherweise nicht korrekt abgerufen wird.

Hier ist mein compute_shipping-Code – kann jemand sagen, was falsch ist?

/**
 * Add free shipping option for customers in base country with an active subscription,
 * but only if the cart doesn't contain an item with the 'heavy-item-shipping-class'.
 *
 * @access public
 * @param mixed $package
 * @return void
 */
public function calculate_shipping($package) {

    global $woocommerce;

    // Check country and subscription status
    if (is_user_in_base_country() && does_user_have_active_subscription()) {

        // This flag will be set to TRUE if cart contains heavy items
        $disable_free_shipping = FALSE;

        // Get cart items
        $cart_items = $package['contents'];

        // Check all cart items
        foreach ($cart_items as $cart_item) {

            // Get shipping class
            $shipping_class = $cart_item['data']->shipping_class; // *** IS THIS THE RIGHT WAY TO GET THE SHIPPING CLASS ??? ***

            // If heavy item, set flag so free shipping option is not made available
            if ($shipping_class === 'heavy-item-shipping-class') {

                // Set flag
                $disable_free_shipping = TRUE;

                // Enough
                break;

            }

        }

        // If appropriate, add the free shipping option
        if ($disable_free_shipping === FALSE) {

            // Create the new rate
            $rate = array(
                'id' => $this->id,
                'label' => "Free Shipping",
                'cost' => '0',
                'taxes' => '',
                'calc_tax' => 'per_order'
            );

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

        }
        else {

            // Doesn't qualify for free shipping, so do nothing

        }

    }

}

AKTUALISIEREN
Ich habe mir die angeschaut %package array und bemerkt, dass es jetzt die Versandklasse unter enthält [shipping_class:protected]. (Früher muss das gewesen sein [shipping_class].) Ist es möglich, diese Daten zu extrahieren? Wenn nicht, was ist der richtige Weg, es zu tun?

  • Was ist $Paket? ist das ein WC-Ordnungsobjekt?

    – Mixdev

    13. Mai 2020 um 14:29 Uhr

  • Nein, es ist nur ein einfaches Array, das von WooCommerce an die übergeben wird calculate_shipping Funktion als Parameter. Entsprechend docs.woocommerce.com/wc-apidocs/class-WC_Shipping.html es ist ein mehrdimensionales Array von Einkaufswagenartikeln, für die der Versand berechnet werden soll.

    – Ban-Geoengineering

    13. Mai 2020 um 18:27 Uhr

Ich habe die Lösung gefunden. Jetzt scheint es, dass der einzige Weg, die Versandklasse eines Produkts/Artikels zu erfahren, darin besteht, anzurufen get_shipping_class() darauf.

Also, in meinem Code-Snippet oben habe ich geändert …

$shipping_class = $cart_item['data']->shipping_class;

…zu…

$shipping_class = $cart_item['data']->get_shipping_class();

Hoffentlich hilft dies jemand anderem. 🙂

1383650cookie-checkWooCommerce – Wie erhalte ich die Versandklasse von Warenkorbartikeln in der Funktion compute_shipping?

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

Privacy policy