代码之家  ›  专栏  ›  技术社区  ›  Group Of Oceninfo

如何将控制台日志数据提供给WooCommerce购物车?

  •  5
  • Group Of Oceninfo  · 技术社区  · 8 年前

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

    在做了一些谷歌搜索之后,我能够得到正确的标题&当一些人单击“立即预订”按钮时,控制台日志下的成本。

    $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

    点击 按钮并查看控制台日志,它返回标题和;但如何在弹出按钮中触发控制台数据值,如 立即购买 & 将项目添加到购物车 .

    当用户点击 将项目添加到购物车 它会将数据添加到 /购物车页面+购物篮 立即购买 /结帐 带有控制台标题和价格的页面

    2 回复  |  直到 8 年前
        1
  •  0
  •   nbwoodward    8 年前

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

    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);
           };
        })();
    

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

        2
  •  0
  •   Group Of Oceninfo    8 年前

    谢谢你的支持和回复,在做了大量的研究后,我用下面的代码得到了我的结果。

    步骤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
    });
    

    作用动态获取自定义成本值的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;
    }