Supporto » Funzionalità = Plugin » Spedizione gratuita per singolo prodotto Woocommerce

  • Ciao a tutti spero riusciate ad aiutarmi.
    Vorrei inserire un hook di codice per abilitare una spedizione gratuita per un singolo prodotto by ID.
    Questo è il mio file functions.php

    [aggiunta hook di codice woocommerce per creare un free shipping by ID in tema child]
    partendo da questo file functions.php
    <?php
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    // AUTO GENERATED - Do not modify or remove comment markers above or below:
    
    if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
        function chld_thm_cfg_locale_css( $uri ){
            if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
                $uri = get_template_directory_uri() . '/rtl.css';
            return $uri;
        }
    endif;
    add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
    
    if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
        function chld_thm_cfg_parent_css() {
            wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( 'font-awesome' ) );
        }
    endif;
    add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
    
    // END ENQUEUE PARENT ACTION
    
    /*nascondo prezzo spediazione quando la spedizione è gratis*/
    add_filter( 'woocommerce_package_rates', 'nascondi_spedizione_quando_gratis', 10, 2 );
    function nascondi_spedizione_quando_gratis( $rates, $package ) {
    $all_free_rates = array();
    foreach ( $rates as $rate_id => $rate ) {
    if ( 'free_shipping' === $rate->method_id ) {
    $all_free_rates[ $rate_id ] = $rate;
    break;
    }
    }
    if ( empty( $all_free_rates )) {
    return $rates;
    } else {
    return $all_free_rates;
    }
    }
    
    /* Avviso nel carrello per avere la spedizione gratuita */
    add_filter( 'flexible_shipping_free_shipping_notice_text', 'wpdesk_flexible_shipping_free_shipping_notice_text', 10, 2 ); function wpdesk_flexible_shipping_free_shipping_notice_text( $notice_text, $amount ) { return sprintf( 'Ti mancano ancora %1$s per avere la spedizione gratuita! %2$sContinua gli acquisti%3$s', wc_price( $amount ), '<a class="button" target="_blank" href="' . esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ) . '">', '</a>' ); }

    Avrei necessità di aggiungere una spedizione gratuita attraverso ID del prodotto, ho trovato questa porzione di codice che mi sembra possa fare a caso mio…

    /* spedizione gratuita by ID */
    function wcs_my_free_shipping( $is_available ) {    global $woocommerce;        // set the product ids that are eligible    $eligible = array( '360' );        // get cart contents    $cart_items = $woocommerce->cart->get_cart();        // loop through the items looking for one in the eligible array    foreach ( $cart_items as $key => $item ) {    if( in_array( $item['product_id'], $eligible ) ) {    return true;    }    }        // nothing found return the default value    return $is_available;   }   add_filter( 'woocommerce_shipping_free_shipping_is_available', 'wcs_my_free_shipping', 20 );

    ora in teoria dovrei sostituire product_id con l’ID del mio prodotto.
    Questa porzione di codice dove dovrei incollarla?
    Potrei avere dei problemi nel testarlo?
    Grazie mille per l’aiuto!

    La pagina su cui ho bisogno di aiuto: [devi essere connesso per vedere il link]

  • Il topic ‘Spedizione gratuita per singolo prodotto Woocommerce’ è chiuso a nuove risposte.