首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用购物车总项在woocommerce中添加自定义运输方法

使用购物车总项在woocommerce中添加自定义运输方法
EN

Stack Overflow用户
提问于 2016-10-28 18:12:22
回答 2查看 1.9K关注 0票数 1

我需要有人帮我完成这段代码。我一直试图为我的woocommerce网站做一个插件,以便根据订购的总项目自动添加运输成本。当我尝试下面的代码时,它会返回一个11 ie的值,而不管我订购的项目数量如何。理想的选择如下:

1-5包5 000 6-10包10 000 11-15包15 000 16-20 000包

下面是我的代码。

代码语言:javascript
复制
<?php
/*
Plugin Name: Your Shipping plugin
Plugin URI: http://woothemes.com/woocommerce
Description: Your shipping method plugin
Version: 1.0.0
Author: WooThemes
Author URI: http://woothemes.com
*/


/**
 * Check if WooCommerce is active
 */

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                 = 'your_shipping_method'; // Id for your shipping method. Should be uunique.
                    $this->method_title       = __( 'Your Shipping Method' );  // Title shown in admin
                    $this->method_description = __( 'Description of your shipping method' ); // 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 ) {
                    session_start();
                    global $woocommerce;

                    $carttotal = $woocommerce->cart->cart_contents_count;

                    /*
                        1-5 packs 5,000
                        6-10 packs 10,000
                        11-15 packs 15,000
                        16-20 packs 20,000

                    */
                    if($carttotal < 20){
                        $cost = 0;//Free delivery for above 20 packs
                    }else if($carttotal >= 16 && $carttotal <= 20){
                        $cost = 20000;
                    }else if($carttotal >= 11 && $carttotal <= 15){
                        $cost = 15000;
                    }else if($carttotal >= 6 && $carttotal <= 10){
                        $cost = 10000;
                    }else if($carttotal >= 5 && $carttotal <= 1){
                        $cost = 5000;
                    }else if($carttotal > 1){
                        $cost = 0;
                    }

                    $rate = array(
                        'id' => $this->id,
                        'label' => 'Shipping',
                        'cost' => $cost
                    );

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

                }
            }
        }
    }

    add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );

    function add_your_shipping_method( $methods ) {
        $methods['your_shipping_method'] = 'WC_Your_Shipping_Method';
        return $methods;
    }

    add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );
}

我得到了来自Credit:How to add custom shipping charge in woocommerce?的一些帮助,但现在我需要完成这个任务。提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-28 18:32:16

看起来您的calculate_shipping函数有一些问题

代码语言:javascript
复制
if($carttotal < 20){
     $cost = 0;//Free delivery for above 20 packs

应该是

代码语言:javascript
复制
if($carttotal > 20){

代码语言:javascript
复制
}else if($carttotal >= 5 && $carttotal <= 1){

则为“大于或等于5,小于或等于1”。

应该是

代码语言:javascript
复制
}else if($carttotal >= 1 && $carttotal <= 5){

另外,如果最后一个语句令人困惑,则if的其余部分应该涵盖了1和20+之间的所有数字

代码语言:javascript
复制
 }else if($carttotal > 1){
    $cost = 0;

你也许可以把那部分去掉。

票数 0
EN

Stack Overflow用户

发布于 2016-10-29 06:19:34

我只是从条件语句中解压缩函数。

代码语言:javascript
复制
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {

不要忘记在底部的最后的结束支撑。我杀了一些标准,即不让它成为插件,而只是制作了一个新文件(避免杂乱),以便包含在我的functions.php中。

因此,对于functions.php,我添加了

代码语言:javascript
复制
require get_template_directory() . '/inc/woo-shipping-methods.php';

和/inc/woo-航运-方法. And文件

有这个代码

代码语言:javascript
复制
<?php

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 
            ----------------------------------------------------------------*/
            public function __construct() {
                $this->id                 = 'your_shipping_method'; // Id for your shipping method. Should be uunique.
                $this->method_title       = __( 'Your Shipping Method' );  // Title shown in admin
                $this->method_description = __( 'Description of your shipping method' ); // 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
            ----------------------------------------------------------------*/
            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.
            ----------------------------------------------------------------*/

            public function calculate_shipping( $package ) {
                global $woocommerce;

                $carttotal = $woocommerce->cart->cart_contents_count;

                /*
                    1-5 packs 5,000
                    6-10 packs 10,000
                    11-15 packs 15,000
                    16-20 packs 20,000

                */
                if($carttotal > 20){
                    $cost = 0;//Free delivery for above 20 packs
                }else if($carttotal >= 16 && $carttotal <= 20){
                    $cost = 20000;
                }else if($carttotal >= 11 && $carttotal <= 15){
                    $cost = 15000;
                }else if($carttotal >= 6 && $carttotal <= 10){
                    $cost = 10000;
                }else if($carttotal <= 5 && $carttotal >= 1){
                    $cost = 5000;
                }

                $rate = array(
                    'id' => $this->id,
                    'label' => 'Shipping',
                    'cost' => $cost,
                );

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

            }
        }
    }
}

add_action( 'woocommerce_shipping_init', 'your_shipping_method_init' );

function add_your_shipping_method( $methods ) {
    $methods['your_shipping_method'] = 'WC_Your_Shipping_Method';
    return $methods;
}

add_filter( 'woocommerce_shipping_methods', 'add_your_shipping_method' );

干杯。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40310914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档