【问题标题】:Enable 'Ship to a different address' check box for specific products in Woocommerce在 Woocommerce 中为特定产品启用“运送到其他地址”复选框
【发布时间】:2019-05-13 19:44:28
【问题描述】:

如何对特定产品强制“运送到不同地址”,而对普通产品禁用?我曾尝试使用此代码,但它对我不起作用。

add_filter( 'woocommerce_available_payment_gateways', 'conditionally_disable_cod_payment_method', 10, 1);
function conditionally_disable_cod_payment_method( $gateways ){

    $products_ids = array(793, 796);

    foreach ( WC()->cart->get_cart() as $cart_item ){

        $product_id = version_compare( WC_VERSION, '3.0', '<' ) ? $cart_item['data']->id : $cart_item['data']->get_id();
        if (in_array( $cart_item['product_id'], $products_ids ) ){

            add_filter( 'woocommerce_ship_to_different_address_checked', '__return_true' );

        }
    }
    return $gateways;
}

【问题讨论】:

    标签: php wordpress checkbox woocommerce checkout


    【解决方案1】:

    请尝试以下方法:

    add_filter( 'woocommerce_ship_to_different_address_checked', 'filter_cart_needs_shipping_address');
    function filter_cart_needs_shipping_address( $checked ) {
    
        $products_ids = array(793, 796);
        $found = $others_found = false;
    
        foreach ( WC()->cart->get_cart() as $cart_item ){
            if (in_array( $cart_item['data']->get_id(), $products_ids ) ){
                $found = true;
            } else {
                $others_found = true;
            }
        }
    
        if( $found && ! $others_found )
            $checked = true;
    
        return $checked;
    } 
    

    【讨论】:

      猜你喜欢
      • 2019-05-19
      • 2020-01-22
      • 2019-01-17
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 2019-09-12
      • 1970-01-01
      • 2021-06-08
      相关资源
      最近更新 更多