简体   繁体   English

WooCommerce:如何通过 URL 将产品价格添加到购物车?

[英]WooCommerce: how to add product price to cart via URL?

I found the ability to add to the cart through URL:我发现可以通过 URL 添加到购物车:

http://yoururl.com/cart/?add-to-cart=ID

I also found how to add quantity and attributes using this link, but can't find a way to add a price.我还找到了如何使用此链接添加数量和属性,但找不到添加价格的方法。
How to use this link to add a price to the cart?如何使用此链接向购物车添加价格?

If it is a simple product, the price is the one declared.如果是简单的产品,则以申报的价格为准。

If it is a variable product, you can use the variation id.如果是可变产品,则可以使用变体 ID。

> href=”http://yourdomain.com/?add-to-cart=88″

You can find more info about that, here.您可以在此处找到更多相关信息。 https://businessbloomer.com/woocommerce-custom-add-cart-urls-ultimate-guide/ https://businessbloomer.com/woocommerce-custom-add-cart-urls-ultimate-guide/

If is not working, please provide what code are you trying to use and more info about your issue.如果不起作用,请提供您尝试使用的代码以及有关您的问题的更多信息。

If you want to add the product to cart with a custom price, you can't via URL.如果您想以自定义价格将产品添加到购物车,则不能通过 URL。 You need to do it via PHP (if you are currently using JS, you will need to use an AJAX function like jQuery $.post and call a PHP function).您需要通过 PHP 来完成(如果您当前正在使用 JS,则需要使用 AJAX 函数,如 jQuery $.post 并调用 PHP 函数)。

In the PHP function you add the product:在 PHP 函数中添加产品:

function addtocart(){
   $cart_item_data['custom_price'] = 5678;
   WC()->cart->add_to_cart( $product_id, 1, 0, array(), $cart_item_data);
}

You will also need to modify the price in the cart您还需要修改购物车中的价格

// Change product price in the cart
add_action( 'woocommerce_before_calculate_totals', 'change_price_function' );

function change_price_function( $_cart ){
        // loop through the cart_contents
        foreach ( $_cart->cart_contents as $cart_item_key => $value ) {       
            $value['data']->set_price($value['custom_price']);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM