如何在woocommerce

时间:2016-03-19 12:05:21

标签: wordpress woocommerce

我要求在woocommerce中订购产品。我创建了一个用于订购产品的自定义模板。在该模板中,用户必须输入尺寸,例如长度,宽度和高度。根据他们输入的尺寸,使用ajax执行一些计算,并向用户显示价格。下面有一个订购按钮。

(现在我创建了一个充当按钮<a id="order" href="site/checkout/?add-to-cart=96">Order</a>

的链接

我遇到的问题是:

  1. 如何处理基于用户输入维度生成的价格,并使用此生成价格的购物车和结帐的woocommerce正常工作。
  2. 非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

你可能会感兴趣的是两个钩子。第一个是在网站上显示价格时运行:

function woo_dm_get_price($price, $product) {    
    // do something with $price
    return $price;
}
add_filter( 'woocommerce_get_price', 'woo_dm_get_price', 999, 2 );

当项目放入购物车时,第二个是运行的:

function woo_dm_add_cart_item( $item_data, $item_key ){
    $price = 123;
    // do something to calculate new price
    $item_data['data']->set_price( number_format( $price, 2 ) );
    return $item_data;
}
add_filter( 'woocommerce_add_cart_item', 'woo_dm_add_cart_item', 999, 2 );

答案 1 :(得分:0)

请将此代码段添加到您的 function.php 中,它将根据您的需要工作。

add_filter( 'woocommerce_add_cart_item', 'wolf_options_add_cart_item', 20, 1 );
function wolf_options_add_cart_item( $cart_item ) {

    if (isset($cart_item['_other_options'])) :
        if( isset($cart_item['_other_options']['product-price']) )
            $extra_cost = floatval($cart_item['_other_options']['product-price']);

        $cart_item['data']->adjust_price( $extra_cost );

    endif;

    return $cart_item;
}

这将根据您的需要工作,您只需要更改从产品页面传输到此行中的数据

if( isset($cart_item['_other_options']['product-price']) )
            $extra_cost = floatval($cart_item['_other_options']['product-price']);

如果你不擅长代码也可以试试这个插件:WooCommerce Variable Pricing