小编Eya*_*tan的帖子

WooCommerce添加到购物车验证:阻止添加到购物车

大家好,我对woocommerce遇到了问题,我试图修复几天。

即时通讯为一个人创建了一个网站,他希望我在产品页面上添加自定义输入,我自己无法做到这一点,所以我在网上使用了自由职业者。

在我的产品页面上

加入购物车按钮,数量输入和日期输入。

“日期输入”是自由职业者所做的。

问题是,当$ _REQUEST ['thedate']为空时,将弹出自定义错误,但产品仍添加到购物车中。

代码:(functions.php)

function add_the_date_validation() { 
if ( empty( $_REQUEST['thedate'] )) {
    wc_add_notice( __( 'Please enter a date.', 'woocommerce' ), 'error' );
    return false;
}
return true;
}
add_action( 'woocommerce_add_to_cart_validation', 'add_the_date_validation', 10, 5 );    
Run Code Online (Sandbox Code Playgroud)

视图:http : //i.stack.imgur.com/a75P6.png

  • 如何防止产品将其添加到购物车?

自由职业者执行的其他代码:

function save_add_the_date_field( $cart_item_key, $product_id = null, $quantity= null, $variation_id= null, $variation= null ) {
if( isset( $_REQUEST['thedate'] ) ) {
    WC()->session->set( $cart_item_key.'_the_date', $_REQUEST['thedate'] );
}
}

add_action( 'woocommerce_add_to_cart', 'save_add_the_date_field', 1, 5 );

function render_meta_on_checkout_order_review_item( …
Run Code Online (Sandbox Code Playgroud)

php wordpress woocommerce

2
推荐指数
1
解决办法
1万
查看次数

防止 WooCommerce 订单状态从特定状态更改/更改为特定状态

您好,我正在尝试在 mu 插件中创建一个功能,以防止某些用户将订单状态从特定订单状态更改为特定订单状态。

我到处寻找,尝试了很多不同的方法,但似乎没有任何效果。

实际上该函数是使用woocommerce_order_status_changed动作钩子运行的。问题是这个钩子在订单状态已经改变之后运行,这导致了无限循环。

我发现的最有用的钩子似乎是woocommerce_before_order_object_save.

我在 WooCommerce Github 上发现了“添加附加参数以防止调用‘woocommerce_order_status_changed’”有用的相关线程。
我尝试使用@kloon代码片段解决方案

add_filter( 'woocommerce_before_order_object_save', 'prevent_order_status_change', 10, 2 );
function prevent_order_status_change( $order, $data_store ) {
    $changes = $order->get_changes();
    if ( isset( $changes['status'] ) ) {
        $data = $order->get_data();
        $from_status = $data['status'];
        $to_status = $changes['status'];

        // Do your logic here and update statuses with CRUD eg $order->set_status( 'completed' );
                // Be sure to return the order object

    }
    return $order;
}
Run Code Online (Sandbox Code Playgroud)

$changes变量始终是一个空数组。

我尝试使用 …

php wordpress orders woocommerce hook-woocommerce

1
推荐指数
1
解决办法
1720
查看次数

标签 统计

php ×2

woocommerce ×2

wordpress ×2

hook-woocommerce ×1

orders ×1