Php 更改购物车项目价格,如果特定项目在购物车上
我正在使用WooCommerce会员资格,如果首次会员购买了特定产品,我希望将免费会员资格扩展到首次会员。我可以让其中的一些单独工作,但我有困难,使它全部走到一起 他们必须购买的物品也有销售价格,所以我也在检查日期,看看该物品是否在销售窗口中。那部分正在发挥作用 似乎当我添加参数来检查商品是否在购物车中时,整个过程都中断了。如果我分别运行这两个函数,它们都可以工作Php 更改购物车项目价格,如果特定项目在购物车上,php,wordpress,woocommerce,cart,membership,Php,Wordpress,Woocommerce,Cart,Membership,我正在使用WooCommerce会员资格,如果首次会员购买了特定产品,我希望将免费会员资格扩展到首次会员。我可以让其中的一些单独工作,但我有困难,使它全部走到一起 他们必须购买的物品也有销售价格,所以我也在检查日期,看看该物品是否在销售窗口中。那部分正在发挥作用 似乎当我添加参数来检查商品是否在购物车中时,整个过程都中断了。如果我分别运行这两个函数,它们都可以工作 function mps_registration_price($cart_object) { global $post,
function mps_registration_price($cart_object) {
global $post, $product;
// Bail if memberships is not active
if ( ! function_exists( 'wc_memberships' ) ) {
return;
}
$user_id = get_current_user_id();
date_default_timezone_set("America/Chicago");
$today = time();
// Check if user was never a member
if ( !wc_memberships_get_user_memberships( $user_id )) {
if( mps_check_for_product() ) {
foreach ( $cart_object->get_cart() as $cart_item ) {
// get the product id (or the variation id)
$id = $cart_item['data']->get_id();
if($id == 17) {
$salePrice = get_post_meta($id, '_sale_price', true);
$saleFrom = get_post_meta($id, '_sale_price_dates_from', true);
$saleTo = get_post_meta($id, '_sale_price_dates_to', true);
if( !empty($salePrice) && $today >= $saleFrom && $today <= $saleTo ) {
$new_price = $salePrice;
} else {
$new_price = get_post_meta($id, '_regular_price', true);
}
// Updated cart item price
$cart_item['data']->set_price( $new_price );
}
}
} else {
foreach ( $cart_object->get_cart() as $cart_item ) {
// get the product id (or the variation id)
$id = $cart_item['data']->get_id();
if($id == 17) {
$salePrice = get_post_meta($id, '_sale_price', true);
$saleFrom = get_post_meta($id, '_sale_price_dates_from', true);
$saleTo = get_post_meta($id, '_sale_price_dates_to', true);
if( !empty($salePrice) && $today >= $saleFrom && $today <= $saleTo ) {
$new_price = $salePrice + 50;
} else {
$new_price = get_post_meta($id, '_regular_price', true);
}
// Updated cart item price
$cart_item['data']->set_price( $new_price );
}
}
}
}
}
add_filter( 'woocommerce_before_calculate_totals', 'mps_registration_price', 10, 1 );
function mps_check_for_product() {
$product_id = 11;
$product_cart_id = WC()->cart->generate_cart_id( $product_id );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
return true;
} else {
return false;
}
}
add_action('woocommerce_before_cart', 'mps_check_for_product');
功能mps\u注册\u价格($cart\u对象){
全球$post$product;
//如果会员资格未激活,则保释
如果(!function_存在('wc_memberships')){
返回;
}
$user\u id=get\u current\u user\u id();
日期默认时区设置(“美国/芝加哥”);
$today=时间();
//检查用户是否从未成为成员
如果(!wc\u成员身份\u获取用户成员身份($user\u id)){
if(mps\U检查\U产品()){
foreach($cart\u object->get\u cart()作为$cart\u项目){
//获取产品id(或变体id)
$id=$cart_item['data']->get_id();
如果($id==17){
$salePrice=get_post_meta($id,'u sale_price',true);
$saleFrom=get_post_meta($id,'u sale_price_dates_from',true);
$saleTo=get_post_meta($id,''u sale''u price''u dates''u to',true);
如果(!empty($salePrice)&&$today>=$saleFrom&&$today设置价格($new价格);
}
}
}否则{
foreach($cart\u object->get\u cart()作为$cart\u项目){
//获取产品id(或变体id)
$id=$cart_item['data']->get_id();
如果($id==17){
$salePrice=get_post_meta($id,'u sale_price',true);
$saleFrom=get_post_meta($id,'u sale_price_dates_from',true);
$saleTo=get_post_meta($id,''u sale''u price''u dates''u to',true);
如果(!empty($salePrice)&&$today>=$saleFrom&&$today设置价格($new价格);
}
}
}
}
}
添加过滤器('woocommerce'u before\u calculate\u totals'、'mps\u registration\u price',10,1);
功能mps\U检查产品的功能(){
$product_id=11;
$product\U cart\U id=WC()->cart->生成\U cart\U id($product\U id);
$in_cart=WC()->cart->find_product_in_cart($product_cart_id);
如果($in_cart){
返回true;
}否则{
返回false;
}
}
添加操作(“购物车前的woocommerce”、“mps检查产品”);
更新(2018年10月)
这里的一切都很复杂,因为当客户尚未成为会员时,您只想在销售的特定产品(ID 17)中增加额外成本
你不需要两个分开的函数,你的第二个函数做得不对。如果你在第一个钩住的函数中调用它,就不需要钩住它
我使用WC\u-Product
方法而不是get\u-post\u-meta()
,完全重新访问了您的代码
另外,WC\u Product
方法是\u on\u sale()
管理所有东西,比如销售产品价格的起止日期。因此,您不需要在函数中获得这些信息
在您的代码中,当产品11在您的两个购物车循环中处于购物车中或不处于购物车中时,没有差异,因此,您可能需要在那里进行一些更改
因此,您的代码现在非常紧凑:
add_action( 'woocommerce_before_calculate_totals', 'mps_registration_price', 20, 1 );
function mps_registration_price( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Bail if memberships is not active
if ( ! function_exists( 'wc_memberships' ) ) return; // Exit
// Only for non members
$user_id = get_current_user_id();
if ( wc_memberships_get_user_memberships( $user_id ) ) return; // Exit
// First loop to check if product 11 is in cart
foreach ( $cart->get_cart() as $cart_item ){
$is_in_cart = $cart_item['product_id'] == 11 ? true : false;
}
// Second loop change prices
foreach ( $cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object (or the variation product object)
$product = $cart_item['data'];
// Method is_on_sale() manage everything (dates…)
// Here we target product ID 17
if( $product->is_on_sale() && $product->get_id() == 17 ) {
// When product 11 is not cart
if( ! $is_in_cart ){
$product->set_price( $product->get_sale_price() + 50);
}
}
}
}
代码位于活动子主题(或活动主题)的function.php文件或任何插件文件中
经过测试,效果良好。非常好!谢谢!我似乎无法让它反向工作,就像产品14在产品17之前就在购物车中一样。我假设逻辑仍然是一样的,但价格没有改变。