استفاده از short code یک قالب دیگر

پرسیده شده
فعالیت 1439 روز پیش
دیده شده 857 بار
0

با سلام چه طور میتونم یک شورت کد که برای قالب دیگری نوشته شده رو به قالب دیگر منتقل کنم؟ من فایل شورت کد رو توی همون مسیر در قالب جدید گذاشتم و کد رو هم به فانکشن اضافه کردم ولی کار نمیکرد. چیکارباید کنم؟

 

در فایل function.php قالب اصلی کد زیر نوشته شده است:

require get_template_directory() . '/inc/short-codes.php';

و در فایل short-codes.php هم موارد زیر نوشته شده است:

<?php
// Add Shortcode
function plato_custom_coffee_generate( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'data' => '',
		),
		$atts,
		'custom_coffee'
    );
    if (is_user_logged_in()){
		return '<div id="plato_custom_coffee" data-data="'.$atts['data'].'"></div>';
	}
	else{
		return do_shortcode('[lrm_form default_tab="login" logged_in_message="You have been already logged in!"]');
	}
    
}
add_shortcode( 'custom_coffee', 'plato_custom_coffee_generate' );

/* Ajax Submit order Process */
add_action('wp_ajax_nopriv_submitorder', 'subscribeTosubmitorder');
add_action('wp_ajax_submitorder', 'subscribeTosubmitorder');

function subscribeTosubmitorder()
{
	$input = $_POST['data'];

	$posts = json_decode(stripslashes($_POST['data']));

	ob_start();

        //$product_id        = 264;
        $product_id        = 382;
		$quantity          = 1;
		$cart_item_data = array();   
		foreach ($posts as $key => $value) {
			$cart_item_data[$value->step] = $value->name;
		}
        $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
        $product_status    = get_post_status( $product_id );
		if( isset( $cart_item_data['coffee_type'] ) ) {

			if($cart_item_data["coffee_type"] == 'کافئین بالا'){
				$variation_id=398;
			}
			elseif($cart_item_data["coffee_type"] == 'کافئین متوسط'){
				$variation_id=397;
			}
			elseif($cart_item_data["coffee_type"] == 'کافئین بسیار پایین'){
				$variation_id=396;
			}
		}
		else{
			$variation_id=0;
		}
        if ( $passed_validation && WC()->cart->add_to_cart( $product_id, $quantity,$variation_id ,array(),$cart_item_data  ) && 'publish' === $product_status ) {

            do_action( 'woocommerce_ajax_added_to_cart', $product_id );
			WC()->cart->calculate_totals();
			// Save cart to session
			WC()->cart->set_session();
			// Maybe set cart cookies
			WC()->cart->maybe_set_cart_cookies();
			$cart_page_id = wc_get_page_id( 'cart' );
			$cart_page_url = $cart_page_id ? get_permalink( $cart_page_id ) : '';
			// If there was an error adding to the cart, redirect to the product page to show any errors
            $data = array(
                'success'       => true,
                'checkout_url' =>  $cart_page_url

			);

			wp_send_json( $data );
	
        } else {

            // If there was an error adding to the cart, redirect to the product page to show any errors
            $data = array(
                'error'       => true,
                'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id )
            );

            wp_send_json( $data );

		}
}

function woocommerce_custom_price_to_cart_item( $cart_object ) {  
    if( !WC()->session->__isset( "reload_checkout" )) {
        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["coffee_value"] ) ) {
                //for woocommerce version lower than 3
                //$value['data']->price = $value["custom_price"];
                //for woocommerce version +3  
				$variation_id = $value["variation_id"];
				$price = get_post_meta($variation_id, '_price', true);
				if($value["coffee_value"] == '250 گرم'){
					$value['data']->set_price($price*0.25);
				}
				elseif($value["coffee_value"] == '500 گرم'){
					$value['data']->set_price($price*0.5);
				}
	   		}
		}  
    }  
}
add_action( 'woocommerce_before_calculate_totals', 'woocommerce_custom_price_to_cart_item', 99 );

/**
 * Display custom item data in the cart
 */
function plugin_republic_get_item_data( $item_data, $cart_item_data ) {
	if( isset( $cart_item_data['coffee_type'] ) ) {
		$item_data[] = array(
		'key' => __( 'نوع قهوه', 'plugin-republic' ),
		'value' => wc_clean( $cart_item_data['coffee_type'] )
		);
		}
		if( isset( $cart_item_data['coffee_amount'] ) ) {
			$item_data[] = array(
			'key' => __( 'مقدار کافئین', 'plugin-republic' ),
			'value' => wc_clean( $cart_item_data['coffee_amount'] )
			);
		}
		if( isset( $cart_item_data['coffee_sour'] ) ) {
			$item_data[] = array(
			'key' => __( 'ترشی', 'plugin-republic' ),
			'value' => wc_clean( $cart_item_data['coffee_sour'] )
			);
		}
		if( isset( $cart_item_data['coffee_bitter'] ) ) {
			$item_data[] = array(
			'key' => __( 'تلخی', 'plugin-republic' ),
			'value' => wc_clean( $cart_item_data['coffee_bitter'] )
			);
		}
		if( isset( $cart_item_data['coffee_mill'] ) ) {
			$item_data[] = array(
			'key' => __( 'نوع آسیاب', 'plugin-republic' ),
			'value' => wc_clean( $cart_item_data['coffee_mill'] )
			);
		}
		if( isset( $cart_item_data['coffee_value'] ) ) {
			$item_data[] = array(
			'key' => __( 'حجم انتخابی', 'plugin-republic' ),
			'value' => wc_clean( $cart_item_data['coffee_value'] )
			);
		}
	return $item_data;
   }
   add_filter( 'woocommerce_get_item_data', 'plugin_republic_get_item_data', 10, 2 );
/**
 * Add custom meta to order
 */
function plugin_republic_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
	if( isset( $values['coffee_type'] ) ) {
		$item->add_meta_data(
		__( 'نوع قهوه', 'plugin-republic' ),
		$values['coffee_type'],
		true
		);
		}
	if( isset( $values['coffee_amount'] ) ) {
		$item->add_meta_data(
		__( 'مقدار کافئین', 'plugin-republic' ),
		$values['coffee_amount'],
		true
		);
		}
	if( isset( $values['coffee_sour'] ) ) {
		$item->add_meta_data(
		__( 'ترشی', 'plugin-republic' ),
		$values['coffee_sour'],
		true
		);
		}
	if( isset( $values['coffee_bitter'] ) ) {
		$item->add_meta_data(
		__( 'تلخی', 'plugin-republic' ),
		$values['coffee_bitter'],
		true
		);
		}
	if( isset( $values['coffee_mill'] ) ) {
		$item->add_meta_data(
		__( 'نوع آسیاب', 'plugin-republic' ),
		$values['coffee_mill'],
		true
		);
		}
	if( isset( $values['coffee_value'] ) ) {
		$item->add_meta_data(
		__( 'حجم انتخابی', 'plugin-republic' ),
		$values['coffee_value'],
		true
		);
		}
   }
add_action( 'woocommerce_checkout_create_order_line_item', 'plugin_republic_checkout_create_order_line_item', 10, 4 );
   

?>

دیگه باید چه چیزهایی  از چه قسمت هایی کپی کنم تا بتونم این قسمت رو به قالب جدید منتقل کنم؟

 

ممنون میشم پاسخ دقیق و کاملی رو بهم بدید تا بتونم با موفقیت انجامش بدم.

فایل پیوست

محمد بانشی
محمد بانشی

2 خرداد 99

1
حذف شده

سلام خدمت شما. همون طور که خودتون هم انجام دادید کافیه کدهای رو داخل فایل functions.php به صورت صحیح قرار بدید و هم چنین شورتکد رو به صورت صحیح فرخوانی کنید.

فایل پیوست

کیوان علی محمدی

توسط

کیوان علی محمدی

4 خرداد 99

حذف شده
استاد این کار رو کردم اما در فایل فانکشن ارور میده. روی قالب دیگه هم تست کردم ارور نمیداد اما اجرا هم نمیشد. چاره چیست؟
محمد بانشی

5 خرداد 99

حذف شده
استاد این کار رو کردم اما در فایل فانکشن ارور میده. روی قالب دیگه هم تست کردم ارور نمیداد اما اجرا هم نمیشد. چاره چیست؟
محمد بانشی

5 خرداد 99