• Resolved Benjamin Pau

    (@soapking)


    I’m trying to add a remark on the minimum order amount for free delivery via the code snippet below but the value of $mov is not showing.

    /**
     * Add free delivery MOV 
     */
    add_action( 'wpo_wcpdf_before_document_notes', 'wpo_wcpdf_free_shipping', 10, 2 );
    function wpo_wcpdf_free_shipping ($document_type, $order) {
    	if ($document_type == 'invoice') {
    		$free_shipping_settings = get_option('woocommerce_free_shipping_1_settings');
    		$mov = $free_shipping_settings['min_amount'];
    		echo "<p>Free shipping minimum order amount: $mov</p>";
      }
    }
Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi, @soapking:

    Are you sure the meta key is woocommerce_free_shipping_1_settings? Please note that it could be something else, e.g. woocommerce_free_shipping_2_settings, woocommerce_free_shipping_3_settings, etc.

    Also, the value coming from 'min_amount' is a raw value, so you should format the output, this way:

    /**
     * Add free delivery MOV 
     */
    add_action( 'wpo_wcpdf_before_document_notes', 'wpo_wcpdf_free_shipping', 10, 2 );
    function wpo_wcpdf_free_shipping ( $document_type, $order ) {
    	if ( $document_type == 'invoice' ) {
    		$free_shipping_settings = get_option('woocommerce_free_shipping_2_settings');
    		$mov = wc_price( $free_shipping_settings['min_amount'] );
    		echo "<p>Free shipping minimum order amount: $mov</p>";
      }
    }
    Thread Starter Benjamin Pau

    (@soapking)

    I have different shipping zones with different Minimum Order Amount required for free shipping. I intend to pull those in regards to the location of the user as WooCommerce has determined with the Flat Rate shipping set in table. Is there a way to do this?

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @soapking:

    My apology for the late reply.

    It took me more time than I expected, to understand how WooCommerce stores this data and how to get it dynamically, based on the free shipping method selected by the customer, when you have more than one.

    That said, I have already written a code snippet for you to save the free minimum amount within the order data:

    /**
     * WooCommerce:
     * Save the minimum amount (if set) within the order data when a free shipping method is selected  
     */
    add_action( 'woocommerce_checkout_create_order', 'wpo_safe_free_shipping_amount_within_order_data' );
    function wpo_safe_free_shipping_amount_within_order_data( $order ) {
        $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
    	
        // If a free shipping method is selected...
        if ( strpos( $chosen_shipping_methods[0], 'free_shipping' ) !== false ) {
    		list( $shipping_name, $shipping_id ) = explode( ':', $chosen_shipping_methods[0] );
    		
            // ...get the minimum amount from its settings
            $free_shipping_settings = get_option( "woocommerce_{$shipping_name}_{$shipping_id}_settings" );
    		$min_amount = $free_shipping_settings['min_amount'];
    
            // If there is a minimum amount set, save it within the order data
            if ( ! empty( $min_amount ) ) {
                $order->update_meta_data( '_free_shipping_min_amount', $min_amount );
                $order->save();
            }
        }
    }

    …and this is the updated code snippet to display the minimum amount value, saved by the code above, on the PDF invoices :

    /**
     * PDF Invoices & Packing Slips for WooCommerce:
     * Show the free method minimum amount (if exists) before the document notes
     */
    add_action( 'wpo_wcpdf_before_document_notes', 'wpo_wcpdf_free_shipping_min_amount', 10, 2 );
    function wpo_wcpdf_free_shipping_min_amount ( $document_type, $order ) {
    	if ( $document_type == 'invoice' ) {
    		$min_amount = wc_price( $order->get_meta( '_free_shipping_min_amount' ) );
    		echo "<p>Free shipping minimum order amount: {$min_amount}</p>";
      }
    }

    Let me know whether it worked as expected, or you need more tweaks!

    Thread Starter Benjamin Pau

    (@soapking)

    Hi Yordan, thanks for your help in advance. I’ve tried the code snippets provided above but unfortunately the amount returned is 0. Seems like the updated code snippet to display the statement and amount is working. The free delivery amount data saved within the order is not for some reason.

    Thread Starter Benjamin Pau

    (@soapking)

    I’ve installed the plugin Store Toolkit for WooCommerce and didn’t found the meta key _free_shipping_min_amount created and saved to the test order.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @soapking,

    Please note that the code snippet will not save the value in existing orders, but new ones.

    That said, I have updated the second code snippet, so the text only appears if the _free_shipping_min_amount meta key exists in the order:

    /**
     * PDF Invoices & Packing Slips for WooCommerce:
     * Show the free method minimum amount (if exists) before the document notes
     */
    add_action( 'wpo_wcpdf_before_document_notes', 'wpo_wcpdf_free_shipping_min_amount', 10, 2 );
    function wpo_wcpdf_free_shipping_min_amount ( $document_type, $order ) {
    	if ( $document_type == 'invoice' && ( $min_amount = $order->get_meta( '_free_shipping_min_amount' ) ) ) {
    		$min_amount = wc_price( $min_amount );
    		echo "<p>Free shipping minimum order amount: {$min_amount}</p>";
      }
    }
    Thread Starter Benjamin Pau

    (@soapking)

    Hi Yordan, thank you for your patience with me so far. I think I may have created a misunderstanding, please give me the chance to rectify this.

    1. The customer is not able to select the shipping method.
    2. The flat rate option is selected by default if the Minimum Order Amount is not achieved.
    3. I intend to pull the Minimum Order Amount for Free Shipping of the Shipping Zone where the customer is based.

    I’ve modified the code snippet given to the one below to suit my purpose.

    add_action( 'woocommerce_checkout_create_order', 'save_free_shipping_amount_based_on_zone' );
    function save_free_shipping_amount_based_on_zone( $order ) {
        $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping_method = reset( $chosen_shipping_methods ); // Get the first selected method
    
        // Get the customer's shipping zone
        $customer_shipping_zone = WC_Shipping_Zones::get_zone_matching_package( array(
            'destination' => $order->get_shipping_address(),
        ) );
    
        // Find the free shipping method within the customer's zone
        $free_shipping_method = null;
        foreach ( $customer_shipping_zone->get_shipping_methods() as $method ) {
            if ( $method->id !== $chosen_shipping_method ) { // Skip the chosen method
                if ( $method->id === 'free_shipping' ) { // Check for Free Shipping specifically
                    $free_shipping_method = $method;
                    break;
                }
            }
        }
    
        // If a free shipping method is found in the zone, get its minimum amount
        if ( $free_shipping_method ) {
            $min_amount = $free_shipping_method->settings['min_amount'];
            $order->update_meta_data( '_free_shipping_min_amount', $min_amount );
            $order->save();
        }
    }
    

    Unfortunately I am unable to display the Minimum Order Amount with the updated second half of the code snippet that you have provided.

    /**
     * PDF Invoices & Packing Slips for WooCommerce:
     * Show the free method minimum amount (if exists) before the document notes
     */
    add_action( 'wpo_wcpdf_before_document_notes', 'wpo_wcpdf_free_shipping_min_amount', 10, 2 );
    function wpo_wcpdf_free_shipping_min_amount ( $document_type, $order ) {
    	if ( $document_type == 'invoice' ) {
    		$min_amount = wc_price( $order->get_meta( '_free_shipping_min_amount' ) );
    		echo "<p>Free shipping minimum order amount: {$min_amount}</p>";
      }
    }

    Please advise where I have gone wrong. Thanks in advance Yordan.

    Thread Starter Benjamin Pau

    (@soapking)

    Is it possible?

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @soapking,

    Sorry for not to reply sooner!

    Answering your question, yes, it should be possible, but although I tried helping you to achieve what you wanted, as a courtesy, this is an advance customization that we cannot offer within our free support.

    Hope you understand.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Adding Free Delivery Minimum Order Amount’ is closed to new replies.