首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Woocommerce中用Toolset CRED表单数据覆盖购物车项目标题和价格

在Woocommerce中用Toolset CRED表单数据覆盖购物车项目标题和价格
EN

Stack Overflow用户
提问于 2018-09-24 19:58:30
回答 3查看 1.1K关注 0票数 1

我试图在WooCommerce手推车中覆盖一种产品的价格。问题是,我使用的是Divi,它包括ajax购物车更新。因此,当我重新加载签出页面时,我可以在1-2秒内看到覆盖的更改(在ajax加载期间),在此之后,购物车的详细信息再次被默认值覆盖。我试过每个人的想法,不同的钩子,延迟,优先级的改变,但都没有效果。

这是我的初始代码:

代码语言:javascript
运行
复制
add_filter( 'woocommerce_calculate_totals', 'custom_cart_items_prices', 1000, 1 )
function custom_cart_items_prices( $cart_object ) {

$title = $_GET['form-title'];
$money = $_GET['money'];

if ($title && $money) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

        // Iterating through cart items
        foreach ( $cart_object->get_cart() as $cart_item ) {

        // Get an instance of the WC_Product object
        $wc_product = $cart_item['data'];

        // Get the product name (WooCommerce versions 2.5.x to 3+)
        $original_name = method_exists( $wc_product, 'get_name' ) ? $wc_product->get_name() : $wc_product->post->post_title;

        // SET THE NEW NAME
        $new_name = $title;

        // Set the new name (WooCommerce versions 2.5.x to 3+)
        if( method_exists( $wc_product, 'set_name' ) )
            $wc_product->set_name( $new_name );
        else
            $wc_product->post->post_title = $new_name;
        }

    // Updated cart item price
    $cart_item['data']->set_price( $money ); 

}}

加载期间的图片(一切看起来都很棒):

当加载完成时:

Update --我也尝试了一种不同的方法,因为我使用了一个用create和Toolset插件制作的自定义表单,其中客户创建了一个具有自定义标题和价格的产品,在购物车中添加了一个默认的现有产品(并重定向到结帐页面)。我试图用提交的表格数据替换购物车的标题和价格。

这是我的代码:

代码语言:javascript
运行
复制
// Change the product ID
add_filter('cred_commerce_add_product_to_cart', function( $product_id, $form_id, $post_id ) {
    error_log($form_id);
    if( $form_id == 55 ) {
        if (!session_id()) {
            session_start();
        }
        $_SESSION['target_post_id'] = $post_id;
        if(isset($_POST['product-id'])){
            $product_id = $_POST['product-id'];
            $_SESSION['target_post_id'] = $post_id;
        }
    }
    return $product_id;
}, 20, 3);

// change the price in cart
add_action('woocommerce_before_calculate_totals', function(){
    session_start();
    if($form_id != 55 && !isset($_SESSION['target_post_id']))return;
    global $woocommerce;

    $post_id = $_SESSION['target_post_id']; 
    $price = 0;
    foreach ( $woocommerce->cart->get_cart() as $key => $cart_item ) {

        $product_id = $cart_item['data']->get_id();
        $adult_price = get_post_meta($product_id, 'wpcf-prezzo-1', true);
        $adult_num = get_post_meta($post_id, 'wpcf-adult-number', true);
        $child_price = get_post_meta($product_id, 'wpcf-prezzo-2', true);
        $child_num = get_post_meta($post_id, 'wpcf-children-number', true);

        $price = $adult_price * $adult_num + $child_price * $child_num; 
        $cart_item['data']->set_price($price); 
    }

}, 999);

但也不起作用。

如何获得提交的表单数据(自定义标题和价格)并覆盖Woocommerce中的购物车标题和价格?

EN

Stack Overflow用户

回答已采纳

发布于 2018-09-25 00:07:24

在使用带有自定义表单的工具集插件时,可以使用WC_Sessions存储产品ID并更改价格,请尝试以下操作:

代码语言:javascript
运行
复制
add_filter( 'cred_commerce_add_product_to_cart', 'cred_commerce_add_product_to_cart', 20, 3 );
function cred_commerce_add_product_to_cart( $product_id, $form_id, $post_id ) {
    if( $form_id == 55 && $post_id > 0 ) {
        WC()->session->set( 'cp_id', $post_id );
    }
    return $product_id;
}

add_filter( 'woocommerce_add_cart_item_data', 'filter_add_cart_item_data', 30, 4 );
function filter_add_cart_item_data( $cart_item_data, $product_id, $variation_id ){
    if ( WC()->session->get('cp_id') ) {
        $cp_id   = WC()->session->get('cp_id');
        $product = wc_get_product($cp_id);
        $cart_item_data['cp_price'] = $product->get_regular_price();
        $cart_item_data['cp_title'] = $product->get_title();
    }
    return $cart_item_data;
}

add_action( 'woocommerce_before_calculate_totals', 'custom_cart_items_price', 30, 1 );
function custom_cart_items_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Iterating through cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cat_item['cp_price']) && isset($cat_item['cp_title']) ){
            $cart_item['data']->set_price( $cat_item['cp_price'] ); 
            $cart_item['data']->set_name( $cat_item['cp_title'] );
        }
    }
}

代码在您的活动子主题(或活动主题)的function.php文件中。应该行得通。

一些related answers

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52486516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档