wordpress Woocommerce -创建免费送货的单个项目,同时使用UPS扩展

t5fffqht  于 2023-01-16  发布在  WordPress
关注(0)|答案(4)|浏览(153)

我正在woocommerce中使用UPS扩展。我需要为某些项目提供免费送货,绕过UPS计算器。似乎一个好方法是将该项目标记为“virtualL”,但该项目不需要送货。我需要为该项目要求送货。我已经能够自定义结帐页面并强制送货字段显示在虚拟项目上,但他们并不存储实际订单。
有人知道我如何创建一个自定义函数或修改woocommerce文件,以迫使结帐过程中需要航运的虚拟项目,而所有绕过UPS计算器?(其他项目需要计算航运)

dzhpxtsq

dzhpxtsq1#

我也有同样的问题,插件的作者回答我说,这是不能做到的,一个必须写的代码来做到这一点.

brvekthn

brvekthn2#

我刚刚遇到了这个问题,也许这会帮助一些人摆脱困境。对于所有我想要免费送货的产品,我做了虚拟产品。
然后我去了WooCommerce〉航运选项选项卡,选中框中说“收集航运地址,即使不需要”,这是在“航运目的地部分”。

cbeh67ev

cbeh67ev3#

我也正是需要这个,并有一个可行的解决方案:
首先在"Appearance"->"Editor"->"Theme Functions"中进入您的functions.php,并添加以下代码(我强烈建议首先使用“child theme“,这样您就不会修改主题的functions.php ...或者您可以选择create a custom plugin)。

/* Puts items with "free-shipping" shipping class into a different shipping package. */

    function free_woocommerce_cart_shipping_packages( $packages ) {

        // Reset the packages
        $packages = array();

        // Free items
        $freeshipping_items = array();
        $regular_items      = array();

        // Sort free from regular (replace "free-shipping" below with the shipping class slug you actually use).
        foreach( WC()->cart->get_cart() as $item ) {
            if( $item['data']->needs_shipping() ) {
                if( $item['data']->get_shipping_class() == 'free-shipping' ) {
                    $freeshipping_items[] = $item;
                }
                else {
                    $regular_items[] = $item;
                }
            }
        }

        // Put inside packages:
        if( $freeshipping_items ) {
            $packages[] = array(
                'ship_via'        => array( 'flat_rate' ),
                'contents'        => $freeshipping_items,
                'contents_cost'   => array_sum(wp_list_pluck($freeshipping_items, '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()
                )
            );
        }
        if( $regular_items ) {
            $packages[] = array(
             // 'ship_via'        => array( 'usps' ),
                'contents'        => $regular_items,
                'contents_cost'   => array_sum(wp_list_pluck($regular_items, '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()
                )
            );
        }

        return $packages;
    }

    add_filter('woocommerce_cart_shipping_packages', 'free_woocommerce_cart_shipping_packages');

其次,进入"Products"->"Shipping Classes",创建一个名为“Free Shipping“的航运类,并创建一个名为“free-shipping“的slug(slug名称是我们将绑定自定义函数的名称),然后创建“Add New Shipping Class“。
然后在"WooCommerce"->"Settings"->"Shipping"->"Flat Rate"中启用“Flat Rate“shipping,并在“Shipping Class Costs“部分下,在我们刚刚创建的“Free Shipping“类旁边放置一个值“0.00“。在我的例子中,我还将“Calculation Type“设置为“Per Class“。然后是“Save changes“。
最后,在"WooCommerce"->"Settings"->"Shipping"->"Shipping Options"中的“Shipping Methods“部分,确保“Flat Rate“的“Selection Priority“(在该名称的列中)高于您正在使用的任何其他运输方式,并且它是列表中的最后一个(您可以使用每个选项左侧的菜单图标拖动以重新排序)。“Save Changes“。
然后编辑一个你想要免运费的商品,转到“Product Data"->"Shipping“部分,将运费等级更改为“Free Shipping“(你创建的新等级)。然后“Update“该商品以保存更改。然后将其添加到你的购物车,并将其他一些有定期非免运费的商品添加到购物车。

测试

您现在应该看到免运费商品有自己的运费“包裹”行(“Shipping #1“和“Shipping #2“),与仍应收取运费的商品分开,即使您的购物车中有每种商品中的一些。
或者,你也可以尝试全付费插件称为“Per Product Shipping“.
或者这个free plugin may work也是(没有承诺)。

mspsb9vt

mspsb9vt4#

另一种方法,我相信可能会帮助你使一个项目有“免费”航运(和一种方法,我已经在过去使用),是创建一个优惠券,将折扣的产品(不要标记为“虚拟”),它的价格减去运费。
所以说,例如你的产品是$100和航运是$10(共$110),你然后创建一个优惠券,专门用于产品(s),将给予它$10折扣,使您的产品&航运总额$100.
我想你已经知道如何在woocommerce创建优惠券(如果你需要一些刷新如何这样做this page会有帮助).要使优惠券适用于特定的产品,你想有免费送货,去使用限制-〉产品,并指定特定的产品,你想有“免费”(或在这种情况下,折扣)送货.
为了保存用户在结账时输入优惠券代码的麻烦,您可以在functions.php中添加以下代码:

//Check for specific products then discount to adjust for 'free' shipping.

function my_free_shipping(){
//variables.
global $woocommerce;
$coupon_code = 'free-shiping';
$products= array('1', '2'); //products (ID's) to have discounted shipping.

//get the cart contents.
$cart_item = $woocommerce->cart->get_cart();
//loop through the cart items looking for the products in $products.
    foreach ($cart_item as $key => $item){
        //check if the cart items match to any of those in the array.
        if(in_array($item['product_id'], $products)){       
            //apply discount.
            $woocommerce->cart->add_discount(sanitize_text_field($coupon_code));
        }
    }
} 

add_action('init', 'my_free_shipping');

请注意,我还没有用UPS扩展测试这段代码,但是请随意修改和使用它。
我希望这篇文章对你有帮助!

相关问题