显示航运选择有条件的多个包

问题描述 投票:0回答:1

所以,我使用的是插件创建我的产品的多个封装,每个类都有不同的运输方式。 e.g一个带邮寄交付和洗衣机是由面包车交付

我也使用过滤器除去我不需要航运选择,取决于航运类。例如皮带不需要由van选项的交付和洗衣机不需要邮寄选项提供

但是,我有麻烦两者结合时,有我的车不止一个项目。当前系统消除了所有的从购物车中的第一项不必要的航运选择,并适用于所有项目的筛选器。

我需要一种方法用于过滤器通过检查每个包中的类出货,而不是从购物车(被称为包之前)申请本身

这里是我的过滤器:(我重复刚才的过滤器不同的运输类)

add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_miele_freestanding_shipping_class', 10, 2);

function wf_hide_shipping_method_based_on_miele_freestanding_shipping_class($available_shipping_methods, $package)
{
    $hide_when_shipping_class_exist = array(
        4048 => array(
            'flat_rate:17',                 // set the options you want to hide when this class is in the basket
        'flat_rate:23',
        'flat_rate:22',
        'flat_rate:21',
        'local_pickup:25',
        'free_shipping:16'
        )
    );    

    $shipping_class_in_cart = array();
    foreach(WC()->cart->get_cart_contents() as $key => $values) {
       $shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
    }

    foreach($hide_when_shipping_class_exist as $class_id => $methods) {
        if(in_array($class_id, $shipping_class_in_cart)){
            foreach($methods as & $current_method) {
                unset($available_shipping_methods[$current_method]);
            }
        }
    }
    foreach($hide_when_shipping_class_not_exist as $class_id => $methods) {
        if(!in_array($class_id, $shipping_class_in_cart)){
            foreach($methods as & $current_method) {
                unset($available_shipping_methods[$current_method]);
            }
        }
    }
    return $available_shipping_methods;
}

这里是插件:

add_action('plugins_loaded', 'woocommerce_multiple_packaging_init', 106);
function woocommerce_multiple_packaging_init() {

    /**
     * Check if WooCommerce is active
     */
    if ( class_exists( 'woocommerce' ) || class_exists( 'WooCommerce' ) ) {

        if ( !class_exists( 'BE_Multiple_Packages' ) ) {

            // Include Necessary files
            require_once('class-settings.php');
            add_filter( 'woocommerce_cart_shipping_packages', array( 'BE_Multiple_Packages', 'generate_packages' ) );

            class BE_Multiple_Packages {

                /**
                 * Constructor.
                 */
                public function __construct() {
                    $settings_class = new BE_Multiple_Packages_Settings();
                    $settings_class->get_package_restrictions();
                    $this->package_restrictions = $settings_class->package_restrictions;
                }


                /**
                 * Get Settings for Restrictions Table
                 *
                 * @access public
                 * @return void
                 */
                function generate_packages( $packages ) {
                    if( get_option( 'multi_packages_enabled' ) ) {
                        // Reset the packages
                        $packages = array();
                        $settings_class = new BE_Multiple_Packages_Settings();
                        $package_restrictions = $settings_class->package_restrictions;
                        $free_classes = get_option( 'multi_packages_free_shipping' );

                        // Determine Type of Grouping
                        if( get_option( 'multi_packages_type' ) == 'per-product' ) :
                            // separate each item into a package
                            $n = 0;
                            foreach ( WC()->cart->get_cart() as $item ) {
                                if ( $item['data']->needs_shipping() ) {
                                    // Put inside packages
                                    $packages[ $n ] = array(
                                        'contents' => array($item),
                                        'contents_cost' => array_sum( wp_list_pluck( array($item), 'line_total' ) ),
                                        'applied_coupons' => WC()->cart->applied_coupons,
                                        'destination' => array(
                                            'country' => WC()->customer->get_shipping_country(),
                                            'state' => WC()->customer->get_shipping_state(),
                                            'postcode' => WC()->customer->get_shipping_postcode(),
                                            'city' => WC()->customer->get_shipping_city(),
                                            'address' => WC()->customer->get_shipping_address(),
                                            'address_2' => WC()->customer->get_shipping_address_2()
                                        )
                                    );

                                    // Determine if 'ship_via' applies
                                    $key = $item['data']->get_shipping_class_id();
                                    if( $free_classes && in_array( $key, $free_classes ) ) {
                                        $packages[ $n ]['ship_via'] = array('free_shipping');
                                    } elseif( count( $package_restrictions ) && isset( $package_restrictions[ $key ] ) ) {
                                        $packages[ $n ]['ship_via'] = $package_restrictions[ $key ];
                                    }
                                    $n++;
                                }
                            }

                        else :
                            // Create arrays for each shipping class
                            $shipping_classes = $other = array();
                            $get_classes = WC()->shipping->get_shipping_classes();
                            foreach ( $get_classes as $key => $class ) {
                                $shipping_classes[ $class->term_id ] = $class->slug;
                                $array_name = $class->slug;
                                $$array_name = array();
                            }
                            $shipping_classes['misc'] = 'other';

                            // Sort bulky from regular
                            foreach ( WC()->cart->get_cart() as $item ) {
                                if ( $item['data']->needs_shipping() ) {
                                    $item_class = $item['data']->get_shipping_class();
                                    if( isset( $item_class ) && $item_class != '' ) {
                                        foreach ($shipping_classes as $class_id => $class_slug) {
                                            if ( $item_class == $class_slug ) {
                                                array_push( $$class_slug, $item );
                                            }
                                        }
                                    } else {
                                        $other[] = $item;
                                    }
                                }
                            }

                            // Put inside packages
                            $n = 0;
                            foreach ($shipping_classes as $key => $value) {
                                if ( count( $$value ) ) {
                                    $packages[ $n ] = array(
                                        'contents' => $$value,
                                        'contents_cost' => array_sum( wp_list_pluck( $$value, 'line_total' ) ),
                                        'applied_coupons' => WC()->cart->applied_coupons,
                                        'destination' => array(
                                            'country' => WC()->customer->get_shipping_country(),
                                            'state' => WC()->customer->get_shipping_state(),
                                            'postcode' => WC()->customer->get_shipping_postcode(),
                                            'city' => WC()->customer->get_shipping_city(),
                                            'address' => WC()->customer->get_shipping_address(),
                                            'address_2' => WC()->customer->get_shipping_address_2()
                                        )
                                    );

                                    // Determine if 'ship_via' applies
                                    if( $free_classes && in_array( $key, $free_classes ) ) {
                                        $packages[ $n ]['ship_via'] = array('free_shipping');
                                    } elseif( count( $package_restrictions ) && isset( $package_restrictions[ $key ] ) ) {
                                        $packages[ $n ]['ship_via'] = $package_restrictions[ $key ];
                                    }
                                    $n++;
                                }
                            }

                        endif;

                        return $packages;
                    }
                }

            } // end class BE_Multiple_Packages

        } // end IF class 'BE_Multiple_Packages' exists

    } // end IF woocommerce exists

    add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'be_multiple_packages_plugin_action_links' );

    function be_multiple_packages_plugin_action_links( $links ) {
        return array_merge(
            array(
                'settings' => '<a href="' . get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=wc-settings&tab=multiple_packages">Settings</a>',
                'support' => '<a href="http://bolderelements.net/" target="_blank">Bolder Elements</a>'
            ),
            $links
        );
    }

} // end function: woocommerce_multiple_packaging_init

编辑:该插件相关文件class_settings;

<?php
/**
 * WooCommerce Multiple Packages Settings Page
 *
 * @author      Erica Dion
 * @category    Classes
 * @package     WooCommerce-Multiple-Packaging
 * @version     1.0
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'BE_Multiple_Packages_Settings' ) ) :
include_once( WC()->plugin_path().'/includes/admin/settings/class-wc-settings-page.php' );
class BE_Multiple_Packages_Settings extends WC_Settings_Page {

/**
 * Constructor.
 */
public function __construct() {
    global $woocommerce;

    $this->id = 'multiple_packages';
    $this->version = '1.1';
    $this->label = __( 'Multiple Packages', 'bolder-multi-package-woo' );
    $this->multi_package_restrictions = 'bolder_multi_package_woo_restrictions';

    $this->get_package_restrictions();

    parent::__construct();

    //add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 82 );
    //add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) );
    add_action( 'woocommerce_admin_field_shipping_restrictions', array( $this, 'output_additional_settings' ) );
    add_filter( 'woocommerce_admin_settings_sanitize_option_shipping_restrictions', array( $this, 'sanitize_shipping_restrictions_field' ), 15, 3 );
    //add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) );
    //add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) );
    //add_action( 'woocommerce_sections_' . $this->id, array( $this, 'additional_output' ) );

}

/**
 * Output the settings
 */
public function output() {

    $settings = $this->get_settings( );
    WC_Admin_Settings::output_fields( $settings );
}

/**
 * Save settings
 */
public function save() {

    $settings = $this->get_settings( );
    WC_Admin_Settings::save_fields( $settings );
    //$this->save_additional_settings();
}


/**
 * Get Page Settings
 *
 * @return array
 */
function get_settings( $current_section = '' ) {

    $shipping_classes = array();
    $get_classes = WC()->shipping->get_shipping_classes();
    foreach ($get_classes as $key => $class) {
        $shipping_classes[ $class->term_id ] = $class->name;
    }

    return apply_filters('woocommerce_multi_packages_settings', array(

        array(  
            'id'        => 'multi-packages_options',
            'type'      => 'title', 
            'title'     => __( 'Multiple Packages for Shipping', 'woocommerce' ), 
            'desc'      => __( 'Separate your customer\'s shopping cart into groups or per product to display multiple shipping select boxes', 'woocommerce' ), 
            ),

        array(
            'id'        => 'multi_packages_enabled',
            'type'      => 'checkbox',
            'title'     => __( 'Enable/Disable', 'bolder-multi-package-woo' ),
            'default'   => 'yes',
            'desc'      => __( 'Enable Multiple Shipping Packages', 'bolder-multi-package-woo' ),
            ),

         array(
            'id'        => 'multi_packages_type',
            'type'      => 'select',
            'title'     => __( 'Group By', 'bolder-multi-package-woo' ),
            'desc'      => __( 'How packages are defined, in groups or per product','bolder-multi-package-woo'),
            'default'   => 'shipping-class',
            'class'     => 'chosen_select',
            'desc_tip'  => true,
            'options'   => array(
                'shipping-class'    => __( 'Shipping Class', 'bolder-multi-package-woo'),
                'per-product'       => __( 'Product (individual)', 'bolder-multi-package-woo' ),
                )
            ),

        array(
            'id'        => 'multi_packages_free_shipping',
            'type'      => 'multiselect',
            'class'     => 'chosen_select',
            'title'     => __( 'Free Shipping Classes', 'bolder-multi-package-woo' ),
            'desc'      => '<em>' . __( '\'Free_Shipping\' method must be enabled', 'bolder-multi-package-woo' ) . '</em>',
            'default'   => __( 'Let me know when this item is back in stock!', 'bolder-multi-package-woo' ),
            'desc_tip'  => __( 'Exclude the selected shipping classes from being charged shipping', 'bolder-multi-package-woo' ),
            'options'   => $shipping_classes,
            ),

        array(
            'id'        => 'shipping_restrictions',
            'type'      => 'shipping_restrictions',
            ),

        array( 'type' => 'sectionend', 'id' => 'multi_packages' ),

        )
    );
}


/**
 * Print Out Additional Settings
 *
 * @return array
 */
function output_additional_settings( $current_section = '' ) {
    // get list of current shipping classes     
    $shipping_classes = array();
    $get_classes = WC()->shipping->get_shipping_classes();
    foreach ($get_classes as $key => $class) {
        $shipping_classes[ $class->term_id ] = $class->name;
    }

    $shipping_methods = WC()->shipping->get_shipping_methods();
    $total_shipping_methods = count( $shipping_methods ) + 1;
?>
        <style>#shipping_package_restrictions .restriction_rows th, #shipping_package_restrictions .restriction_rows td {text-align: center;} #shipping_package_restrictions .class_name {font-weight: bold;text-align: left;}</style>
            <tr valign="top" id="shipping_package_restrictions">
                <th scope="row" class="titledesc"><?php _e( 'Shipping Method Restrictions', 'bolder-multi-package-woo' ); ?>
                    <?php echo wc_help_tip( __('If separating by shipping class, select which shipping methods to use for each class','bolder-multi-package-woo') ); ?>
                    </th>
                <td class="forminp" id="<?php echo $this->id; ?>_restrictions">
                    <table class="restriction_rows widefat" style="width: 60%;min-width:550px;" cellspacing="0">
                        <thead>
                            <tr>
                                <th>&nbsp;</th>
                                <?php foreach ( $shipping_methods as $key => $method ) : ?>
                                <th><?php echo ( method_exists( $method, 'get_method_title' ) ) ? $method->get_method_title() : $method->get_title(); ?></th>
                                <?php endforeach; ?>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <td colspan="<?php echo $total_shipping_methods; ?>"><em><?php _e( 'If left blank, all active shipping methods will be used for each shipping class', 'bolder-multi-package-woo' ); ?></em></td>
                            </tr>
                        </tfoot>
                        <tbody class="shipping_restrictions">
<?php
                        $i = -1;
                        if( count( $shipping_classes ) > 0 ) :

                            foreach ( $shipping_classes as $id => $name ) :
?>
                            <tr>
                                <td class="class_name"><?php echo $name; ?></td>
                                <?php foreach ( $shipping_methods as $key => $method ) : ?>
                                <?php $checked = ( isset( $this->package_restrictions[ $id ] ) && in_array( sanitize_title( $key ), $this->package_restrictions[ $id ] ) ) ? 'checked="checked"' : ''; ?>
                                <td><input type="checkbox" name="restrictions[<?php echo $id; ?>][<?php echo sanitize_title( $key ); ?>]" <?php echo $checked; ?> /></td>
                                <?php endforeach; ?>
                            </tr>
<?php
                            endforeach;
                        else :
                            echo '<tr colspan="'.$total_shipping_methods.'">' . _e( 'No shipping classes have been created yet...', 'bolder-multi-package-woo' ) . '</tr>';
                        endif;
?>
                        </tbody>
                    </table>
                </td>
            </tr>
<?php
}


/**
 * Output Additional Information
 *
 * @return array
 */
function additional_output( $current_section = '' ) {
?>
        <style>.woocommerce_wrap { position:relative; padding-right: 300px; } a:hover { text-decoration: none; }</style>
        <div class="woocommerce_wrap">
        <div style="position:absolute;top:25px;right:10px;width:275px;display:block;padding:10px;border:1px solid #9dbc5a;border-radius:5px;">
            <h3>Bolder Elements also offers premium plugins! Here are a few you might be interested in...</h3>
            <div style="margin-bottom:25px;">
                <strong>Table Rate Shipping for WooCommerce</strong>
                <p>Has the ability to return multiple rates based on a variety of conditions such as location, subtotal, shipping class, weight, and more</p>
                <p style="text-align:right"><a href="http://codecanyon.net/item/table-rate-shipping-for-woocommerce/3796656?ref=bolderelements" style="color:#9dbc5a;" target="_blank">More Info</a></p>
            </div>
            <div style="margin-bottom:25px;">
                <strong>Bolder Fees for WooCommerce</strong>
                <p>Add extra flat rate, percentage, and even optional (checkbox) fees to the customer's cart. Can be based on subtotal, item details, and more</p>
                <p style="text-align:right"><a href="http://codecanyon.net/item/bolder-fees-for-woocommerce/6125068?ref=bolderelements" style="color:#9dbc5a;" target="_blank">More Info</a></p>
            </div>
            <div style="">
                <strong>Cart Based Shipping for WooCommerce</strong>
                <p>Allows you to change the shipping rate based on the customer's cart. Could be based on the subtotal, item count, or weight.</p>
                <p style="text-align:right"><a href="http://codecanyon.net/item/woocommerce-cart-based-shipping/3156515?ref=bolderelements" style="color:#9dbc5a;" target="_blank">More Info</a></p>
            </div>
        </div>
<?php
}


/**
 * Print Out Additional Settings
 *
 * @return array
 */
public function sanitize_shipping_restrictions_field( $value, $option, $raw_value ) {

    if ( isset( $_POST['restrictions'] ) ) {

        // Save settings
        $restrictions_safe = array();
        foreach ($_POST['restrictions'] as $key => $value) {
            $key_safe = intval( $key );
            foreach ($value as $key_method => $value_method) {
                $restrictions_safe[ $key_safe ][] = sanitize_title( $key_method );
            }
        }

        update_option( $this->multi_package_restrictions, $restrictions_safe );
        $this->get_package_restrictions();

        return $restrictions_safe;
    }
}


/**
 * Get Settings for Restrictions Table
 *
 * @access public
 * @return void
 */
function get_package_restrictions() {
    $this->package_restrictions = array_filter( (array) get_option( $this->multi_package_restrictions ) );
}

}

endif;

return new BE_Multiple_Packages_Settings();

最后这里有2张截图展示了这个问题:

  • 实施例1:Example 1
  • 实施例2:Example 2
woocommerce cart checkout shipping
1个回答
0
投票

一些帮助后,我的问题已经解决了。这个问题是我是从车而不是包叫我的航运类。正确的代码是这样的:

add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_miele_freestanding_shipping_class', 10, 2);

function wf_hide_shipping_method_based_on_miele_freestanding_shipping_class($available_shipping_methods, $package)
{
$hide_when_shipping_class_exist = array(
    4048 => array(                      // number is shipping class
        'flat_rate:17',                 // set the options you want to hide when this class is in the basket
    'flat_rate:23',
    'flat_rate:22',
    'flat_rate:21',
    'local_pickup:25',
    'free_shipping:16'
    )
);    

$shipping_class_in_cart = array();
foreach($package['contents'] as $key => $values) {
   $shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
}

foreach($hide_when_shipping_class_exist as $class_id => $methods) {
    if(in_array($class_id, $shipping_class_in_cart)){
        foreach($methods as & $current_method) {
            unset($available_shipping_methods[$current_method]);
        }
    }
}
foreach($hide_when_shipping_class_not_exist as $class_id => $methods) {
    if(!in_array($class_id, $shipping_class_in_cart)){
        foreach($methods as & $current_method) {
            unset($available_shipping_methods[$current_method]);
        }
    }
}
return $available_shipping_methods;
}

因此,与多个包工作时,这一点使用“$包[”内容“]”现在调用航运类

希望这会帮助别人那里!

© www.soinside.com 2019 - 2024. All rights reserved.