标签: WooCommerce

  • WordPress 取消引用 stats.wp.com/w.js 图文教程

    WordPress 取消引用 stats.wp.com/w.js 图文教程

    最近发现WordPress后台自动引用了stats.wp.com/w.js

    访问后台变慢,浏览器下方提示:“正在与stats.wp.com进行TLS握手…” 导致的速度变慢问题。

    正在与stats.wp.com进行TLS握手是Woocommerce后台在访问stats.wp.com造成的,具体原因在我们进行分析之后如下:

    这里注册了一个woo-tracks的js,这个js是远程调用的stats.wp.com

    这个js应该是在woocommerce配置时开启了woocommerce跟踪功能,让woocommerce能够从用户处搜集一些数据以更好地提供更新服务。

    这个跟踪功能如果开启了,每次进入woocommerce相关后台界面(不影响前端)都会加载stats.wp.com,而这个网站的速度在国内访问会比较慢,因此如果你觉得这个问题比较影响后台体验,可以按照下面的方式禁止访问,这样可以提高后台的体验速度。

    禁止woocommerce后台访问stats.wp.com,避免速度降低

    以上可知注册的js的id为woo-tracks

    那么我们可以再主题的functions.php最后一行中加入代码去除这个注册

    //禁止发送统计到`stats.wp.com`
    function uzi_remove_woo_tracks()
    {
    	wp_deregister_script('woo-tracks');
    	wp_deregister_script('jp-tracks');
    }
    add_action('init', 'uzi_remove_woo_tracks');

    加入完成之后,这个js不再加载,后台速度也变得正常了。

  • WooCommerce创建各种表单 woocommerce_form_field() 函数 代码调用实例

    WooCommerce创建各种表单 woocommerce_form_field() 函数 代码调用实例

    woocommerce_form_field() 是用来创建WooCommerce里各种表单元素的函数,本文列举了输出常见元素的代码示例,供参考。

    Text Box / 文本域

    woocommerce_form_field("my_textbox", array(
        'type'     			=> 'text',
        'class'    			=> array('form-row-wide my-textbox'),
        'label'    			=> 'Textbox Field',
        'placeholder' 		=> 'Placehoder text',
        'required'			=> true,
        'default'  			=> ''
    ), $checkout->get_value( 'my_textbox' ) );

    Checkbox / 复选框

    woocommerce_form_field("my_textbox", array(
        'type'     			=> 'checkbox',
        'class'    			=> array('form-row-wide my-checkbox'),
        'label'    			=> 'Checkbox Field',
        'description'		=> 'A short description of this checkbox',
        'default'  			=> ''
    ), $checkout->get_value( 'my_textbox' ) );

    Textarea / 文本域

    woocommerce_form_field("my_textarea", array(
        'type'     			=> 'textarea',
        'class'    			=> array('form-row-wide my-textarea'),
        'label'    			=> 'Textarea Field',
        'custom_attributes' => array( 'rows' => 10, 'cols' => 10 ),
        'default'  			=> ''
    ), $checkout->get_value( 'my_textarea' ) );

    Select / 下拉列表

    woocommerce_form_field("my_select", array(
        'type'     			=> 'select',
        'class'    			=> array('form-row-wide my-select'),
        'label'    			=> 'Dropdown',
        'options'			=> array( '1' => 'Option 1' , '2' => 'Option 2', '3' => 'Option 3' ),
    ), $checkout->get_value( 'my_select' ) );

    Radio / 单选按钮

    woocommerce_form_field("my_radio", array(
        'type'     			=> 'radio',
        'class'    			=> array('form-row-wide my-radio'),
        'label'    			=> 'Radio',
        'label_class'		=> 'radio-box',
        'options'			=> array( '1' => 'Option 1' , '2' => 'Option 2', '3' => 'Option 3' ),
    ), $checkout->get_value( 'my_radio' ) );

    Pasword / 密码域

    woocommerce_form_field("my_textbox", array(
        'type'     			=> 'password',
        'class'    			=> array('form-row-wide my-textbox'),
        'label'    			=> 'Password',
        'placeholder' 		=> '',
        'required'			=> true,
        'default'  			=> ''
    ), $checkout->get_value( 'my_textbox' ) );

    控制分栏

    通过给”class”参数传递适当的值,可以控制表单字段占全宽还是1/2宽度

    form-row-wide: 全宽

    form-row-first: 1/2宽度,第一栏

    form-row-last:1/2宽度,第二栏

    增加清除浮动结构

    要在表单字段后输出<div class="clear"></div>,增加'clear' => true

    创建自定义字段之input type=“hidden”

    WooCommerce的表单API可以增加自定义字段,例如输出如下结构

    <p class="form-row form-row-wide my-hidden-field" id="my_hidden_field_field">
    	<input type="hidden" class="input-text " name="my_hidden_field" id="my_hidden_field" value="">
    </p>

    首先创建产生这个结构的处理代码

    function wc_form_hidden_field( $field, $key, $args, $value ){
    	$defaults = array(
    		'label'             => '',
    		'id'                => $key,
    		'class'             => array(),
    		'input_class'       => array(),
    		'custom_attributes' => array(),
    		'default'           => '',
    	);	
    	$args = wp_parse_args( $args, $defaults );
    	// Custom attribute handling
    	$custom_attributes = array();
    	if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) )
    		foreach ( $args['custom_attributes'] as $attribute => $attribute_value )
    			$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
    	$field = '<p class="form-row ' . esc_attr( implode( ' ', $args['class'] ) ) .'" id="' . esc_attr( $args['id'] ) . '_field">';
    	$field .= '<input type="hidden" class="input-text ' . esc_attr( implode( ' ', $args['input_class'] ) ) .'" name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" value="' . esc_attr( $value ) . '" ' . implode( ' ', $custom_attributes ) . ' /></p>';
    	
    	return $field;
    }
    add_filter( 'woocommerce_form_field_hidden', 'wc_form_hidden_field', 10, 4 );

    然后正常调用woocommerce_form_field() 创建隐藏字段,type为hidden

    woocommerce_form_field("my_hidden_field", array(
        'type'     			=> 'hidden',
        'class'    			=> array('form-row-wide my-hidden-field'),
        'label'    			=> 'Hidden Field',
        'placeholder' 		=> '',
        'default'  			=> ''
    ), $checkout->get_value( 'my_hidden_field' ) );
  • WordPress WooCommerce检测某个产品ID Product ID 是否在订单中 代码实例

    WordPress WooCommerce检测某个产品ID Product ID 是否在订单中 代码实例

    在WordPress的主题WooCommerce商城开发插件中,我们需要检测一个产品id是否在某个订单中,下面我们记录一下代码实例

    function check_order_product_id($order_id,$the_product_id)
    {
        $order = wc_get_order((int)$order_id);
        $items = $order->get_items();
        foreach ($items as $item_id => $item) {
            $product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
            if ($product_id === (int)$the_product_id) {
                return true;
            }
        }
        return false;
    }
    
  • WooCommerce Javascpt或PHP 添加购物车功能 图文教程

    WooCommerce Javascpt或PHP 添加购物车功能 图文教程

    前篇老季写了WordPress 商城开发 WooCommerce 插件创建自定义产品类型Product_type 并在前端页面循环中隐藏,本文我们将记录一下,如何使用PHP或Javascript功能来实现添加到购物车功能。

    Javascript

    <script type="text/javascript">
    	function jwc_add_to_cart( product_id ) {
    		if ( 'undefined' === typeof wc_add_to_cart_params ) {
    			// The add to cart params are not present.
    			return false;
    		}
    		var data = {
    			product_id: product_id,
    			quantity: 1,
    		};
    		jQuery.post( wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ), data, function( response ) {
    			if ( ! response ) {
    				return;
    			}
    			// This redirects the user to the product url if for example options are needed ( in a variable product ).
    			// You can remove this if it's not the case.
    			if ( response.error && response.product_url ) {
    				window.location = response.product_url;
    				return;
    			}
    			// Remove this if you never want this action redirect.
    			if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
    				window.location = wc_add_to_cart_params.cart_url;
    				return;
    			}
    			// This is important so your theme gets a chance to update the cart quantity for example, but can be removed if not needed.
    			jQuery( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash ] );
    		});
    	}
    </script>

    PHP

    WC()->cart->add_to_cart( $product_id, $quantity );
    

  • WordPress 商城开发 WooCommerce 插件创建自定义产品类型Product_type 并在前端页面循环中隐藏

    WordPress 商城开发 WooCommerce 插件创建自定义产品类型Product_type 并在前端页面循环中隐藏

    近日老季在Wordpress的商城插件Woocommerce开发中,创建自定义的产品product_type类型,下面给出代码段实例:

    
    add_filter( 'product_type_selector', 'pd_add_jappt_product_type' );
     
    function pd_add_jappt_product_type( $types ){
        $types[ 'jappt_service' ] = 'Jappt Service';
        return $types;
    }
     
    /* 
     * Add New Product Type Class
     */
    add_action( 'init', 'pd_create_jappt_product_type' );
     
    function pd_create_jappt_product_type(){
        class WC_Product_Jappt_Service extends WC_Product {
            public function get_type() {
                return 'jappt_service';
            }
        }
    }
     
    /* 
     * Load New Product Type Class
     */
    add_filter( 'woocommerce_product_class', 'pd_woocommerce_product_class', 10, 2 );
     
    function pd_woocommerce_product_class( $classname, $product_type ) {
        if ( $product_type == 'jappt_service' ) { 
            $classname = 'WC_Product_Jappt_Service';
        }
        return $classname;
    }
    
    // Adding Price fields & inventory to jappt product type
    add_action('admin_footer', 'pd_jappt_product_admin_jappt_js');
    function pd_jappt_product_admin_jappt_js() {
        if ('product' != get_post_type()) :
            return;
        endif;
        ?>
        <script type='text/javascript'>
            jQuery(document).ready(function () {
                //for Price tab
                jQuery('.options_group.pricing').addClass('show_if_jappt_service').show();
                // //for Inventory tab
                // jQuery('.inventory_options').addClass('show_if_jappt').show();
                // jQuery('#inventory_product_data ._manage_stock_field').addClass('show_if_jappt').show();
                // jQuery('#inventory_product_data ._sold_individually_field').parent().addClass('show_if_jappt').show();
                // jQuery('#inventory_product_data ._sold_individually_field').addClass('show_if_jappt').show();
            });
        </script>
        <?php
    }
    
    // add the settings under ‘General’ sub-menu
    add_action( 'woocommerce_product_options_general_product_data', 'pd_add_jappt_settings' );
    function pd_add_jappt_settings() {
        // global $woocommerce, $post;
        echo '<div class="options_group">';
    
        echo '</div>';
    }
    

    jappt_service 这个类型的产品类型,不允许在正常的产品页面loop中展示,下面我们给出代码段实例

    add_action( 'woocommerce_product_query', 'hide_jappt_products_type' );
       
    function hide_jappt_products_type( $q ) {
        
        if( is_admin() )    
            return;
        $tax_query = (array) $q->get( 'tax_query' );
      
        $tax_query[] = array(
               'taxonomy' => 'product_type',
               'field' => 'slug',
               'terms' => array( 'jappt_service' ), // Category slug here
               'operator' => 'NOT IN'
        );
      
      
        $q->set( 'tax_query', $tax_query );
      
    }

    以上代码需要放入对应主题的 functions.php 中才能生效。

  • WordPress WooCommerce不显示价格 不显示购物车按钮

    不显示价格

    不显示价格,去掉价格字段即可 把代码放在主题的functions.php下

    //Hide price
    add_filter( 'woocommerce_get_price_html', 'woocommerce_hide_price' );
    function woocommerce_hide_price( $price ){
        return '';
    }

    去掉购买按钮

    //Hide buy button
    add_action( 'init', 'woocommerce_hide_buy_button' );
    function woocommerce_hide_buy_button(){
        remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
        remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    }

    wc-ajax

    没有购买按钮,就无法向购物车中添加任何产品,购物车页面可有可无。
    如果完全不需要购物功能,将WooCommerce的购物车、结账等页面删除即可(此项未亲测)。

    在实际的使用过程中,会发现”wc-ajax = get_refreshed_fragments”的加载时间为1.5秒,然后这并不是必须的。并且延缓了网站的加载速度。可以加入代码:

    add_action( 'wp_print_scripts', 'de_script', 100 );
    function de_script() {
        wp_dequeue_script( 'wc-cart-fragments' );
        return true;
    }

    删除产品架构标记

    /**
     * 从“产品类别”和“商店”页面中删除生成的产品架构标记。
     */
    function wc_remove_product_schema_product_archive() {
    	remove_action( 'woocommerce_shop_loop', array( WC()->structured_data, 'generate_product_data' ), 10, 0 );
    }
    add_action( 'woocommerce_init', 'wc_remove_product_schema_product_archive' );
  • WordPress 删除WooCommerce商品详情评论选项卡

    使用wordpress建设网站时,会遇到不想像显示产品评论 信息选项卡太多内容,如何从WooCommerce单一产品页面中删除描述和评论标签。有时我们需要从单一产品页面中删除产品描述和产品评论标签,以使单个产品页面简单而令人印象深刻,或者我们不希望该用户将分享关于网站上列出的产品的评论。如何删除WooCommerce中的描述,评论和其他信息选项卡:

    在functions.php文件的末尾使用下面的代码。

    add_filter( 'woocommerce_product_tabs', 'wcs_woo_remove_reviews_tab', 98 );
    function wcs_woo_remove_reviews_tab($tabs) {
        unset($tabs['reviews']);
        return $tabs;
    }

    此外,要删除额外的信息选项卡,然后使用下面的你的functions.php文件:

    add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
    function woo_remove_product_tabs( $tabs ) {
        unset( $tabs['description'] ); // Remove the description tab
        unset( $tabs['reviews'] ); // Remove the reviews tab
        unset( $tabs['additional_information'] ); // Remove the additional information tab
        return $tabs;
    }
  • Woocommerce文件构成及修改默认模板结构介绍

    Woocommerce文件构成及修改默认模板结构介绍

    修改默认模板样式

    WooCommerce 自带模板在 wp-content/plugins/woocommerce/templates 下。如果想修改默认的样式,可以在当前使用的主题下面创建一个文件夹:woocommerce,粘贴 templates 下所有文件到这个文件。

    文件目录功能

    • auth 用户登录登出认证模板
    • cart 购物车的模板
    • checkout 结账的模板
    • global 公用文件模板
    • loop 商品循环模板
    • myaccount 账号信息模板
    • notices 订购消息模板
    • order 订单模板
    • single-product 单个产品模板
WeChat
↑