我正在为电子商务商店写一个插件。
有时,这些项目的设置成本与订购相关
即。如果您需要50个蓝色上限,则需要支付(50 x unit_cost_price)+(1 x unit_setup_cost)
我需要找到一种方法来为购物车添加单独的费用以反映这些设置成本。
我想在计算购物车总数之前挂钩:
add_action( 'woocommerce_before_calculate_totals', 'update_custom_price', 1, 1 );
function update_custom_price( $cart_object ) {
foreach ( $cart_object->cart_contents as $cart_item_key => $value ) {
$price = my_custom_calculate_func( $value );
$value['data']->set_price($price);
}
}
function my_custom_calculate_func( $cart_item ) {
$product_id = $cart_item["product_id"];
$qty = $cart_item["quantity"];
if (!array_key_exists("_custom_options_one", $cart_item)) return $cart_item["price"];
$option_one = $cart_item["_custom_options_one"];
$option_two = $cart_item["_custom_options_two"];
// setup cost product id == 30786
if ($product_id != 30786) {
$request_url = site_url() . "/?post_type=product"
. "&add-to-cart=30786"
. "&quantity=1"
. "&custom_options_one=" . encodeURIComponent($option_one)
. "&custom_options_two=" . encodeURIComponent($option_two)
. "&custom_options_related_product_id=" . $product_id;
error_log($request_url);
$curl = new \MyApp\Http\Curl($request_url, array());
$response = $curl->__toString();
}
$price = get_price_custom($product_id, $option_one, $option_two, $qty);
$price = ($price == NULL) ? $cart_item["price"] : $price;
return $price;
}
但是,尽管生成了正确的URL,但该项目未添加到购物车中。
我可以手动使用该URL并输入该项,但它在钩子中不起作用。
有人能建议更好的方法来解决这个设置成本问题吗?