Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce:以编程方式更改默认配送方式_Php_Wordpress_Woocommerce - Fatal编程技术网

Php WooCommerce:以编程方式更改默认配送方式

Php WooCommerce:以编程方式更改默认配送方式,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,WooCommerce无法优先考虑表价运输方法,所以我尝试自己做,但失败惨重 我尝试在我认为已设置的点添加操作,但它不起作用。以下是我尝试过的方法(我只想使用available shipping methods数组中的第一个shipping方法): 我已经测试了$available\u methods和$selected\u method,它们都按预期显示(在页面中运行时),但是,一旦我将其添加到所选的操作woocommerce\u shipping\u method\u中,它似乎不会更新 e、

WooCommerce无法优先考虑表价运输方法,所以我尝试自己做,但失败惨重

我尝试在我认为已设置的点添加操作,但它不起作用。以下是我尝试过的方法(我只想使用available shipping methods数组中的第一个shipping方法):

我已经测试了
$available\u methods
$selected\u method
,它们都按预期显示(在页面中运行时),但是,一旦我将其添加到所选的操作
woocommerce\u shipping\u method\u
中,它似乎不会更新

e、 g.如果我通过另一个钩子运行此操作:

function output_to_footer() {
    if ( current_user_can( 'administrator' ) ) :

        $current_method = WC()->session->get( 'chosen_shipping_methods');
        $available_methods = WC()->shipping->packages[0]['rates'];
        $chosen_method = key($available_methods);
        WC()->session->set( 'chosen_shipping_methods', $chosen_method );        

        print "\n".'-----'."\n";
        print_r($current_method);
        print "\n".'-----'."\n";
        print_r($available_methods);
        print "\n".'-----'."\n";
        print_r($chosen_method);
        print "\n".'-----'."\n";

    endif;
}
add_action('print_footer_messages', 'output_to_footer');
看起来一切都在做它应该做的,但是当我从以下操作运行它时:
woocommerce\u shipping\u method\u selected
它“似乎”做了所有事情,但是shipping radio仍然设置为旧的shipping method

----- Array ( [0] => table_rate-5 : 70 ) ----- Array ( [table_rate-7 : 72] => WC_Shipping_Rate Object ( [id] => table_rate-7 : 72 [label] => Registered Australian Post (2 to 8 Business Days) [cost] => 4.0909 [taxes] => Array ( [1] => 0.40909 ) [method_id] => table_rate ) [table_rate-8 : 90] => WC_Shipping_Rate Object ( [id] => table_rate-8 : 90 [label] => Tracking and Freight Insurance [cost] => 20.055881818182 [taxes] => Array ( [1] => 2.0055881818182 ) [method_id] => table_rate ) [table_rate-5 : 70] => WC_Shipping_Rate Object ( [id] => table_rate-5 : 70 [label] => Nation-Wide Delivery (5 to 12 Business Days) [cost] => 0 [taxes] => Array ( ) [method_id] => table_rate ) [local_pickup] => WC_Shipping_Rate Object ( [id] => local_pickup [label] => Local Pickup [cost] => 0 [taxes] => Array ( ) [method_id] => local_pickup ) ) ----- table_rate-7 : 72 ----- ----- 排列 ( [0]=>表5:70 ) ----- 排列 ( [table_rate-7:72]=>WC_Shipping_rate对象 ( [id]=>表7:72 [标签]=>澳大利亚注册邮政(2至8个工作日) [成本]=>4.0909 [taxes]=>数组 ( [1] => 0.40909 ) [方法id]=>表格费率 ) [表8:90]=>WC\U运费对象 ( [id]=>表8:90 [标签]=>跟踪和货运保险 [成本]=>20.05588182 [taxes]=>数组 ( [1] => 2.0055881818182 ) [方法id]=>表格费率 ) [table_rate-5:70]=>WC_Shipping_rate对象 ( [id]=>表5:70 [标签]=>全国范围内交付(5至12个工作日) [成本]=>0 [taxes]=>数组 ( ) [方法id]=>表格费率 ) [local_Pick]=>WC_Shipping_Rate对象 ( [id]=>本地接收 [标签]=>本地拾取 [成本]=>0 [taxes]=>数组 ( ) [方法\u id]=>本地\u拾取 ) ) ----- 表11-7:72 -----
我一整天都在做这件事,没有找到解决办法。希望这里有人能帮上忙。

woocommerce\u shipping\u choosed\u method
是一个过滤器挂钩,而不是一个动作挂钩。请尝试以下代码

function reset_default_shipping_method( $method, $available_methods ) {

    // If the shipping method has been chosen don't do anything
    if ( ! empty( $method ) ) {
        return $method;
    }        

    // add code to set 'Table Rate' as the default shipping method 

    $method = 'table_rate-5';

    return $method;    
}

add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method', 10, 2);

注:我已经硬编码了
表_rate-5
,作为给你提供要点的默认方法,所以将其更改为所需的方法。理想情况下,您需要编写代码,以查看您想要设置为默认值的方法是否在
$available\u methods
中可用,然后相应地工作。

这就是我最后使用的方法,它按需工作:

/*=Use the shipping method filter to set the "selected" shipping method to 
 * the first (default) method in the list
**************************************************************************/
function oley_reset_default_shipping_method( $method, $available_methods ) {

    $method = key($available_methods);
    return $method;

}
add_filter('woocommerce_shipping_chosen_method', 'oley_reset_default_shipping_method', 10, 2);

(注意:这是有效的,因为我想要的运费实际上是列表中的第一个,但只是默认情况下没有被选中)

那么您希望“表运费”始终是默认的运费方式吗?您使用的是哪个版本的WooCommerce?还有一点需要注意的是,
woocommerce\u shipping\u selected\u method
是一个过滤器挂钩,而不是一个动作挂钩。因此,请使用
add_filter
而不是
add_action
并返回您的默认发货方法Table Rate已经是默认发货方法-我希望Table Rate选项1是默认的,而不是Table Rate选项3.PS。我是一个太多的新手,无法理解添加过滤器和添加操作之间的区别,所以当我听到你说什么的时候,我真的不明白。我也硬编码尝试并强制它,但它仍然不会更新。这看起来是重复我做过的,并且方法没有改变。好的,这确实有效。问题是我(和这里的代码)引用了动作挂钩“woocommerce\u shipping\u method\u Selected”。您为过滤器提供了代码:“woocommerce\u shipping\u selected\u method”-一旦我更改了挂钩,它就工作了。请更新你的答案,我会接受的。
/*=Use the shipping method filter to set the "selected" shipping method to 
 * the first (default) method in the list
**************************************************************************/
function oley_reset_default_shipping_method( $method, $available_methods ) {

    $method = key($available_methods);
    return $method;

}
add_filter('woocommerce_shipping_chosen_method', 'oley_reset_default_shipping_method', 10, 2);