首页 文章

如何将控制台日志数据提供给WooCommerce Cart?

提问于
浏览
6

我正在使用第三方小工具,他们提供实时可用性,成本和立即预订按钮 . 当客户点击立即预订按钮时,它正在使用他们想要忽略的预订小工具 .

在做了一些谷歌研究之后,当一些人点击现在的书籍按钮时,我能够在控制台日志下获得正确的 Headers 和费用 .

$w.event.subscribe("item.book.click", function(item) { 
   console.log(item);
   console.log("Title " + item[3].Name + " (Date " + item[4].date  + ", Period " + item[4].period + ", Adults " + item[4].adults + ", Children " + item[4].children + ", Infants " + item[4].infants + ")");
   console.log("Price " + item[3].Availability.Cost);
});

WooCommerce简单产品现场演示:http://plugin.seminyak.holiday/product/the-layar/#/accom/66268

单击 Book Now 按钮,看到控制台记录它返回Title&Price的值,但是如何在 BUY NOWADD ITEM TO CART 等弹出按钮中触发控制台数据值 .

当用户点击 ADD ITEM TO CART 时,它会将数据添加到 /cart page + basket ,如果有人点击 BUY NOW 按钮,它将获取数据并重定向用户 /checkout 页面上的控制台 Headers 和价格

如果您有任何疑问,请告诉我 .

2 回答

  • 0

    正如在这个答案中所做的那样:

    Capturing javascript console.log?

    您可以在页面加载时捕获控制台日志,并根据输出触发事件 .

    (function(){
          var oldLog = console.log;
          console.log = function (message) {
              if(message.indexOf("Price") !== -1){
                var price = message.split(' ')[1]
                // Do something with the price...
              }
              oldLog.apply(console, arguments);
           };
        })();
    

    您可能需要做一些棘手的事情,因为描述和价格分为两个单独的日志 .

  • 0

    感谢您的支持和回复 . 经过大量研究后,我得到了以下代码的结果 .

    为了达到最终结果,我必须安装WC Fields Factory插件并使用控制台日志或变量def传递动态值

    步骤1:在.js文件下使用此代码,并确保在按钮点击时它会触发 .

    $w.event.subscribe("item.book.click", function(item) {
        $('[name="room_type"]')[0].value = item[3].Name;
        $('[name="date"]')[0].value = item[4].date;
        $('[name="nights"]')[0].value = item[4].period;
        $('[name="adults"]')[0].value = item[4].adults;
        $('[name="children"]')[0].value = item[4].children;
        $('[name="infants"]')[0].value = item[4].infants;
        $('[name="cost"]')[0].value = item[3].Availability.Cost;
        $('[name="add-to-cart"]')[0].click(); // Clicked on add to cart button
    });
    

    function.php代码动态获取自定义成本值 .

    /* Price Update Based On Plugin WC Field */
    
    function calculate_cart_total( $cart_object ) {
        $additionalPrice = 0;
    
        foreach ( $cart_object as $key => $valueArray ) {
            if(is_array($valueArray)) {
                foreach ( $valueArray as $k => $value ) {
                    if($value['wccpf_cost']) {
                        $additionalPrice = $value['wccpf_cost']['user_val'];
                    }
                }
            }
        }
    
        foreach ( WC()->cart->get_cart() as $key => $value ) {
            if( method_exists( $value['data'], "set_price" ) ) {
                $value['data']->set_price( $additionalPrice );
            } else {
                $value['data']->price = ($additionalPrice );
            }     
        }
    }
    add_action( 'woocommerce_before_calculate_totals', 'calculate_cart_total', 99 );
    
    /* before addto cart, clear cart values */
    add_filter( 'woocommerce_add_to_cart_validation', 'woo_custom_add_to_cart_before' );
    
    function woo_custom_add_to_cart_before( $cart_item_data ) {
    
        global $woocommerce;
        $woocommerce->cart->empty_cart();
    
        // Do nothing with the data and return
        return true;
    }
    

    在WC Fields Factory下我创建了room_type,日期,夜晚,成人,儿童,婴儿,成本字段和onclick值,因此动态获取 .

相关问题