Create custom post type with their custom taxonomies

Find below code to create custom post type with their custom taxonomies.

//Create Custom Post in wordpess
add_action( ‘init’, ‘create_post_type’ );
function create_post_type() {
register_post_type( ‘custom_post’,
array(
‘labels’ => array(
‘name’ => __( “Custom Post” ),
‘singular_name’ => __( ‘Custom’ )
),
‘public’ => true,
‘has_archive’ => true,
‘supports’ => array(‘title’,’editor’,’thumbnail’),
‘taxonomies’ => array(‘topics’),
)
);
}
//hook into the init action and call custom taxonomies
add_action( ‘init’, ‘create_topics_hierarchical_taxonomy’, 0 );

//create a custom taxonomy name it topics for your posts

function create_topics_hierarchical_taxonomy() {

// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI

$labels = array(
‘name’ => _x( ‘Topics’, ‘taxonomy general name’ ),
‘singular_name’ => _x( ‘Topic’, ‘taxonomy singular name’ ),
‘search_items’ => __( ‘Search Topics’ ),
‘all_items’ => __( ‘All Topics’ ),
‘parent_item’ => __( ‘Parent Topic’ ),
‘parent_item_colon’ => __( ‘Parent Topic:’ ),
‘edit_item’ => __( ‘Edit Topic’ ),
‘update_item’ => __( ‘Update Topic’ ),
‘add_new_item’ => __( ‘Add New Topic’ ),
‘new_item_name’ => __( ‘New Topic Name’ ),
‘menu_name’ => __( ‘Topics’ ),
);

// Now register the taxonomy

register_taxonomy(‘topics’,array(‘post’), array(
‘hierarchical’ => true,
‘labels’ => $labels,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘topic’ ),
));

}

Advertisements

Woo-commerce add_to_cart function with custom data

Please find below code…

/*Custom data*/
$cart_item_data = array(‘inputurl’ => “data”);

/*Variation*/
$variation = array(‘attribute_pa_color’ => $color);

WC()->cart->add_to_cart( $product_id, $quntity, $variation_id, $variation , $cart_item_data );

/*Add custom data using filter hook wordpress*/
Put below code in functions.php

add_filter( ‘woocommerce_add_cart_item_data’,’add_cart_item_data’, 10, 3 );
function add_cart_item_data( $cart_item_meta, $product_id, $variation_id ) {
// function code
}

Woo-commerce functionality with variation products

Woo-commerce functionality with variation products
/* Set woo-commerce session */
WC()->session->set( ‘gift_user’, “data” );

/* Get woo-commerce session */
WC()->session->get( ‘gift_user’);

/* Store variation using variation ID */
$variation = array(‘attribute_variation’ => $color);

/* Empty Woo-Commerce cart */
WC()->cart->empty_cart();

/*Get Woo-commerce price*/
$_product = wc_get_product( $product_id );

$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();

WooCommerce rename or remove coupon message on checkout page

Please find below code to change coupon code message.

function woocommerce_rename_coupon_message_on_checkout() {
return ‘ <a href=”#” class=”showcoupon”>’ . __( ‘Have a promotional code?’, ‘woocommerce’ ) . ‘</a>’;
}
add_filter( ‘woocommerce_checkout_coupon_message’, ‘woocommerce_rename_coupon_message_on_checkout’ );

/* Remove successfully add to cart message */

add_filter( ‘wc_add_to_cart_message’, ‘__return_empty_string’ );