Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将标准WooCommerce产品小部件添加到日志时出错_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 将标准WooCommerce产品小部件添加到日志时出错

Php 将标准WooCommerce产品小部件添加到日志时出错,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在使用一个代码来更改添加到购物车的产品的“添加到购物车”按钮的文本和样式。(我的主题的所有CSS样式) 据我所知,购物车没有在管理面板中初始化,因为这里不需要它 如何解决这个问题 我将为你的帮助而高兴 在特殊情况下,或者由于插件/主题的组合,您确实会遇到错误消息 解决方案是在执行下一段代码之前添加多个检查,这样可以防止错误消息 例如,您可以将现有的新产品\u按钮\u文本回调函数替换为: 功能新产品按钮文本($text,$product){ 如果(is_admin())返回$text; 如果(

我正在使用一个代码来更改添加到购物车的产品的“添加到购物车”按钮的文本和样式。(我的主题的所有CSS样式)

据我所知,购物车没有在管理面板中初始化,因为这里不需要它

如何解决这个问题


我将为你的帮助而高兴

在特殊情况下,或者由于插件/主题的组合,您确实会遇到错误消息

解决方案是在执行下一段代码之前添加多个检查,这样可以防止错误消息

例如,您可以将现有的
新产品\u按钮\u文本
回调函数替换为:

功能新产品按钮文本($text,$product){
如果(is_admin())返回$text;
如果($product->is_type('simple')&&&$product->is_purchable()&&&$product->is_in_stock()){
//厕所推车
如果(WC()->cart){
//推车
$cart=WC()->cart;
//如果购物车不是空的
如果(!$cart->为空(){
//在购物车中查找产品
如果($cart->find_product_in_cart($cart->generate_cart_id($product->get_id())){
$text='购物车中的产品';
}
}
}
}
返回$text;
}

这可以写得更简洁,但是通过将条件分散到多个if语句中,如果仍然收到错误消息,您可以快速确定哪里出了问题

/* Change the text of the add to cart button for the archive and categories */
add_filter( 'woocommerce_product_add_to_cart_text', 'new_products_button_text', 20, 2 );
 
function new_products_button_text( $text, $product ) {
 
    if( 
       $product->is_type( 'simple' )
       && $product->is_purchasable()
       && $product->is_in_stock()
       && WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) )
    ) { 
        $text = 'Product in Cart'; 
    }
 
    return $text;
 
}

/* Change the add to cart button text for the product page */
add_filter( 'woocommerce_product_single_add_to_cart_text', 'new_single_product_button_text' );
 
function new_single_product_button_text( $text ) {
 
    if( WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( get_the_ID() ) ) ) {
        $text = 'Product in Cart';
    }
 
    return $text;
 
}

add_action( 'wp_footer', 'action_wp_footer' );
function action_wp_footer() {
    ?>
    <script>
        jQuery(document).ready(function($) {
            var selector = '.add_to_cart_text:contains("Product in Cart")';         
            
            // Selector contains specific text
            if ( $( selector ).length > 0 ) {
                $( selector ).addClass( 'product-is-added' );
            } else {
                $( selector ).removeClass( 'product-is-added' );            
            }
            
        });
    </script>
    <?php
}

add_action( 'wp_footer', 'ajax_button_text_js_script' );
function ajax_button_text_js_script() {
    $text = __('Product in Cart', 'woocommerce');
    ?>
    <script>
        jQuery(function($) {
            var text = '<?php echo $text; ?>',      $this;

            $(document.body).on('click', '.ajax_add_to_cart', function(event){
                $this = $(this); // Get button jQuery Object and set it in a variable
            });

            $(document.body).on('added_to_cart', function(event,b,data){
                var buttonText = '<span class="add_to_cart_text product-is-added">'+text+'</span><i class="cart-icon pe-7s-cart"></i>';

                // Change inner button html (with text) and Change "data-tip" attribute value
                $this.html(buttonText).attr('data-tip',text);
            });
        });
    </script>
    <?php
}
&& WC()->cart->find_product_in_cart( WC()->cart->generate_cart_id( $product->get_id() ) )